Skip to content

zha: Clean up listener registration and button state#14197

Merged
balloob merged 1 commit intohome-assistant:devfrom
rcloran:zha-better-buttons
May 1, 2018
Merged

zha: Clean up listener registration and button state#14197
balloob merged 1 commit intohome-assistant:devfrom
rcloran:zha-better-buttons

Conversation

@rcloran
Copy link
Copy Markdown
Contributor

@rcloran rcloran commented Apr 30, 2018

  • Instead of registering listeners in the entity init, do it in
    async_added_to_hass to avoid errors updating an entity which isn't fully
    set up yet
  • schedule_update_ha_state -> async_schedule_update_ha_state

Description:

Related issue (if applicable): fixes #

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.github.io#<home-assistant.github.io PR number goes here>

Example entry for configuration.yaml (if applicable):

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (81 > 79 characters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a future PR: add async_will_remove_from_hass and you're almost able to load/unload ZHA while Home Assistant is running 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called from an async context?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinHjelmare's comments on #12528 suggested I should change this ... but, reading the code, it looks like I shouldn't be changing this. These are run from callbacks. Since async_... is a coro, I'd need to ensure it runs, and that's exactly what the schedule_... does.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to know what is calling the callback, a thread or the event loop. That was my question.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event loop

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, in that case async_schedule_update_ha_state() is ok

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since that is a coro, doesn’t it need to be scheduled anyways?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh, I was reading async_update_ha_state, not async_schedule_update_ha_state. Alright, this all makes much more sense now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will reset the listeners added in the child. You can init the parent before the setting the rest of the child's attributes.

 - Instead of registering listeners in the entity __init__, do it in
   async_added_to_hass to avoid errors updating an entity which isn't fully
   set up yet
 - Change from schedule_update_ha_state to async_schedule_update_ha_state
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@balloob balloob merged commit 9d4d1c8 into home-assistant:dev May 1, 2018
@balloob balloob mentioned this pull request May 11, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants