Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The GeoJSON events component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers import entity_registry as er
11 
12 from .const import DOMAIN, PLATFORMS
13 from .manager import GeoJsonFeedEntityManager
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 
18 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
19  """Set up the GeoJSON events component as config entry."""
20  feeds = hass.data.setdefault(DOMAIN, {})
21  # Create feed entity manager for all platforms.
22  manager = GeoJsonFeedEntityManager(hass, config_entry)
23  feeds[config_entry.entry_id] = manager
24  _LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id)
25  await remove_orphaned_entities(hass, config_entry.entry_id)
26  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
27  await manager.async_init()
28  return True
29 
30 
31 async def remove_orphaned_entities(hass: HomeAssistant, entry_id: str) -> None:
32  """Remove orphaned geo_location entities.
33 
34  This is needed because when fetching data from the external feed this integration is
35  determining which entities need to be added, updated or removed by comparing the
36  current with the previous data. After a restart of Home Assistant the integration
37  has no previous data to compare against, and thus all entities managed by this
38  integration are removed after startup.
39  """
40  entity_registry = er.async_get(hass)
41  orphaned_entries = er.async_entries_for_config_entry(entity_registry, entry_id)
42  if orphaned_entries is not None:
43  for entry in orphaned_entries:
44  if entry.domain == Platform.GEO_LOCATION:
45  _LOGGER.debug("Removing orphaned entry %s", entry.entity_id)
46  entity_registry.async_remove(entry.entity_id)
47 
48 
49 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
50  """Unload the GeoJSON events config entry."""
51  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
52  if unload_ok:
53  manager: GeoJsonFeedEntityManager = hass.data[DOMAIN].pop(entry.entry_id)
54  await manager.async_stop()
55  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:49
None remove_orphaned_entities(HomeAssistant hass, str entry_id)
Definition: __init__.py:31
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:18