snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
274 stars 16 forks source link

Using self.async_update_ha_state() #99 #100

Closed ercsey closed 5 months ago

ercsey commented 1 year ago

Fixes issue #99

snarky-snark commented 1 year ago

Thanks for your contribution.

I would like to merge this in. Can you explain why line 319 was removed rather than replaced with a call to async_write_ha_state?

snarky-snark commented 1 year ago

And why remove the await?

danielbrunt57 commented 11 months ago

I just checked my installed package and it did not have line 319: await self.async_update_ha_state(True)

        # Restore previous values on startup and reload
        if self._restore == True:
            state = await self.async_get_last_state()
            if state:
                self._value = state.state
                if state.attributes and self._attribute_templates:
                    if not hasattr(self,'_attr_extra_state_attributes'):
                        setattr(self, '_attr_extra_state_attributes', {})
                    for key, template in self._attribute_templates.items():
                        self._attr_extra_state_attributes[key] = state.attributes.get(key)

    async def async_will_remove_from_hass(self):
        # Remove event listeners when the entity is removed from hass (for instance when 'reload' is triggered by user)

image

I changed both occurences of await self.async_update_ha_state to await self.async_write_ha_state, restarted HA and now have a new error:

Logger: homeassistant.components.automation.waze_update_travel_time_to_destination
Source: components/automation/__init__.py:655
Integration: Automation (documentation, issues)
First occurred: 12:08:25 PM (1 occurrences)
Last logged: 12:08:25 PM

While executing automation automation.waze_update_travel_time_to_destination
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 655, in async_trigger
    await self.action_script.async_run(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1578, in async_run
    return await asyncio.shield(run.async_run())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 420, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 470, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 493, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2012, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2049, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 235, in handle_service
    return await service.entity_service_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 876, in entity_service_call
    response_data = await _handle_entity_call(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 948, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/config/custom_components/var/__init__.py", line 447, in async_set
    await self.async_write_ha_state()
TypeError: object NoneType can't be used in 'await' expression

I'm not a python programmer but I do have oodles of other programming experience and my guess is... await self.async_write_ha_state() needs to be self.async_write_ha_state() becase () is null whereas the next await self.async_write_ha_state(true) is okay because (true) is not null?

danielbrunt57 commented 11 months ago

I've restarted HA and my startup automations which reference var completed successfully and there are no other errors.

image

image

lloyda commented 10 months ago

@danielbrunt57 said:

I just checked my installed package and it did not have line 319: await self.async_update_ha_state(True)

My installed package (0.15.0) is also missing that line. v0.15.0 (the latest release) was released 18th July, and line 319 was introduced 28th July.

danielbrunt57 commented 10 months ago

My installed package (0.15.0) is also missing that line. v0.15.0 (the latest release) was released 18th July, and line 319 was introduced 28th July. @snarky-snark That change (and many others) seems to have been pushed after 18th July 2022.