This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+45 -36
View File
@@ -4,8 +4,14 @@ import asyncio
import async_timeout
from homeassistant.const import (
CONF_DEVICE, CONF_HOST, CONF_MAC, CONF_NAME, CONF_PASSWORD, CONF_PORT,
CONF_USERNAME)
CONF_DEVICE,
CONF_HOST,
CONF_MAC,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
@@ -53,30 +59,27 @@ class AxisNetworkDevice:
async def async_update_device_registry(self):
"""Update device registry."""
device_registry = await \
self.hass.helpers.device_registry.async_get_registry()
device_registry = await self.hass.helpers.device_registry.async_get_registry()
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, self.serial)},
identifiers={(DOMAIN, self.serial)},
manufacturer='Axis Communications AB',
manufacturer="Axis Communications AB",
model="{} {}".format(self.model, self.product_type),
name=self.name,
sw_version=self.fw_version
sw_version=self.fw_version,
)
async def async_setup(self):
"""Set up the device."""
try:
self.api = await get_device(
self.hass, self.config_entry.data[CONF_DEVICE])
self.api = await get_device(self.hass, self.config_entry.data[CONF_DEVICE])
except CannotConnect:
raise ConfigEntryNotReady
except Exception: # pylint: disable=broad-except
LOGGER.error(
'Unknown error connecting with Axis device on %s', self.host)
LOGGER.error("Unknown error connecting with Axis device on %s", self.host)
return False
self.fw_version = self.api.vapix.params.firmware_version
@@ -86,18 +89,22 @@ class AxisNetworkDevice:
self.hass.async_create_task(
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, 'camera'))
self.config_entry, "camera"
)
)
if self.config_entry.options[CONF_EVENTS]:
self.api.stream.connection_status_callback = \
self.api.stream.connection_status_callback = (
self.async_connection_status_callback
)
self.api.enable_events(event_callback=self.async_event_callback)
platform_tasks = [
self.hass.config_entries.async_forward_entry_setup(
self.config_entry, platform)
for platform in ['binary_sensor', 'switch']
self.config_entry, platform
)
for platform in ["binary_sensor", "switch"]
]
self.hass.async_create_task(self.start(platform_tasks))
@@ -108,7 +115,7 @@ class AxisNetworkDevice:
@property
def event_new_address(self):
"""Device specific event to signal new device address."""
return 'axis_new_address_{}'.format(self.serial)
return "axis_new_address_{}".format(self.serial)
@staticmethod
async def async_new_address_callback(hass, entry):
@@ -124,7 +131,7 @@ class AxisNetworkDevice:
@property
def event_reachable(self):
"""Device specific event to signal a change in connection status."""
return 'axis_reachable_{}'.format(self.serial)
return "axis_reachable_{}".format(self.serial)
@callback
def async_connection_status_callback(self, status):
@@ -134,6 +141,7 @@ class AxisNetworkDevice:
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)
@@ -141,12 +149,12 @@ class AxisNetworkDevice:
@property
def event_new_sensor(self):
"""Device specific event to signal new sensor available."""
return 'axis_add_sensor_{}'.format(self.serial)
return "axis_add_sensor_{}".format(self.serial)
@callback
def async_event_callback(self, action, event_id):
"""Call to configure events when initialized on event stream."""
if action == 'add':
if action == "add":
async_dispatcher_send(self.hass, self.event_new_sensor, event_id)
async def start(self, platform_tasks):
@@ -166,14 +174,17 @@ class AxisNetworkDevice:
if self.config_entry.options[CONF_CAMERA]:
platform_tasks.append(
self.hass.config_entries.async_forward_entry_unload(
self.config_entry, 'camera'))
self.config_entry, "camera"
)
)
if self.config_entry.options[CONF_EVENTS]:
self.api.stop()
platform_tasks += [
self.hass.config_entries.async_forward_entry_unload(
self.config_entry, platform)
for platform in ['binary_sensor', 'switch']
self.config_entry, platform
)
for platform in ["binary_sensor", "switch"]
]
await asyncio.gather(*platform_tasks)
@@ -190,10 +201,13 @@ async def get_device(hass, config):
import axis
device = axis.AxisDevice(
loop=hass.loop, host=config[CONF_HOST],
loop=hass.loop,
host=config[CONF_HOST],
username=config[CONF_USERNAME],
password=config[CONF_PASSWORD],
port=config[CONF_PORT], web_proto='http')
port=config[CONF_PORT],
web_proto="http",
)
device.vapix.initialize_params(preload_data=False)
device.vapix.initialize_ports()
@@ -202,28 +216,23 @@ async def get_device(hass, config):
with async_timeout.timeout(15):
await asyncio.gather(
hass.async_add_executor_job(
device.vapix.params.update_brand),
hass.async_add_executor_job(
device.vapix.params.update_properties),
hass.async_add_executor_job(
device.vapix.ports.update)
hass.async_add_executor_job(device.vapix.params.update_brand),
hass.async_add_executor_job(device.vapix.params.update_properties),
hass.async_add_executor_job(device.vapix.ports.update),
)
return device
except axis.Unauthorized:
LOGGER.warning("Connected to device at %s but not registered.",
config[CONF_HOST])
LOGGER.warning(
"Connected to device at %s but not registered.", config[CONF_HOST]
)
raise AuthenticationRequired
except (asyncio.TimeoutError, axis.RequestError):
LOGGER.error("Error connecting to the Axis device at %s",
config[CONF_HOST])
LOGGER.error("Error connecting to the Axis device at %s", config[CONF_HOST])
raise CannotConnect
except axis.AxisException:
LOGGER.exception('Unknown Axis communication error occurred')
LOGGER.exception("Unknown Axis communication error occurred")
raise AuthenticationRequired