Axis component reflect device availability (#22401)

This commit is contained in:
Robert Svensson 2019-03-29 15:20:12 +01:00 committed by GitHub
parent f4625fd561
commit 5f6037d563
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 69 additions and 28 deletions

View file

@ -35,23 +35,29 @@ class AxisBinarySensor(BinarySensorDevice):
"""Initialize the Axis binary sensor."""
self.event = event
self.device = device
self.delay = device.config_entry.options[CONF_TRIGGER_TIME]
self.remove_timer = None
self.unsub_dispatcher = None
async def async_added_to_hass(self):
"""Subscribe sensors events."""
self.event.register_callback(self.update_callback)
self.unsub_dispatcher = async_dispatcher_connect(
self.hass, self.device.event_reachable, self.update_callback)
def update_callback(self):
"""Update the sensor's state, if needed."""
@callback
def update_callback(self, no_delay=False):
"""Update the sensor's state, if needed.
Parameter no_delay is True when device_event_reachable is sent.
"""
delay = self.device.config_entry.options[CONF_TRIGGER_TIME]
if self.remove_timer is not None:
self.remove_timer()
self.remove_timer = None
if delay == 0 or self.is_on:
self.schedule_update_ha_state()
if self.is_on or delay == 0 or no_delay:
self.async_schedule_update_ha_state()
return
@callback
@ -87,6 +93,10 @@ class AxisBinarySensor(BinarySensorDevice):
return '{}-{}-{}'.format(
self.device.serial, self.event.topic, self.event.id)
def available(self):
"""Return True if device is available."""
return self.device.available
@property
def should_poll(self):
"""No polling needed."""