Refactor august to use self.async_on_remove (#42333)

This commit is contained in:
J. Nick Koston 2020-10-25 07:27:51 -05:00 committed by GitHub
parent 6822190772
commit 3f18cdc75e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View file

@ -46,18 +46,13 @@ class AugustEntityMixin(Entity):
async def async_added_to_hass(self):
"""Subscribe to updates."""
self._data.async_subscribe_device_id(
self._device_id, self._update_from_data_and_write_state
self.async_on_remove(
self._data.async_subscribe_device_id(
self._device_id, self._update_from_data_and_write_state
)
)
self._data.activity_stream.async_subscribe_device_id(
self._device_id, self._update_from_data_and_write_state
)
async def async_will_remove_from_hass(self):
"""Undo subscription."""
self._data.async_unsubscribe_device_id(
self._device_id, self._update_from_data_and_write_state
)
self._data.activity_stream.async_unsubscribe_device_id(
self._device_id, self._update_from_data_and_write_state
self.async_on_remove(
self._data.activity_stream.async_subscribe_device_id(
self._device_id, self._update_from_data_and_write_state
)
)

View file

@ -18,13 +18,21 @@ class AugustSubscriberMixin:
@callback
def async_subscribe_device_id(self, device_id, update_callback):
"""Add an callback subscriber."""
"""Add an callback subscriber.
Returns a callable that can be used to unsubscribe.
"""
if not self._subscriptions:
self._unsub_interval = async_track_time_interval(
self._hass, self._async_refresh, self._update_interval
)
self._subscriptions.setdefault(device_id, []).append(update_callback)
def _unsubscribe():
self.async_unsubscribe_device_id(device_id, update_callback)
return _unsubscribe
@callback
def async_unsubscribe_device_id(self, device_id, update_callback):
"""Remove a callback subscriber."""