1 """Feed Entity Manager Sensor support for GDACS Feed."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from datetime
import datetime
10 from aio_georss_client.status_update
import StatusUpdate
20 from .
import GdacsFeedEntityManager
21 from .const
import DOMAIN, FEED
23 _LOGGER = logging.getLogger(__name__)
25 ATTR_STATUS =
"status"
26 ATTR_LAST_UPDATE =
"last_update"
27 ATTR_LAST_UPDATE_SUCCESSFUL =
"last_update_successful"
28 ATTR_LAST_TIMESTAMP =
"last_timestamp"
29 ATTR_CREATED =
"created"
30 ATTR_UPDATED =
"updated"
31 ATTR_REMOVED =
"removed"
33 DEFAULT_UNIT_OF_MEASUREMENT =
"alerts"
40 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
42 """Set up the GDACS Feed platform."""
43 manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][entry.entry_id]
49 """Status sensor for the GDACS integration."""
51 _attr_should_poll =
False
52 _attr_native_unit_of_measurement = DEFAULT_UNIT_OF_MEASUREMENT
53 _attr_has_entity_name =
True
55 _attr_translation_key =
"alerts"
58 self, config_entry: ConfigEntry, manager: GdacsFeedEntityManager
60 """Initialize entity."""
61 assert config_entry.unique_id
65 self.
_status_status: str |
None =
None
69 self.
_total_total: int |
None =
None
70 self.
_created_created: int |
None =
None
71 self.
_updated_updated: int |
None =
None
72 self.
_removed_removed: int |
None =
None
75 identifiers={(DOMAIN, config_entry.unique_id)},
76 entry_type=DeviceEntryType.SERVICE,
81 """Call when entity is added to hass."""
84 f
"gdacs_status_{self._config_entry_id}",
87 _LOGGER.debug(
"Waiting for updates %s", self.
_config_entry_id_config_entry_id)
92 """Call when entity will be removed from hass."""
98 """Call status update method."""
99 _LOGGER.debug(
"Received status update for %s", self.
_config_entry_id_config_entry_id)
103 """Update this entity from the data held in the feed manager."""
106 status_info = self.
_manager_manager.status_info()
111 """Update the internal state from the provided information."""
114 dt_util.as_utc(status_info.last_update)
if status_info.last_update
else None
116 if status_info.last_update_successful:
118 status_info.last_update_successful
130 """Return the state of the sensor."""
135 """Return the device state attributes."""
139 (ATTR_STATUS, self.
_status_status),
143 (ATTR_CREATED, self.
_created_created),
144 (ATTR_UPDATED, self.
_updated_updated),
145 (ATTR_REMOVED, self.
_removed_removed),
147 if value
or isinstance(value, bool)
None async_will_remove_from_hass(self)
None async_added_to_hass(self)
None _update_status_callback(self)
int|None native_value(self)
None _update_from_status_info(self, StatusUpdate status_info)
dict[str, Any] extra_state_attributes(self)
None __init__(self, ConfigEntry config_entry, GdacsFeedEntityManager manager)
None async_schedule_update_ha_state(self, bool force_refresh=False)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)