2019-02-13 21:21:14 +01:00
|
|
|
"""Support for Axis devices."""
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2020-01-30 22:20:30 +01:00
|
|
|
import logging
|
|
|
|
|
2020-05-14 10:49:27 +02:00
|
|
|
from homeassistant.const import CONF_DEVICE, EVENT_HOMEASSISTANT_STOP
|
|
|
|
|
|
|
|
from .const import DOMAIN as AXIS_DOMAIN
|
|
|
|
from .device import AxisNetworkDevice
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2020-10-12 16:59:05 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-01-30 22:20:30 +01:00
|
|
|
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
async def async_setup(hass, config):
|
2020-01-03 21:25:31 +01:00
|
|
|
"""Old way to set up Axis devices."""
|
2019-03-24 16:16:50 +01:00
|
|
|
return True
|
2017-06-24 09:14:57 +02:00
|
|
|
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up the Axis component."""
|
2020-05-14 10:49:27 +02:00
|
|
|
hass.data.setdefault(AXIS_DOMAIN, {})
|
2017-06-24 09:14:57 +02:00
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
device = AxisNetworkDevice(hass, config_entry)
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
if not await device.async_setup():
|
|
|
|
return False
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2020-01-04 08:58:18 +01:00
|
|
|
# 0.104 introduced config entry unique id, this makes upgrading possible
|
|
|
|
if config_entry.unique_id is None:
|
|
|
|
hass.config_entries.async_update_entry(
|
2020-05-31 20:00:15 +02:00
|
|
|
config_entry, unique_id=device.api.vapix.serial_number
|
2020-01-04 08:58:18 +01:00
|
|
|
)
|
|
|
|
|
2020-05-14 10:49:27 +02:00
|
|
|
hass.data[AXIS_DOMAIN][config_entry.unique_id] = device
|
2017-05-12 17:51:54 +02:00
|
|
|
|
2019-03-27 18:25:01 +01:00
|
|
|
await device.async_update_device_registry()
|
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.shutdown)
|
2017-10-25 09:04:30 +02:00
|
|
|
|
2019-03-24 16:16:50 +01:00
|
|
|
return True
|
2018-10-29 06:52:30 +01:00
|
|
|
|
|
|
|
|
2019-04-16 00:06:45 +02:00
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload Axis device config entry."""
|
2020-05-14 10:49:27 +02:00
|
|
|
device = hass.data[AXIS_DOMAIN].pop(config_entry.unique_id)
|
2019-04-16 00:06:45 +02:00
|
|
|
return await device.async_reset()
|
|
|
|
|
|
|
|
|
2020-01-30 22:20:30 +01:00
|
|
|
async def async_migrate_entry(hass, config_entry):
|
|
|
|
"""Migrate old entry."""
|
2020-10-12 16:59:05 +02:00
|
|
|
_LOGGER.debug("Migrating from version %s", config_entry.version)
|
2020-01-30 22:20:30 +01:00
|
|
|
|
2020-10-16 18:54:48 +02:00
|
|
|
# Flatten configuration but keep old data if user rollbacks HASS prior to 0.106
|
2020-01-30 22:20:30 +01:00
|
|
|
if config_entry.version == 1:
|
|
|
|
config_entry.data = {**config_entry.data, **config_entry.data[CONF_DEVICE]}
|
|
|
|
|
|
|
|
config_entry.version = 2
|
|
|
|
|
2020-10-12 16:59:05 +02:00
|
|
|
_LOGGER.info("Migration to version %s successful", config_entry.version)
|
2020-01-30 22:20:30 +01:00
|
|
|
|
|
|
|
return True
|