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
|
|
|
|
|
2021-01-16 00:01:14 +00:00
|
|
|
from homeassistant.const import CONF_DEVICE, CONF_MAC, EVENT_HOMEASSISTANT_STOP
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.device_registry import format_mac
|
|
|
|
from homeassistant.helpers.entity_registry import async_migrate_entries
|
2020-05-14 08:49:27 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN as AXIS_DOMAIN
|
|
|
|
from .device import AxisNetworkDevice
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2020-10-12 14:59:05 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-01-30 21:20:30 +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-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()
|
|
|
|
|
2021-04-20 18:53:05 +00:00
|
|
|
config_entry.async_on_unload(
|
2021-04-09 08:56:53 +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."""
|
2020-10-12 14:59:05 +00:00
|
|
|
_LOGGER.debug("Migrating from version %s", config_entry.version)
|
2020-01-30 21:20:30 +00:00
|
|
|
|
2020-10-16 16:54:48 +00:00
|
|
|
# Flatten configuration but keep old data if user rollbacks HASS prior to 0.106
|
2020-01-30 21:20:30 +00:00
|
|
|
if config_entry.version == 1:
|
2021-02-06 12:17:52 +00:00
|
|
|
unique_id = config_entry.data[CONF_MAC]
|
|
|
|
data = {**config_entry.data, **config_entry.data[CONF_DEVICE]}
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
config_entry, unique_id=unique_id, data=data
|
|
|
|
)
|
2020-01-30 21:20:30 +00:00
|
|
|
config_entry.version = 2
|
|
|
|
|
2021-01-16 00:01:14 +00:00
|
|
|
# Normalise MAC address of device which also affects entity unique IDs
|
|
|
|
if config_entry.version == 2:
|
|
|
|
old_unique_id = config_entry.unique_id
|
|
|
|
new_unique_id = format_mac(old_unique_id)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_unique_id(entity_entry):
|
|
|
|
"""Update unique ID of entity entry."""
|
|
|
|
return {
|
|
|
|
"new_unique_id": entity_entry.unique_id.replace(
|
|
|
|
old_unique_id, new_unique_id
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-06 12:17:52 +00:00
|
|
|
if old_unique_id != new_unique_id:
|
|
|
|
await async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
|
2021-01-16 00:01:14 +00:00
|
|
|
|
2021-02-06 12:17:52 +00:00
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
config_entry, unique_id=new_unique_id
|
|
|
|
)
|
2021-01-16 00:01:14 +00:00
|
|
|
|
2020-10-12 14:59:05 +00:00
|
|
|
_LOGGER.info("Migration to version %s successful", config_entry.version)
|
2020-01-30 21:20:30 +00:00
|
|
|
|
|
|
|
return True
|