1 """The trafikverket_camera component."""
3 from __future__
import annotations
7 from pytrafikverket
import TrafikverketCamera
15 from .const
import DOMAIN, PLATFORMS
16 from .coordinator
import TVDataUpdateCoordinator
18 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
20 _LOGGER = logging.getLogger(__name__)
22 TVCameraConfigEntry = ConfigEntry[TVDataUpdateCoordinator]
26 """Set up Trafikverket Camera from a config entry."""
29 await coordinator.async_config_entry_first_refresh()
30 entry.runtime_data = coordinator
32 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
38 """Unload Trafikverket Camera config entry."""
39 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
43 """Migrate old entry."""
44 api_key = entry.data[CONF_API_KEY]
46 camera_api = TrafikverketCamera(web_session, api_key)
48 if entry.version == 1:
49 location = entry.data[CONF_LOCATION]
52 camera_info = await camera_api.async_get_camera(location)
55 "Could not migrate the config entry. No connection to the api"
59 if camera_id := camera_info.camera_id:
60 hass.config_entries.async_update_entry(
62 unique_id=f
"{DOMAIN}-{camera_id}",
66 "Migrated Trafikverket Camera config entry unique id to %s",
70 _LOGGER.error(
"Could not migrate the config entry. Camera has no id")
74 if entry.version == 2:
75 location = entry.data[CONF_LOCATION]
78 camera_info = await camera_api.async_get_camera(location)
81 "Could not migrate the config entry. No connection to the api"
85 if camera_id := camera_info.camera_id:
87 "Migrate Trafikverket Camera config entry unique id to %s",
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)
95 _LOGGER.error(
"Could not migrate the config entry. Camera has no id")
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, TVCameraConfigEntry entry)
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)