Skip to content

state_attr in template causes SHA1 to be undefined on boot / reload #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Joannou1 opened this issue Mar 6, 2025 · 1 comment
Closed

Comments

@Joannou1
Copy link

Joannou1 commented Mar 6, 2025

What version of Spook are you using?

3.1.0

What version of Home Assistant are you using?

2025.3.0

The problem

I'm using the SHA1 extension in a template for a REST sensor.
When I reboot or reload the configuration, it tried to use the function before it's ready.

Error while setting up rest platform for sensor: UndefinedError: 'sha1' is undefined

If I copy the same code into the template editor it works without an issue as spook / sha1 is ready.
This completely breaks my template unfortunately.

Anything in the logs? Paste it here!

Logger: homeassistant.components.sensor
Source: helpers/entity_platform.py:382
integration: Sensor (documentation, issues)
First occurred: 19:38:32 (6 occurrences)
Last logged: 19:38:32

Error while setting up rest platform for sensor: UndefinedError: 'sha1' is undefined
Error while setting up rest platform for sensor: TypeError: can only concatenate str (not "NoneType") to str
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 643, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2756, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 2, in top-level template code
  File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 399, in call
    if not __self.is_safe_callable(__obj):
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 3173, in is_safe_callable
    ) or super().is_safe_callable(obj)
         ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/sandbox.py", line 265, in is_safe_callable
    getattr(obj, "unsafe_callable", False) or getattr(obj, "alters_data", False)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2788, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'sha1' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in _async_setup_platform
    await asyncio.shield(awaitable)
  File "/usr/src/homeassistant/homeassistant/components/rest/sensor.py", line 84, in async_setup_platform
    rest = create_rest_data_from_config(hass, conf)
  File "/usr/src/homeassistant/homeassistant/components/rest/__init__.py", line 206, in create_rest_data_from_config
    resource = resource_template.async_render(parse_result=False)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 645, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'sha1' is undefined

snippet of Configuration.yml

sensor:
  - platform: rest
    scan_interval: 120
    name: Trailer Inverter Data
    resource_template: !secret dessmonitor_api_uri
    method: GET
    json_attributes_path: "$.dat.pars"
    json_attributes:
      - gd_
      - sy_
      - pv_
      - bt_
      - bc_
    value_template: "OK"

snippet of secret.yml

dessmonitor_api_uri: >-  
  {% set dess_action = "querySPDeviceLastData" %}
  {% set dess_salt = "xxxxxx" %}
  {% set dess_secret = state_attr('sensor.dess_auth', 'secret') %}
  {% set dess_token = state_attr('sensor.dess_auth', 'token') %}
  {% set dess_pn = "xxxxxx" %}
  {% set dess_sn = "xxxxxx" %}

  {% set current_date = (as_timestamp(now())) | timestamp_custom("%Y-%m-%d", True) %}
  {% set signature = sha1(dess_salt + dess_secret + dess_token + "&action=" + dess_action + "&source=1&pn=" + dess_pn + "&sn=" + dess_sn + "&devcode=2376&devaddr=1&i18n=en_US") %}

  {% set baseURL = "https://api.dessmonitor.com/public/?sign=" + signature + "&salt=" + dess_salt + "&token=" + dess_token + "&action=" + dess_action + "&source=1&pn=" + dess_pn + "&sn=" + dess_sn + "&devcode=2376&devaddr=1&i18n=en_US" %}
  {{ baseURL }}

I have noticed that it's the state_attr('sensor.dess_auth', 'secret') %} that causes this error.
If I input a string instead of a state_attr, it works normally.
I need the state_attr because the secret and tokens that need to be SHA1'd constantly change.
I'm guessing there's some sort of dependency / load order thing that changes when state_attr is introduced?

@Joannou1 Joannou1 changed the title SHA1 is undefined on boot / reload state_attr in template causes SHA1 to be undefined on boot / reload Mar 6, 2025
@frenck
Copy link
Owner

frenck commented Mar 6, 2025

Unfortunately a duplicate / related to #762

../Frenck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants