2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Axis devices."""
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2020-01-30 21:20:30 +00:00
|
|
|
import logging
|
|
|
|
|
2020-05-14 08:49:27 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE, EVENT_HOMEASSISTANT_STOP
|
|
|
|
|
|
|
|
from .const import DOMAIN as AXIS_DOMAIN
|
|
|
|
from .device import AxisNetworkDevice
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2020-01-30 21:20:30 +00:00
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2019-03-24 15:16:50 +00:00
|
|
|
async def async_setup(hass, config):
|
2020-01-03 20:25:31 +00:00
|
|
|
"""Old way to set up Axis devices."""
|
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."""
|
2020-05-14 08:49:27 +00:00
|
|
|
hass.data.setdefault(AXIS_DOMAIN, {})
|
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
|
|
|
|
2020-01-04 07:58:18 +00: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 18:00:15 +00:00
|
|
|
config_entry, unique_id=device.api.vapix.serial_number
|
2020-01-04 07:58:18 +00:00
|
|
|
)
|
|
|
|
|
2020-05-14 08:49:27 +00:00
|
|
|
hass.data[AXIS_DOMAIN][config_entry.unique_id] = 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-04-15 22:06:45 +00:00
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload Axis device config entry."""
|
2020-05-14 08:49:27 +00:00
|
|
|
device = hass.data[AXIS_DOMAIN].pop(config_entry.unique_id)
|
2019-04-15 22:06:45 +00:00
|
|
|
return await device.async_reset()
|
|
|
|
|
|
|
|
|
2020-01-30 21:20:30 +00:00
|
|
|
async def async_migrate_entry(hass, config_entry):
|
|
|
|
"""Migrate old entry."""
|
|
|
|
LOGGER.debug("Migrating from version %s", config_entry.version)
|
|
|
|
|
|
|
|
# Flatten configuration but keep old data if user rollbacks HASS
|
|
|
|
if config_entry.version == 1:
|
|
|
|
config_entry.data = {**config_entry.data, **config_entry.data[CONF_DEVICE]}
|
|
|
|
|
|
|
|
config_entry.version = 2
|
|
|
|
|
|
|
|
LOGGER.info("Migration to version %s successful", config_entry.version)
|
|
|
|
|
|
|
|
return True
|