Skip to content
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

Using self.async_update_ha_state() #99 #100

Closed
wants to merge 1 commit into from

Conversation

ercsey
Copy link

@ercsey ercsey commented Jun 8, 2023

Fixes issue #99

@ercsey ercsey mentioned this pull request Jul 17, 2023
@snarky-snark
Copy link
Owner

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
Copy link
Owner

snarky-snark commented Jul 18, 2023

And why remove the await?

@danielbrunt57
Copy link
Contributor

danielbrunt57 commented Oct 13, 2023

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
Copy link
Contributor

danielbrunt57 commented Oct 13, 2023

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

image

image

@lloyda
Copy link

lloyda commented Oct 15, 2023

@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
Copy link
Contributor

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.

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

Successfully merging this pull request may close these issues.

4 participants