Files
home-assistant-core/homeassistant/components/august/subscriber.py
T
J. Nick Koston 223c01d842 Coordinate all august detail and activity updates (#32249)
* Removing polling from august

* Now using subscribers to the detail and activity

* Fix hash to list keys

* continue to the next house if one fails

* Add async_signal_device_id_update

* Fix double initial update

* Handle self.hass not being available until after async_added_to_hass

* Remove not needed await

* Fix regression with device name
2020-02-27 19:44:22 -08:00

54 lines
1.8 KiB
Python

"""Base class for August entity."""
from homeassistant.core import callback
from homeassistant.helpers.event import async_track_time_interval
class AugustSubscriberMixin:
"""Base implementation for a subscriber."""
def __init__(self, hass, update_interval):
"""Initialize an subscriber."""
super().__init__()
self._hass = hass
self._update_interval = update_interval
self._subscriptions = {}
self._unsub_interval = None
@callback
def async_subscribe_device_id(self, device_id, update_callback):
"""Add an callback subscriber."""
if not self._subscriptions:
self._unsub_interval = async_track_time_interval(
self._hass, self._refresh, self._update_interval
)
self._subscriptions.setdefault(device_id, []).append(update_callback)
@callback
def async_unsubscribe_device_id(self, device_id, update_callback):
"""Remove a callback subscriber."""
self._subscriptions[device_id].remove(update_callback)
if not self._subscriptions[device_id]:
del self._subscriptions[device_id]
if not self._subscriptions:
self._unsub_interval()
self._unsub_interval = None
@callback
def async_signal_device_id_update(self, device_id):
"""Call the callbacks for a device_id."""
if not self._subscriptions.get(device_id):
return
for update_callback in self._subscriptions[device_id]:
update_callback()
def signal_device_id_update(self, device_id):
"""Call the callbacks for a device_id."""
if not self._subscriptions.get(device_id):
return
for update_callback in self._subscriptions[device_id]:
self._hass.loop.call_soon_threadsafe(update_callback)