Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Axis devices."""
2 
3 import logging
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import EVENT_HOMEASSISTANT_STOP
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
9 
10 from .const import PLATFORMS
11 from .errors import AuthenticationRequired, CannotConnect
12 from .hub import AxisHub, get_axis_api
13 
14 _LOGGER = logging.getLogger(__name__)
15 
16 type AxisConfigEntry = ConfigEntry[AxisHub]
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, config_entry: AxisConfigEntry) -> bool:
20  """Set up the Axis integration."""
21  try:
22  api = await get_axis_api(hass, config_entry.data)
23  except CannotConnect as err:
24  raise ConfigEntryNotReady from err
25  except AuthenticationRequired as err:
26  raise ConfigEntryAuthFailed from err
27 
28  hub = config_entry.runtime_data = AxisHub(hass, config_entry, api)
29  await hub.async_update_device_registry()
30  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
31  hub.setup()
32 
33  config_entry.add_update_listener(hub.async_new_address_callback)
34  config_entry.async_on_unload(hub.teardown)
35  config_entry.async_on_unload(
36  hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, hub.shutdown)
37  )
38 
39  return True
40 
41 
42 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
43  """Unload Axis device config entry."""
44  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
45 
46 
47 async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
48  """Migrate old entry."""
49  _LOGGER.debug("Migrating from version %s", config_entry.version)
50 
51  if config_entry.version != 3:
52  # Home Assistant 2023.2
53  hass.config_entries.async_update_entry(config_entry, version=3)
54 
55  _LOGGER.debug("Migration to version %s successful", config_entry.version)
56 
57  return True
axis.AxisDevice get_axis_api(HomeAssistant hass, MappingProxyType[str, Any] config)
Definition: api.py:27
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:42
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:47
bool async_setup_entry(HomeAssistant hass, AxisConfigEntry config_entry)
Definition: __init__.py:19