Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The generic component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import entity_registry as er
11 
12 DOMAIN = "generic"
13 PLATFORMS = [Platform.CAMERA]
14 
15 
16 async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
17  """Handle options update."""
18  await hass.config_entries.async_reload(entry.entry_id)
19 
20 
21 async def _async_migrate_unique_ids(hass: HomeAssistant, entry: ConfigEntry) -> None:
22  """Migrate entities to the new unique id."""
23 
24  @callback
25  def _async_migrator(entity_entry: er.RegistryEntry) -> dict[str, Any] | None:
26  if entity_entry.unique_id == entry.entry_id:
27  # Already correct, nothing to do
28  return None
29  # There is only one entity, and its unique id
30  # should always be the same as the config entry entry_id
31  return {"new_unique_id": entry.entry_id}
32 
33  await er.async_migrate_entries(hass, entry.entry_id, _async_migrator)
34 
35 
36 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
37  """Set up generic IP camera from a config entry."""
38 
39  await _async_migrate_unique_ids(hass, entry)
40  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
41 
42  entry.async_on_unload(entry.add_update_listener(_async_update_listener))
43  return True
44 
45 
46 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
47  """Unload a config entry."""
48 
49  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:16
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:36
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:46
None _async_migrate_unique_ids(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:21