-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
zha: Clean up listener registration and button state #14197
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,11 +288,6 @@ class Entity(entity.Entity): | |
| """A base class for ZHA entities.""" | ||
|
|
||
| _domain = None # Must be overridden by subclasses | ||
| # Normally the entity itself is the listener. Base classes may set this to | ||
| # a dict of cluster ID -> listener to receive messages for specific | ||
| # clusters separately | ||
| _in_listeners = {} | ||
| _out_listeners = {} | ||
|
|
||
| def __init__(self, endpoint, in_clusters, out_clusters, manufacturer, | ||
| model, application_listener, unique_id, **kwargs): | ||
|
|
@@ -321,19 +316,30 @@ def __init__(self, endpoint, in_clusters, out_clusters, manufacturer, | |
| kwargs.get('entity_suffix', ''), | ||
| ) | ||
|
|
||
| for cluster_id, cluster in in_clusters.items(): | ||
| cluster.add_listener(self._in_listeners.get(cluster_id, self)) | ||
| for cluster_id, cluster in out_clusters.items(): | ||
| cluster.add_listener(self._out_listeners.get(cluster_id, self)) | ||
|
|
||
| self._endpoint = endpoint | ||
| self._in_clusters = in_clusters | ||
| self._out_clusters = out_clusters | ||
| self._state = ha_const.STATE_UNKNOWN | ||
| self._unique_id = unique_id | ||
|
|
||
| # Normally the entity itself is the listener. Sub-classes may set this | ||
| # to a dict of cluster ID -> listener to receive messages for specific | ||
| # clusters separately | ||
| self._in_listeners = {} | ||
| self._out_listeners = {} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| application_listener.register_entity(ieee, self) | ||
|
|
||
| async def async_added_to_hass(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a future PR: add |
||
| """Callback once the entity is added to hass. | ||
|
|
||
| It is now safe to update the entity state | ||
| """ | ||
| for cluster_id, cluster in self._in_clusters.items(): | ||
| cluster.add_listener(self._in_listeners.get(cluster_id, self)) | ||
| for cluster_id, cluster in self._out_clusters.items(): | ||
| cluster.add_listener(self._out_listeners.get(cluster_id, self)) | ||
|
|
||
| @property | ||
| def unique_id(self) -> str: | ||
| """Return a unique ID.""" | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 theschedule_...does.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event loop
There was a problem hiding this comment.
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 okThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.