2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Axis devices."""
|
2017-05-12 15:51:54 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant import config_entries
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.const import (
|
2019-03-24 15:16:50 +00:00
|
|
|
CONF_DEVICE, CONF_HOST, CONF_NAME, CONF_TRIGGER_TIME,
|
2018-01-21 06:35:38 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP)
|
2017-05-12 15:51:54 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
from .config_flow import configured_devices, DEVICE_SCHEMA
|
|
|
|
from .const import CONF_CAMERA, CONF_EVENTS, DEFAULT_TRIGGER_TIME, DOMAIN
|
|
|
|
from .device import AxisNetworkDevice, get_device
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-29 14:20:12 +00:00
|
|
|
REQUIREMENTS = ['axis==19']
|
2017-05-12 15:51:54 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
2019-01-22 00:36:04 +00:00
|
|
|
DOMAIN: cv.schema_with_slug_keys(DEVICE_SCHEMA),
|
2017-05-12 15:51:54 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up for Axis devices."""
|
2019-03-24 15:16:50 +00:00
|
|
|
if DOMAIN in config:
|
2018-10-29 05:52:30 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
for device_name, device_config in config[DOMAIN].items():
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
if CONF_NAME not in device_config:
|
|
|
|
device_config[CONF_NAME] = device_name
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
if device_config[CONF_HOST] not in configured_devices(hass):
|
|
|
|
hass.async_create_task(hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={'source': config_entries.SOURCE_IMPORT},
|
|
|
|
data=device_config
|
|
|
|
))
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
return True
|
2017-06-24 07:14:57 +00:00
|
|
|
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up the Axis component."""
|
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
hass.data[DOMAIN] = {}
|
2017-06-24 07:14:57 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
if not config_entry.options:
|
|
|
|
await async_populate_options(hass, config_entry)
|
2017-06-24 07:14:57 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
device = AxisNetworkDevice(hass, config_entry)
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
if not await device.async_setup():
|
|
|
|
return False
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
hass.data[DOMAIN][device.serial] = device
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-27 17:25:01 +00:00
|
|
|
await device.async_update_device_registry()
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.shutdown)
|
2017-10-25 07:04:30 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
return True
|
2018-10-29 05:52:30 +00:00
|
|
|
|
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
async def async_populate_options(hass, config_entry):
|
|
|
|
"""Populate default options for device."""
|
|
|
|
from axis.vapix import VAPIX_IMAGE_FORMAT
|
2018-10-29 05:52:30 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
device = await get_device(hass, config_entry.data[CONF_DEVICE])
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
supported_formats = device.vapix.get_param(VAPIX_IMAGE_FORMAT)
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
camera = bool(supported_formats)
|
2018-10-29 05:52:30 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
options = {
|
|
|
|
CONF_CAMERA: camera,
|
|
|
|
CONF_EVENTS: True,
|
|
|
|
CONF_TRIGGER_TIME: DEFAULT_TRIGGER_TIME
|
|
|
|
}
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
hass.config_entries.async_update_entry(config_entry, options=options)
|