2019-03-24 15:16:50 +00:00
|
|
|
"""Axis network device abstraction."""
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import async_timeout
|
|
|
|
|
2019-10-12 19:50:30 +00:00
|
|
|
import axis
|
|
|
|
from axis.streammanager import SIGNAL_PLAYING
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MAC,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
)
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-03-27 17:25:01 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
|
2019-03-27 17:25:01 +00:00
|
|
|
from .const import CONF_CAMERA, CONF_EVENTS, CONF_MODEL, DOMAIN, LOGGER
|
2019-03-29 14:20:12 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
|
|
|
|
|
|
|
|
|
|
|
class AxisNetworkDevice:
|
|
|
|
"""Manages a Axis device."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config_entry):
|
|
|
|
"""Initialize the device."""
|
|
|
|
self.hass = hass
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.available = True
|
|
|
|
|
|
|
|
self.api = None
|
|
|
|
self.fw_version = None
|
|
|
|
self.product_type = None
|
|
|
|
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""Return the host of this device."""
|
|
|
|
return self.config_entry.data[CONF_DEVICE][CONF_HOST]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model(self):
|
|
|
|
"""Return the model of this device."""
|
|
|
|
return self.config_entry.data[CONF_MODEL]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this device."""
|
|
|
|
return self.config_entry.data[CONF_NAME]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def serial(self):
|
|
|
|
"""Return the mac of this device."""
|
|
|
|
return self.config_entry.data[CONF_MAC]
|
|
|
|
|
2019-03-27 17:25:01 +00:00
|
|
|
async def async_update_device_registry(self):
|
|
|
|
"""Update device registry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
device_registry = await self.hass.helpers.device_registry.async_get_registry()
|
2019-03-27 17:25:01 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=self.config_entry.entry_id,
|
|
|
|
connections={(CONNECTION_NETWORK_MAC, self.serial)},
|
|
|
|
identifiers={(DOMAIN, self.serial)},
|
2019-07-31 19:25:30 +00:00
|
|
|
manufacturer="Axis Communications AB",
|
2019-09-03 14:11:36 +00:00
|
|
|
model=f"{self.model} {self.product_type}",
|
2019-03-27 17:25:01 +00:00
|
|
|
name=self.name,
|
2019-07-31 19:25:30 +00:00
|
|
|
sw_version=self.fw_version,
|
2019-03-27 17:25:01 +00:00
|
|
|
)
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
async def async_setup(self):
|
|
|
|
"""Set up the device."""
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.api = await get_device(self.hass, self.config_entry.data[CONF_DEVICE])
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
except CannotConnect:
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.error("Unknown error connecting with Axis device on %s", self.host)
|
2019-03-24 15:16:50 +00:00
|
|
|
return False
|
|
|
|
|
2019-04-16 08:46:29 +00:00
|
|
|
self.fw_version = self.api.vapix.params.firmware_version
|
|
|
|
self.product_type = self.api.vapix.params.prodtype
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
if self.config_entry.options[CONF_CAMERA]:
|
2019-05-20 05:45:31 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
self.hass.async_create_task(
|
|
|
|
self.hass.config_entries.async_forward_entry_setup(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, "camera"
|
|
|
|
)
|
|
|
|
)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
if self.config_entry.options[CONF_EVENTS]:
|
2019-03-29 14:20:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.api.stream.connection_status_callback = (
|
2019-03-29 14:20:12 +00:00
|
|
|
self.async_connection_status_callback
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-29 14:20:12 +00:00
|
|
|
self.api.enable_events(event_callback=self.async_event_callback)
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
platform_tasks = [
|
|
|
|
self.hass.config_entries.async_forward_entry_setup(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, platform
|
|
|
|
)
|
|
|
|
for platform in ["binary_sensor", "switch"]
|
2019-05-20 05:45:31 +00:00
|
|
|
]
|
|
|
|
self.hass.async_create_task(self.start(platform_tasks))
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-04-02 18:13:11 +00:00
|
|
|
self.config_entry.add_update_listener(self.async_new_address_callback)
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
return True
|
|
|
|
|
2019-04-02 18:13:11 +00:00
|
|
|
@property
|
|
|
|
def event_new_address(self):
|
|
|
|
"""Device specific event to signal new device address."""
|
2019-09-03 14:11:36 +00:00
|
|
|
return f"axis_new_address_{self.serial}"
|
2019-04-02 18:13:11 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def async_new_address_callback(hass, entry):
|
|
|
|
"""Handle signals of device getting new address.
|
|
|
|
|
|
|
|
This is a static method because a class method (bound method),
|
|
|
|
can not be used with weak references.
|
|
|
|
"""
|
|
|
|
device = hass.data[DOMAIN][entry.data[CONF_MAC]]
|
|
|
|
device.api.config.host = device.host
|
|
|
|
async_dispatcher_send(hass, device.event_new_address)
|
|
|
|
|
2019-03-29 14:20:12 +00:00
|
|
|
@property
|
|
|
|
def event_reachable(self):
|
|
|
|
"""Device specific event to signal a change in connection status."""
|
2019-09-03 14:11:36 +00:00
|
|
|
return f"axis_reachable_{self.serial}"
|
2019-03-29 14:20:12 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_connection_status_callback(self, status):
|
2019-04-02 18:13:11 +00:00
|
|
|
"""Handle signals of device connection status.
|
2019-03-29 14:20:12 +00:00
|
|
|
|
|
|
|
This is called on every RTSP keep-alive message.
|
|
|
|
Only signal state change if state change is true.
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-03-29 14:20:12 +00:00
|
|
|
if self.available != (status == SIGNAL_PLAYING):
|
|
|
|
self.available = not self.available
|
|
|
|
async_dispatcher_send(self.hass, self.event_reachable, True)
|
|
|
|
|
2019-03-28 02:54:27 +00:00
|
|
|
@property
|
|
|
|
def event_new_sensor(self):
|
|
|
|
"""Device specific event to signal new sensor available."""
|
2019-09-03 14:11:36 +00:00
|
|
|
return f"axis_add_sensor_{self.serial}"
|
2019-03-28 02:54:27 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
@callback
|
2019-04-17 13:21:42 +00:00
|
|
|
def async_event_callback(self, action, event_id):
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Call to configure events when initialized on event stream."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if action == "add":
|
2019-04-17 13:21:42 +00:00
|
|
|
async_dispatcher_send(self.hass, self.event_new_sensor, event_id)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-05-20 05:45:31 +00:00
|
|
|
async def start(self, platform_tasks):
|
|
|
|
"""Start the event stream when all platforms are loaded."""
|
|
|
|
await asyncio.gather(*platform_tasks)
|
2019-04-15 16:20:01 +00:00
|
|
|
self.api.start()
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
@callback
|
|
|
|
def shutdown(self, event):
|
|
|
|
"""Stop the event stream."""
|
|
|
|
self.api.stop()
|
|
|
|
|
2019-04-15 22:06:45 +00:00
|
|
|
async def async_reset(self):
|
|
|
|
"""Reset this device to default state."""
|
2019-05-20 05:45:31 +00:00
|
|
|
platform_tasks = []
|
2019-04-15 22:06:45 +00:00
|
|
|
|
|
|
|
if self.config_entry.options[CONF_CAMERA]:
|
2019-05-20 05:45:31 +00:00
|
|
|
platform_tasks.append(
|
|
|
|
self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, "camera"
|
|
|
|
)
|
|
|
|
)
|
2019-04-15 22:06:45 +00:00
|
|
|
|
|
|
|
if self.config_entry.options[CONF_EVENTS]:
|
2019-05-20 05:45:31 +00:00
|
|
|
self.api.stop()
|
|
|
|
platform_tasks += [
|
|
|
|
self.hass.config_entries.async_forward_entry_unload(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.config_entry, platform
|
|
|
|
)
|
|
|
|
for platform in ["binary_sensor", "switch"]
|
2019-05-20 05:45:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
await asyncio.gather(*platform_tasks)
|
2019-04-15 22:06:45 +00:00
|
|
|
|
|
|
|
for unsub_dispatcher in self.listeners:
|
|
|
|
unsub_dispatcher()
|
|
|
|
self.listeners = []
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-03-29 14:20:12 +00:00
|
|
|
async def get_device(hass, config):
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Create a Axis device."""
|
|
|
|
|
|
|
|
device = axis.AxisDevice(
|
2019-07-31 19:25:30 +00:00
|
|
|
loop=hass.loop,
|
|
|
|
host=config[CONF_HOST],
|
2019-03-24 15:16:50 +00:00
|
|
|
username=config[CONF_USERNAME],
|
|
|
|
password=config[CONF_PASSWORD],
|
2019-07-31 19:25:30 +00:00
|
|
|
port=config[CONF_PORT],
|
|
|
|
web_proto="http",
|
|
|
|
)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-04-16 08:46:29 +00:00
|
|
|
device.vapix.initialize_params(preload_data=False)
|
2019-05-20 05:45:31 +00:00
|
|
|
device.vapix.initialize_ports()
|
2019-04-16 08:46:29 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
try:
|
|
|
|
with async_timeout.timeout(15):
|
2019-05-20 05:45:31 +00:00
|
|
|
|
|
|
|
await asyncio.gather(
|
2019-07-31 19:25:30 +00:00
|
|
|
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),
|
2019-05-20 05:45:31 +00:00
|
|
|
)
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
return device
|
|
|
|
|
|
|
|
except axis.Unauthorized:
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.warning(
|
|
|
|
"Connected to device at %s but not registered.", config[CONF_HOST]
|
|
|
|
)
|
2019-03-24 15:16:50 +00:00
|
|
|
raise AuthenticationRequired
|
|
|
|
|
|
|
|
except (asyncio.TimeoutError, axis.RequestError):
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.error("Error connecting to the Axis device at %s", config[CONF_HOST])
|
2019-03-24 15:16:50 +00:00
|
|
|
raise CannotConnect
|
|
|
|
|
|
|
|
except axis.AxisException:
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER.exception("Unknown Axis communication error occurred")
|
2019-03-24 15:16:50 +00:00
|
|
|
raise AuthenticationRequired
|