Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The foscam component."""
2 
3 from libpyfoscam import FoscamCamera
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import (
7  CONF_HOST,
8  CONF_PASSWORD,
9  CONF_PORT,
10  CONF_USERNAME,
11  Platform,
12 )
13 from homeassistant.core import HomeAssistant, callback
14 from homeassistant.helpers.entity_registry import async_migrate_entries
15 
16 from .config_flow import DEFAULT_RTSP_PORT
17 from .const import CONF_RTSP_PORT, DOMAIN, LOGGER, SERVICE_PTZ, SERVICE_PTZ_PRESET
18 from .coordinator import FoscamCoordinator
19 
20 PLATFORMS = [Platform.CAMERA, Platform.SWITCH]
21 
22 
23 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
24  """Set up foscam from a config entry."""
25 
26  session = FoscamCamera(
27  entry.data[CONF_HOST],
28  entry.data[CONF_PORT],
29  entry.data[CONF_USERNAME],
30  entry.data[CONF_PASSWORD],
31  verbose=False,
32  )
33  coordinator = FoscamCoordinator(hass, session)
34 
35  await coordinator.async_config_entry_first_refresh()
36 
37  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
38 
39  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
40 
41  return True
42 
43 
44 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
45  """Unload a config entry."""
46  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
47  if unload_ok:
48  hass.data[DOMAIN].pop(entry.entry_id)
49 
50  if not hass.data[DOMAIN]:
51  hass.services.async_remove(domain=DOMAIN, service=SERVICE_PTZ)
52  hass.services.async_remove(domain=DOMAIN, service=SERVICE_PTZ_PRESET)
53 
54  return unload_ok
55 
56 
57 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
58  """Migrate old entry."""
59  LOGGER.debug("Migrating from version %s", entry.version)
60 
61  if entry.version == 1:
62  # Change unique id
63  @callback
64  def update_unique_id(entry):
65  return {"new_unique_id": entry.entry_id}
66 
67  await async_migrate_entries(hass, entry.entry_id, update_unique_id)
68 
69  # Get RTSP port from the camera or use the fallback one and store it in data
70  camera = FoscamCamera(
71  entry.data[CONF_HOST],
72  entry.data[CONF_PORT],
73  entry.data[CONF_USERNAME],
74  entry.data[CONF_PASSWORD],
75  verbose=False,
76  )
77 
78  ret, response = await hass.async_add_executor_job(camera.get_port_info)
79 
80  rtsp_port = DEFAULT_RTSP_PORT
81 
82  if ret != 0:
83  rtsp_port = response.get("rtspPort") or response.get("mediaPort")
84 
85  hass.config_entries.async_update_entry(
86  entry,
87  data={**entry.data, CONF_RTSP_PORT: rtsp_port},
88  version=2,
89  unique_id=None,
90  )
91 
92  LOGGER.debug("Migration to version %s successful", entry.version)
93 
94  return True
None async_migrate_entries(HomeAssistant hass, dict[str, AdapterDetails] adapters, str default_adapter)
Definition: __init__.py:249
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
Definition: __init__.py:168
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:23
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:57
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44