Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -4,17 +4,21 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE, CONF_MAC, CONF_NAME, CONF_TRIGGER_TIME,
|
||||
EVENT_HOMEASSISTANT_STOP)
|
||||
CONF_DEVICE,
|
||||
CONF_MAC,
|
||||
CONF_NAME,
|
||||
CONF_TRIGGER_TIME,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .config_flow import DEVICE_SCHEMA
|
||||
from .const import CONF_CAMERA, CONF_EVENTS, DEFAULT_TRIGGER_TIME, DOMAIN
|
||||
from .device import AxisNetworkDevice, get_device
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: cv.schema_with_slug_keys(DEVICE_SCHEMA),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{DOMAIN: cv.schema_with_slug_keys(DEVICE_SCHEMA)}, extra=vol.ALLOW_EXTRA
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
@@ -26,10 +30,13 @@ async def async_setup(hass, config):
|
||||
if CONF_NAME not in device_config:
|
||||
device_config[CONF_NAME] = device_name
|
||||
|
||||
hass.async_create_task(hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
|
||||
data=device_config
|
||||
))
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_IMPORT},
|
||||
data=device_config,
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
@@ -72,7 +79,7 @@ async def async_populate_options(hass, config_entry):
|
||||
options = {
|
||||
CONF_CAMERA: camera,
|
||||
CONF_EVENTS: True,
|
||||
CONF_TRIGGER_TIME: DEFAULT_TRIGGER_TIME
|
||||
CONF_TRIGGER_TIME: DEFAULT_TRIGGER_TIME,
|
||||
}
|
||||
|
||||
hass.config_entries.async_update_entry(config_entry, options=options)
|
||||
@@ -17,8 +17,11 @@ class AxisEntityBase(Entity):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe device events."""
|
||||
self.unsub_dispatcher.append(async_dispatcher_connect(
|
||||
self.hass, self.device.event_reachable, self.update_callback))
|
||||
self.unsub_dispatcher.append(
|
||||
async_dispatcher_connect(
|
||||
self.hass, self.device.event_reachable, self.update_callback
|
||||
)
|
||||
)
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Unsubscribe device events when removed."""
|
||||
@@ -33,9 +36,7 @@ class AxisEntityBase(Entity):
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return a device description for device registry."""
|
||||
return {
|
||||
'identifiers': {(AXIS_DOMAIN, self.device.serial)}
|
||||
}
|
||||
return {"identifiers": {(AXIS_DOMAIN, self.device.serial)}}
|
||||
|
||||
@callback
|
||||
def update_callback(self, no_delay=None):
|
||||
@@ -71,8 +72,7 @@ class AxisEventBase(AxisEntityBase):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the event."""
|
||||
return '{} {} {}'.format(
|
||||
self.device.name, self.event.TYPE, self.event.id)
|
||||
return "{} {} {}".format(self.device.name, self.event.TYPE, self.event.id)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
@@ -82,5 +82,4 @@ class AxisEventBase(AxisEntityBase):
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique identifier for this device."""
|
||||
return '{}-{}-{}'.format(
|
||||
self.device.serial, self.event.topic, self.event.id)
|
||||
return "{}-{}-{}".format(self.device.serial, self.event.topic, self.event.id)
|
||||
@@ -28,8 +28,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
if event.CLASS != CLASS_OUTPUT:
|
||||
async_add_entities([AxisBinarySensor(event, device)], True)
|
||||
|
||||
device.listeners.append(async_dispatcher_connect(
|
||||
hass, device.event_new_sensor, async_add_sensor))
|
||||
device.listeners.append(
|
||||
async_dispatcher_connect(hass, device.event_new_sensor, async_add_sensor)
|
||||
)
|
||||
|
||||
|
||||
class AxisBinarySensor(AxisEventBase, BinarySensorDevice):
|
||||
@@ -63,8 +64,8 @@ class AxisBinarySensor(AxisEventBase, BinarySensorDevice):
|
||||
self.remove_timer = None
|
||||
|
||||
self.remove_timer = async_track_point_in_utc_time(
|
||||
self.hass, _delay_update,
|
||||
utcnow() + timedelta(seconds=delay))
|
||||
self.hass, _delay_update, utcnow() + timedelta(seconds=delay)
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
@@ -74,10 +75,13 @@ class AxisBinarySensor(AxisEventBase, BinarySensorDevice):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the event."""
|
||||
if self.event.CLASS == CLASS_INPUT and self.event.id and \
|
||||
self.device.api.vapix.ports[self.event.id].name:
|
||||
return '{} {}'.format(
|
||||
self.device.name,
|
||||
self.device.api.vapix.ports[self.event.id].name)
|
||||
if (
|
||||
self.event.CLASS == CLASS_INPUT
|
||||
and self.event.id
|
||||
and self.device.api.vapix.ports[self.event.id].name
|
||||
):
|
||||
return "{} {}".format(
|
||||
self.device.name, self.device.api.vapix.ports[self.event.id].name
|
||||
)
|
||||
|
||||
return super().name
|
||||
@@ -2,18 +2,30 @@
|
||||
|
||||
from homeassistant.components.camera import SUPPORT_STREAM
|
||||
from homeassistant.components.mjpeg.camera import (
|
||||
CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, MjpegCamera, filter_urllib3_logging)
|
||||
CONF_MJPEG_URL,
|
||||
CONF_STILL_IMAGE_URL,
|
||||
MjpegCamera,
|
||||
filter_urllib3_logging,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_AUTHENTICATION, CONF_DEVICE, CONF_HOST, CONF_MAC, CONF_NAME,
|
||||
CONF_PASSWORD, CONF_PORT, CONF_USERNAME, HTTP_DIGEST_AUTHENTICATION)
|
||||
CONF_AUTHENTICATION,
|
||||
CONF_DEVICE,
|
||||
CONF_HOST,
|
||||
CONF_MAC,
|
||||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_PORT,
|
||||
CONF_USERNAME,
|
||||
HTTP_DIGEST_AUTHENTICATION,
|
||||
)
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from .axis_base import AxisEntityBase
|
||||
from .const import DOMAIN as AXIS_DOMAIN
|
||||
|
||||
AXIS_IMAGE = 'http://{}:{}/axis-cgi/jpg/image.cgi'
|
||||
AXIS_VIDEO = 'http://{}:{}/axis-cgi/mjpg/video.cgi'
|
||||
AXIS_STREAM = 'rtsp://{}:{}@{}/axis-media/media.amp?videocodec=h264'
|
||||
AXIS_IMAGE = "http://{}:{}/axis-cgi/jpg/image.cgi"
|
||||
AXIS_VIDEO = "http://{}:{}/axis-cgi/mjpg/video.cgi"
|
||||
AXIS_STREAM = "rtsp://{}:{}@{}/axis-media/media.amp?videocodec=h264"
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
@@ -29,10 +41,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
CONF_PASSWORD: config_entry.data[CONF_DEVICE][CONF_PASSWORD],
|
||||
CONF_MJPEG_URL: AXIS_VIDEO.format(
|
||||
config_entry.data[CONF_DEVICE][CONF_HOST],
|
||||
config_entry.data[CONF_DEVICE][CONF_PORT]),
|
||||
config_entry.data[CONF_DEVICE][CONF_PORT],
|
||||
),
|
||||
CONF_STILL_IMAGE_URL: AXIS_IMAGE.format(
|
||||
config_entry.data[CONF_DEVICE][CONF_HOST],
|
||||
config_entry.data[CONF_DEVICE][CONF_PORT]),
|
||||
config_entry.data[CONF_DEVICE][CONF_PORT],
|
||||
),
|
||||
CONF_AUTHENTICATION: HTTP_DIGEST_AUTHENTICATION,
|
||||
}
|
||||
async_add_entities([AxisCamera(config, device)])
|
||||
@@ -48,8 +62,11 @@ class AxisCamera(AxisEntityBase, MjpegCamera):
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe camera events."""
|
||||
self.unsub_dispatcher.append(async_dispatcher_connect(
|
||||
self.hass, self.device.event_new_address, self._new_address))
|
||||
self.unsub_dispatcher.append(
|
||||
async_dispatcher_connect(
|
||||
self.hass, self.device.event_new_address, self._new_address
|
||||
)
|
||||
)
|
||||
|
||||
await super().async_added_to_hass()
|
||||
|
||||
@@ -63,7 +80,8 @@ class AxisCamera(AxisEntityBase, MjpegCamera):
|
||||
return AXIS_STREAM.format(
|
||||
self.device.config_entry.data[CONF_DEVICE][CONF_USERNAME],
|
||||
self.device.config_entry.data[CONF_DEVICE][CONF_PASSWORD],
|
||||
self.device.host)
|
||||
self.device.host,
|
||||
)
|
||||
|
||||
def _new_address(self):
|
||||
"""Set new device address for video stream."""
|
||||
@@ -74,4 +92,4 @@ class AxisCamera(AxisEntityBase, MjpegCamera):
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique identifier for this device."""
|
||||
return '{}-camera'.format(self.device.serial)
|
||||
return "{}-camera".format(self.device.serial)
|
||||
@@ -4,8 +4,14 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
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.helpers import config_validation as cv
|
||||
from homeassistant.util.json import load_json
|
||||
@@ -14,36 +20,40 @@ from .const import CONF_MODEL, DOMAIN
|
||||
from .device import get_device
|
||||
from .errors import AlreadyConfigured, AuthenticationRequired, CannotConnect
|
||||
|
||||
AXIS_OUI = {'00408C', 'ACCC8E', 'B8A44F'}
|
||||
AXIS_OUI = {"00408C", "ACCC8E", "B8A44F"}
|
||||
|
||||
CONFIG_FILE = 'axis.conf'
|
||||
CONFIG_FILE = "axis.conf"
|
||||
|
||||
EVENT_TYPES = ['motion', 'vmd3', 'pir', 'sound',
|
||||
'daynight', 'tampering', 'input']
|
||||
EVENT_TYPES = ["motion", "vmd3", "pir", "sound", "daynight", "tampering", "input"]
|
||||
|
||||
PLATFORMS = ['camera']
|
||||
PLATFORMS = ["camera"]
|
||||
|
||||
AXIS_INCLUDE = EVENT_TYPES + PLATFORMS
|
||||
|
||||
AXIS_DEFAULT_HOST = '192.168.0.90'
|
||||
AXIS_DEFAULT_USERNAME = 'root'
|
||||
AXIS_DEFAULT_PASSWORD = 'pass'
|
||||
AXIS_DEFAULT_HOST = "192.168.0.90"
|
||||
AXIS_DEFAULT_USERNAME = "root"
|
||||
AXIS_DEFAULT_PASSWORD = "pass"
|
||||
DEFAULT_PORT = 80
|
||||
|
||||
DEVICE_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_HOST, default=AXIS_DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_USERNAME, default=AXIS_DEFAULT_USERNAME): cv.string,
|
||||
vol.Optional(CONF_PASSWORD, default=AXIS_DEFAULT_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
DEVICE_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_HOST, default=AXIS_DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_USERNAME, default=AXIS_DEFAULT_USERNAME): cv.string,
|
||||
vol.Optional(CONF_PASSWORD, default=AXIS_DEFAULT_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def configured_devices(hass):
|
||||
"""Return a set of the configured devices."""
|
||||
return {entry.data[CONF_MAC]: entry for entry
|
||||
in hass.config_entries.async_entries(DOMAIN)}
|
||||
return {
|
||||
entry.data[CONF_MAC]: entry
|
||||
for entry in hass.config_entries.async_entries(DOMAIN)
|
||||
}
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(DOMAIN)
|
||||
@@ -76,7 +86,7 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
CONF_HOST: user_input[CONF_HOST],
|
||||
CONF_PORT: user_input[CONF_PORT],
|
||||
CONF_USERNAME: user_input[CONF_USERNAME],
|
||||
CONF_PASSWORD: user_input[CONF_PASSWORD]
|
||||
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
||||
}
|
||||
device = await get_device(self.hass, self.device_config)
|
||||
|
||||
@@ -90,26 +100,30 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
return await self._create_entry()
|
||||
|
||||
except AlreadyConfigured:
|
||||
errors['base'] = 'already_configured'
|
||||
errors["base"] = "already_configured"
|
||||
|
||||
except AuthenticationRequired:
|
||||
errors['base'] = 'faulty_credentials'
|
||||
errors["base"] = "faulty_credentials"
|
||||
|
||||
except CannotConnect:
|
||||
errors['base'] = 'device_unavailable'
|
||||
errors["base"] = "device_unavailable"
|
||||
|
||||
data = self.import_schema or self.discovery_schema or {
|
||||
vol.Required(CONF_HOST): str,
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Required(CONF_PORT, default=DEFAULT_PORT): int
|
||||
}
|
||||
data = (
|
||||
self.import_schema
|
||||
or self.discovery_schema
|
||||
or {
|
||||
vol.Required(CONF_HOST): str,
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
|
||||
}
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id='user',
|
||||
step_id="user",
|
||||
description_placeholders=self.device_config,
|
||||
data_schema=vol.Schema(data),
|
||||
errors=errors
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
async def _create_entry(self):
|
||||
@@ -119,8 +133,8 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
"""
|
||||
if self.name is None:
|
||||
same_model = [
|
||||
entry.data[CONF_NAME] for entry
|
||||
in self.hass.config_entries.async_entries(DOMAIN)
|
||||
entry.data[CONF_NAME]
|
||||
for entry in self.hass.config_entries.async_entries(DOMAIN)
|
||||
if entry.data[CONF_MODEL] == self.model
|
||||
]
|
||||
|
||||
@@ -138,10 +152,7 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
}
|
||||
|
||||
title = "{} - {}".format(self.model, self.serial_number)
|
||||
return self.async_create_entry(
|
||||
title=title,
|
||||
data=data
|
||||
)
|
||||
return self.async_create_entry(title=title, data=data)
|
||||
|
||||
async def _update_entry(self, entry, host):
|
||||
"""Update existing entry if it is the same device."""
|
||||
@@ -153,38 +164,40 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
This flow is triggered by the discovery component.
|
||||
"""
|
||||
serialnumber = discovery_info['properties']['macaddress']
|
||||
serialnumber = discovery_info["properties"]["macaddress"]
|
||||
|
||||
if serialnumber[:6] not in AXIS_OUI:
|
||||
return self.async_abort(reason='not_axis_device')
|
||||
return self.async_abort(reason="not_axis_device")
|
||||
|
||||
if discovery_info[CONF_HOST].startswith('169.254'):
|
||||
return self.async_abort(reason='link_local_address')
|
||||
if discovery_info[CONF_HOST].startswith("169.254"):
|
||||
return self.async_abort(reason="link_local_address")
|
||||
|
||||
# pylint: disable=unsupported-assignment-operation
|
||||
self.context['macaddress'] = serialnumber
|
||||
self.context["macaddress"] = serialnumber
|
||||
|
||||
if any(serialnumber == flow['context']['macaddress']
|
||||
for flow in self._async_in_progress()):
|
||||
return self.async_abort(reason='already_in_progress')
|
||||
if any(
|
||||
serialnumber == flow["context"]["macaddress"]
|
||||
for flow in self._async_in_progress()
|
||||
):
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
|
||||
device_entries = configured_devices(self.hass)
|
||||
|
||||
if serialnumber in device_entries:
|
||||
entry = device_entries[serialnumber]
|
||||
await self._update_entry(entry, discovery_info[CONF_HOST])
|
||||
return self.async_abort(reason='already_configured')
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
config_file = await self.hass.async_add_executor_job(
|
||||
load_json, self.hass.config.path(CONFIG_FILE))
|
||||
load_json, self.hass.config.path(CONFIG_FILE)
|
||||
)
|
||||
|
||||
if serialnumber not in config_file:
|
||||
self.discovery_schema = {
|
||||
vol.Required(
|
||||
CONF_HOST, default=discovery_info[CONF_HOST]): str,
|
||||
vol.Required(CONF_HOST, default=discovery_info[CONF_HOST]): str,
|
||||
vol.Required(CONF_USERNAME): str,
|
||||
vol.Required(CONF_PASSWORD): str,
|
||||
vol.Required(CONF_PORT, default=discovery_info[CONF_PORT]): int
|
||||
vol.Required(CONF_PORT, default=discovery_info[CONF_PORT]): int,
|
||||
}
|
||||
return await self.async_step_user()
|
||||
|
||||
@@ -193,10 +206,10 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
device_config[CONF_HOST] = discovery_info[CONF_HOST]
|
||||
|
||||
if CONF_NAME not in device_config:
|
||||
device_config[CONF_NAME] = discovery_info['hostname']
|
||||
device_config[CONF_NAME] = discovery_info["hostname"]
|
||||
|
||||
except vol.Invalid:
|
||||
return self.async_abort(reason='bad_config_file')
|
||||
return self.async_abort(reason="bad_config_file")
|
||||
|
||||
return await self.async_step_import(device_config)
|
||||
|
||||
@@ -213,10 +226,8 @@ class AxisFlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
self.import_schema = {
|
||||
vol.Required(CONF_HOST, default=import_config[CONF_HOST]): str,
|
||||
vol.Required(
|
||||
CONF_USERNAME, default=import_config[CONF_USERNAME]): str,
|
||||
vol.Required(
|
||||
CONF_PASSWORD, default=import_config[CONF_PASSWORD]): str,
|
||||
vol.Required(CONF_PORT, default=import_config[CONF_PORT]): int
|
||||
vol.Required(CONF_USERNAME, default=import_config[CONF_USERNAME]): str,
|
||||
vol.Required(CONF_PASSWORD, default=import_config[CONF_PASSWORD]): str,
|
||||
vol.Required(CONF_PORT, default=import_config[CONF_PORT]): int,
|
||||
}
|
||||
return await self.async_step_user(user_input=import_config)
|
||||
@@ -3,10 +3,10 @@ import logging
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
|
||||
DOMAIN = 'axis'
|
||||
DOMAIN = "axis"
|
||||
|
||||
CONF_CAMERA = 'camera'
|
||||
CONF_EVENTS = 'events'
|
||||
CONF_MODEL = 'model'
|
||||
CONF_CAMERA = "camera"
|
||||
CONF_EVENTS = "events"
|
||||
CONF_MODEL = "model"
|
||||
|
||||
DEFAULT_TRIGGER_TIME = 0
|
||||
@@ -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
|
||||
@@ -24,8 +24,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
if event.CLASS == CLASS_OUTPUT:
|
||||
async_add_entities([AxisSwitch(event, device)], True)
|
||||
|
||||
device.listeners.append(async_dispatcher_connect(
|
||||
hass, device.event_new_sensor, async_add_switch))
|
||||
device.listeners.append(
|
||||
async_dispatcher_connect(hass, device.event_new_sensor, async_add_switch)
|
||||
)
|
||||
|
||||
|
||||
class AxisSwitch(AxisEventBase, SwitchDevice):
|
||||
@@ -38,22 +39,24 @@ class AxisSwitch(AxisEventBase, SwitchDevice):
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Turn on switch."""
|
||||
action = '/'
|
||||
action = "/"
|
||||
await self.hass.async_add_executor_job(
|
||||
self.device.api.vapix.ports[self.event.id].action, action)
|
||||
self.device.api.vapix.ports[self.event.id].action, action
|
||||
)
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Turn off switch."""
|
||||
action = '\\'
|
||||
action = "\\"
|
||||
await self.hass.async_add_executor_job(
|
||||
self.device.api.vapix.ports[self.event.id].action, action)
|
||||
self.device.api.vapix.ports[self.event.id].action, action
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the event."""
|
||||
if self.event.id and self.device.api.vapix.ports[self.event.id].name:
|
||||
return '{} {}'.format(
|
||||
self.device.name,
|
||||
self.device.api.vapix.ports[self.event.id].name)
|
||||
return "{} {}".format(
|
||||
self.device.name, self.device.api.vapix.ports[self.event.id].name
|
||||
)
|
||||
|
||||
return super().name
|
||||
Reference in new issue
Block a user