Axis component reflect device availability (#22401)

This commit is contained in:
Robert Svensson authored and GitHub committed 2019-03-29 15:20:12 +01:00
1 parent f4625fd561
commit 5f6037d563
10 files changed
+69 -28

No files matched your search

+26 -6
View File
@@ -12,6 +12,7 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_send
from .const import CONF_CAMERA, CONF_EVENTS, CONF_MODEL, DOMAIN, LOGGER
from .errors import AuthenticationRequired, CannotConnect
@@ -72,8 +73,7 @@ class AxisNetworkDevice:
try:
self.api = await get_device(
hass, self.config_entry.data[CONF_DEVICE],
event_types='on', signal_callback=self.async_signal_callback)
hass, self.config_entry.data[CONF_DEVICE])
except CannotConnect:
raise ConfigEntryNotReady
@@ -95,17 +95,38 @@ class AxisNetworkDevice:
self.hass.async_create_task(
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, 'binary_sensor'))
self.api.stream.connection_status_callback = \
self.async_connection_status_callback
self.api.enable_events(event_callback=self.async_event_callback)
self.api.start()
return True
@property
def event_reachable(self):
"""Device specific event to signal a change in connection status."""
return 'axis_reachable_{}'.format(self.serial)
@callback
def async_connection_status_callback(self, status):
"""Handle signals of gateway connection status.
This is called on every RTSP keep-alive message.
Only signal state change if state change is true.
"""
from axis.streammanager import SIGNAL_PLAYING
if self.available != (status == SIGNAL_PLAYING):
self.available = not self.available
async_dispatcher_send(self.hass, self.event_reachable, True)
@property
def event_new_sensor(self):
"""Device specific event to signal new sensor available."""
return 'axis_add_sensor_{}'.format(self.serial)
@callback
def async_signal_callback(self, action, event):
def async_event_callback(self, action, event):
"""Call to configure events when initialized on event stream."""
if action == 'add':
async_dispatcher_send(self.hass, self.event_new_sensor, event)
@@ -116,7 +137,7 @@ class AxisNetworkDevice:
self.api.stop()
async def get_device(hass, config, event_types=None, signal_callback=None):
async def get_device(hass, config):
"""Create a Axis device."""
import axis
@@ -124,8 +145,7 @@ async def get_device(hass, config, event_types=None, signal_callback=None):
loop=hass.loop, host=config[CONF_HOST],
username=config[CONF_USERNAME],
password=config[CONF_PASSWORD],
port=config[CONF_PORT], web_proto='http',
event_types=event_types, signal=signal_callback)
port=config[CONF_PORT], web_proto='http')
try:
with async_timeout.timeout(15):