Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The trafikverket_camera component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from pytrafikverket import TrafikverketCamera
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import CONF_API_KEY, CONF_ID, CONF_LOCATION
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import config_validation as cv
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN, PLATFORMS
16 from .coordinator import TVDataUpdateCoordinator
17 
18 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 TVCameraConfigEntry = ConfigEntry[TVDataUpdateCoordinator]
23 
24 
25 async def async_setup_entry(hass: HomeAssistant, entry: TVCameraConfigEntry) -> bool:
26  """Set up Trafikverket Camera from a config entry."""
27 
28  coordinator = TVDataUpdateCoordinator(hass, entry)
29  await coordinator.async_config_entry_first_refresh()
30  entry.runtime_data = coordinator
31 
32  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
33 
34  return True
35 
36 
37 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
38  """Unload Trafikverket Camera config entry."""
39  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
40 
41 
42 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43  """Migrate old entry."""
44  api_key = entry.data[CONF_API_KEY]
45  web_session = async_get_clientsession(hass)
46  camera_api = TrafikverketCamera(web_session, api_key)
47  # Change entry unique id from location to camera id
48  if entry.version == 1:
49  location = entry.data[CONF_LOCATION]
50 
51  try:
52  camera_info = await camera_api.async_get_camera(location)
53  except Exception: # noqa: BLE001
54  _LOGGER.error(
55  "Could not migrate the config entry. No connection to the api"
56  )
57  return False
58 
59  if camera_id := camera_info.camera_id:
60  hass.config_entries.async_update_entry(
61  entry,
62  unique_id=f"{DOMAIN}-{camera_id}",
63  version=2,
64  )
65  _LOGGER.debug(
66  "Migrated Trafikverket Camera config entry unique id to %s",
67  camera_id,
68  )
69  else:
70  _LOGGER.error("Could not migrate the config entry. Camera has no id")
71  return False
72 
73  # Change entry data from location to id
74  if entry.version == 2:
75  location = entry.data[CONF_LOCATION]
76 
77  try:
78  camera_info = await camera_api.async_get_camera(location)
79  except Exception: # noqa: BLE001
80  _LOGGER.error(
81  "Could not migrate the config entry. No connection to the api"
82  )
83  return False
84 
85  if camera_id := camera_info.camera_id:
86  _LOGGER.debug(
87  "Migrate Trafikverket Camera config entry unique id to %s",
88  camera_id,
89  )
90  new_data = entry.data.copy()
91  new_data.pop(CONF_LOCATION)
92  new_data[CONF_ID] = camera_id
93  hass.config_entries.async_update_entry(entry, data=new_data, version=3)
94  return True
95  _LOGGER.error("Could not migrate the config entry. Camera has no id")
96  return False
97  return True
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:42
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:37
bool async_setup_entry(HomeAssistant hass, TVCameraConfigEntry entry)
Definition: __init__.py:25
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)