1 """Geolocation support for GDACS Feed."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import datetime
10 from aio_georss_gdacs.feed_entry
import GdacsFeedEntry
22 from .
import GdacsFeedEntityManager
23 from .const
import DEFAULT_ICON, DOMAIN, FEED
25 _LOGGER = logging.getLogger(__name__)
27 ATTR_ALERT_LEVEL =
"alert_level"
28 ATTR_COUNTRY =
"country"
29 ATTR_DESCRIPTION =
"description"
30 ATTR_DURATION_IN_WEEK =
"duration_in_week"
31 ATTR_EVENT_TYPE =
"event_type"
32 ATTR_EXTERNAL_ID =
"external_id"
33 ATTR_FROM_DATE =
"from_date"
34 ATTR_POPULATION =
"population"
35 ATTR_SEVERITY =
"severity"
36 ATTR_TO_DATE =
"to_date"
37 ATTR_VULNERABILITY =
"vulnerability"
40 "DR":
"mdi:water-off",
42 "FL":
"mdi:home-flood",
43 "TC":
"mdi:weather-hurricane",
45 "VO":
"mdi:image-filter-hdr",
55 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
57 """Set up the GDACS Feed platform."""
58 manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][entry.entry_id]
61 def async_add_geolocation(
62 feed_manager: GdacsFeedEntityManager, integration_id: str, external_id: str
64 """Add geolocation entity from feed."""
65 new_entity =
GdacsEvent(feed_manager, integration_id, external_id)
66 _LOGGER.debug(
"Adding geolocation %s", new_entity)
69 manager.listeners.append(
71 hass, manager.async_event_new_entity(), async_add_geolocation
76 hass.async_create_task(manager.async_update())
77 _LOGGER.debug(
"Geolocation setup done")
81 """Represents an external event with GDACS feed data."""
83 _attr_should_poll =
False
88 feed_manager: GdacsFeedEntityManager,
92 """Initialize entity with data from feed entry."""
98 self.
_country_country: str |
None =
None
103 self.
_from_date_from_date: datetime |
None =
None
104 self.
_to_date_to_date: datetime |
None =
None
106 self.
_severity_severity: str |
None =
None
108 self.
_version_version: int |
None =
None
113 """Call when entity is added to hass."""
114 if self.
hasshass.config.units
is US_CUSTOMARY_SYSTEM:
124 """Call when entity will be removed from hass."""
128 entity_registry = er.async_get(self.
hasshass)
129 if self.
entity_identity_id
in entity_registry.entities:
130 entity_registry.async_remove(self.
entity_identity_id)
134 """Remove this entity."""
135 self.
hasshass.async_create_task(self.
async_removeasync_remove(force_remove=
True))
139 """Call update method."""
143 """Update this entity from the data held in the feed manager."""
144 _LOGGER.debug(
"Updating %s", self.
_external_id_external_id)
150 """Update the internal state from the provided feed entry."""
151 if not (event_name := feed_entry.event_name):
153 event_name = f
"{feed_entry.country} ({feed_entry.event_id})"
154 self.
_attr_name_attr_name = f
"{feed_entry.event_type}: {event_name}"
156 if self.
hasshass.config.units
is US_CUSTOMARY_SYSTEM:
158 feed_entry.distance_to_home, UnitOfLength.KILOMETERS, UnitOfLength.MILES
183 """Return the icon to use in the frontend, if any."""
190 """Return the device state attributes."""
198 (ATTR_COUNTRY, self.
_country_country),
201 (ATTR_TO_DATE, self.
_to_date_to_date),
203 (ATTR_SEVERITY, self.
_severity_severity),
206 if value
or isinstance(value, bool)
None _update_callback(self)
None async_added_to_hass(self)
dict[str, Any] extra_state_attributes(self)
None _update_from_feed(self, GdacsFeedEntry feed_entry)
_attr_unit_of_measurement
None __init__(self, GdacsFeedEntityManager feed_manager, str integration_id, str external_id)
None async_will_remove_from_hass(self)
None _delete_callback(self)
None async_schedule_update_ha_state(self, bool force_refresh=False)
None async_remove(self, *bool force_remove=False)
config_entries.ConfigEntry|None get_entry(HomeAssistant hass, websocket_api.ActiveConnection connection, str entry_id, int msg_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)