Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Global Disaster Alert and Coordination System (GDACS) integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from datetime import datetime, timedelta
7 import logging
8 
9 from aio_georss_client.status_update import StatusUpdate
10 from aio_georss_gdacs import GdacsFeedManager
11 from aio_georss_gdacs.feed_entry import FeedEntry
12 
13 from homeassistant.config_entries import ConfigEntry
14 from homeassistant.const import (
15  CONF_LATITUDE,
16  CONF_LONGITUDE,
17  CONF_RADIUS,
18  CONF_SCAN_INTERVAL,
19  UnitOfLength,
20 )
21 from homeassistant.core import HomeAssistant, callback
22 from homeassistant.helpers import aiohttp_client
23 from homeassistant.helpers.dispatcher import async_dispatcher_send
24 from homeassistant.helpers.event import async_track_time_interval
25 from homeassistant.util.unit_conversion import DistanceConverter
26 from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
27 
28 from .const import ( # noqa: F401
29  CONF_CATEGORIES,
30  DEFAULT_SCAN_INTERVAL,
31  DOMAIN,
32  FEED,
33  PLATFORMS,
34 )
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 
39 async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
40  """Set up the GDACS component as config entry."""
41  hass.data.setdefault(DOMAIN, {})
42  feeds = hass.data[DOMAIN].setdefault(FEED, {})
43 
44  radius = config_entry.data[CONF_RADIUS]
45  if hass.config.units is US_CUSTOMARY_SYSTEM:
46  radius = DistanceConverter.convert(
47  radius, UnitOfLength.MILES, UnitOfLength.KILOMETERS
48  )
49  # Create feed entity manager for all platforms.
50  manager = GdacsFeedEntityManager(hass, config_entry, radius)
51  feeds[config_entry.entry_id] = manager
52  _LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id)
53  await manager.async_init()
54  return True
55 
56 
57 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
58  """Unload an GDACS component config entry."""
59  manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED].pop(entry.entry_id)
60  await manager.async_stop()
61  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
62 
63 
65  """Feed Entity Manager for GDACS feed."""
66 
67  def __init__(
68  self, hass: HomeAssistant, config_entry: ConfigEntry, radius_in_km: float
69  ) -> None:
70  """Initialize the Feed Entity Manager."""
71  self._hass_hass = hass
72  self._config_entry_config_entry = config_entry
73  coordinates = (
74  config_entry.data[CONF_LATITUDE],
75  config_entry.data[CONF_LONGITUDE],
76  )
77  categories = config_entry.data[CONF_CATEGORIES]
78  websession = aiohttp_client.async_get_clientsession(hass)
79  self._feed_manager_feed_manager = GdacsFeedManager(
80  websession,
81  self._generate_entity_generate_entity,
82  self._update_entity_update_entity,
83  self._remove_entity_remove_entity,
84  coordinates,
85  filter_radius=radius_in_km,
86  filter_categories=categories,
87  status_async_callback=self._status_update_status_update,
88  )
89  self._config_entry_id_config_entry_id = config_entry.entry_id
90  self._scan_interval_scan_interval = timedelta(seconds=config_entry.data[CONF_SCAN_INTERVAL])
91  self._track_time_remove_callback_track_time_remove_callback: Callable[[], None] | None = None
92  self._status_info_status_info: StatusUpdate | None = None
93  self.listenerslisteners: list[Callable[[], None]] = []
94 
95  async def async_init(self) -> None:
96  """Schedule initial and regular updates based on configured time interval."""
97 
98  await self._hass_hass.config_entries.async_forward_entry_setups(
99  self._config_entry_config_entry, PLATFORMS
100  )
101 
102  async def update(event_time: datetime) -> None:
103  """Update."""
104  await self.async_updateasync_update()
105 
106  # Trigger updates at regular intervals.
107  self._track_time_remove_callback_track_time_remove_callback = async_track_time_interval(
108  self._hass_hass, update, self._scan_interval_scan_interval
109  )
110 
111  _LOGGER.debug("Feed entity manager initialized")
112 
113  async def async_update(self) -> None:
114  """Refresh data."""
115  await self._feed_manager_feed_manager.update()
116  _LOGGER.debug("Feed entity manager updated")
117 
118  async def async_stop(self) -> None:
119  """Stop this feed entity manager from refreshing."""
120  for unsub_dispatcher in self.listenerslisteners:
121  unsub_dispatcher()
122  self.listenerslisteners = []
123  if self._track_time_remove_callback_track_time_remove_callback:
124  self._track_time_remove_callback_track_time_remove_callback()
125  _LOGGER.debug("Feed entity manager stopped")
126 
127  @callback
128  def async_event_new_entity(self) -> str:
129  """Return manager specific event to signal new entity."""
130  return f"gdacs_new_geolocation_{self._config_entry_id}"
131 
132  def get_entry(self, external_id: str) -> FeedEntry | None:
133  """Get feed entry by external id."""
134  return self._feed_manager_feed_manager.feed_entries.get(external_id)
135 
136  def status_info(self) -> StatusUpdate | None:
137  """Return latest status update info received."""
138  return self._status_info_status_info
139 
140  async def _generate_entity(self, external_id: str) -> None:
141  """Generate new entity."""
143  self._hass_hass,
144  self.async_event_new_entityasync_event_new_entity(),
145  self,
146  self._config_entry_config_entry.unique_id,
147  external_id,
148  )
149 
150  async def _update_entity(self, external_id: str) -> None:
151  """Update entity."""
152  async_dispatcher_send(self._hass_hass, f"gdacs_update_{external_id}")
153 
154  async def _remove_entity(self, external_id: str) -> None:
155  """Remove entity."""
156  async_dispatcher_send(self._hass_hass, f"gdacs_delete_{external_id}")
157 
158  async def _status_update(self, status_info: StatusUpdate) -> None:
159  """Propagate status update."""
160  _LOGGER.debug("Status update received: %s", status_info)
161  self._status_info_status_info = status_info
162  async_dispatcher_send(self._hass_hass, f"gdacs_status_{self._config_entry_id}")
None _status_update(self, StatusUpdate status_info)
Definition: __init__.py:158
None __init__(self, HomeAssistant hass, ConfigEntry config_entry, float radius_in_km)
Definition: __init__.py:69
FeedEntry|None get_entry(self, str external_id)
Definition: __init__.py:132
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:57
bool async_setup_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:39
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)
Definition: event.py:1679