2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Axis devices."""
|
2020-01-30 21:20:30 +00:00
|
|
|
import logging
|
|
|
|
|
2021-12-31 05:17:05 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-01-13 16:12:51 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-28 09:41:03 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2020-05-14 08:49:27 +00:00
|
|
|
|
2022-07-28 09:41:03 +00:00
|
|
|
from .const import DOMAIN as AXIS_DOMAIN, PLATFORMS
|
|
|
|
from .device import AxisNetworkDevice, get_axis_device
|
|
|
|
from .errors import AuthenticationRequired, CannotConnect
|
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
|
|
|
|
2021-12-31 05:17:05 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2022-07-28 09:41:03 +00:00
|
|
|
"""Set up the Axis integration."""
|
2020-05-14 08:49:27 +00:00
|
|
|
hass.data.setdefault(AXIS_DOMAIN, {})
|
2017-06-24 07:14:57 +00:00
|
|
|
|
2022-07-28 09:41:03 +00:00
|
|
|
try:
|
|
|
|
api = await get_axis_device(hass, config_entry.data)
|
|
|
|
except CannotConnect as err:
|
|
|
|
raise ConfigEntryNotReady from err
|
|
|
|
except AuthenticationRequired as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2017-05-12 15:51:54 +00:00
|
|
|
|
2022-07-30 09:04:31 +00:00
|
|
|
device = AxisNetworkDevice(hass, config_entry, api)
|
2023-01-13 21:21:40 +00:00
|
|
|
hass.data[AXIS_DOMAIN][config_entry.entry_id] = device
|
2019-03-27 17:25:01 +00:00
|
|
|
await device.async_update_device_registry()
|
2022-07-28 09:41:03 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
|
|
|
device.async_setup_events()
|
2019-03-27 17:25:01 +00:00
|
|
|
|
2022-07-28 09:41:03 +00:00
|
|
|
config_entry.add_update_listener(device.async_new_address_callback)
|
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
|
|
|
|
|
|
|
|
2021-12-31 05:17:05 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2019-04-15 22:06:45 +00:00
|
|
|
"""Unload Axis device config entry."""
|
2023-01-13 21:21:40 +00:00
|
|
|
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
|
2019-04-15 22:06:45 +00:00
|
|
|
return await device.async_reset()
|
|
|
|
|
|
|
|
|
2022-01-10 08:34:22 +00:00
|
|
|
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-01-30 21:20:30 +00:00
|
|
|
"""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
|
|
|
|
2023-01-13 16:12:51 +00:00
|
|
|
if config_entry.version != 3:
|
|
|
|
# Home Assistant 2023.2
|
|
|
|
config_entry.version = 3
|
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
|