1 """Feed Entity Manager Sensor support for GeoNet NZ Volcano Feeds."""
3 from __future__
import annotations
27 _LOGGER = logging.getLogger(__name__)
29 ATTR_LAST_UPDATE =
"feed_last_update"
30 ATTR_LAST_UPDATE_SUCCESSFUL =
"feed_last_update_successful"
34 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
36 """Set up the GeoNet NZ Volcano Feed platform."""
37 manager = hass.data[DOMAIN][FEED][entry.entry_id]
40 def async_add_sensor(feed_manager, external_id, unit_system):
41 """Add sensor entity from feed."""
43 entry.entry_id, feed_manager, external_id, unit_system
45 _LOGGER.debug(
"Adding sensor %s", new_entity)
48 manager.listeners.append(
50 hass, manager.async_event_new_entity(), async_add_sensor
53 hass.async_create_task(manager.async_update())
54 _LOGGER.debug(
"Sensor setup done")
58 """Represents an external event with GeoNet NZ Volcano feed data."""
60 _attr_should_poll =
False
62 def __init__(self, config_entry_id, feed_manager, external_id, unit_system):
63 """Initialize entity with data from feed entry."""
82 """Call when entity is added to hass."""
85 f
"geonetnz_volcano_update_{self._external_id}",
90 """Call when entity will be removed from hass."""
96 """Call update method."""
100 """Update this entity from the data held in the feed manager."""
101 _LOGGER.debug(
"Updating %s", self.
_external_id_external_id)
104 last_update_successful = self.
_feed_manager_feed_manager.last_update_successful()
106 self.
_update_from_feed_update_from_feed(feed_entry, last_update, last_update_successful)
109 """Update the internal state from the provided feed entry."""
110 self.
_title_title = feed_entry.title
114 DistanceConverter.convert(
115 feed_entry.distance_to_home,
116 UnitOfLength.KILOMETERS,
122 self.
_distance_distance = round(feed_entry.distance_to_home, 1)
123 self.
_latitude_latitude = round(feed_entry.coordinates[0], 5)
124 self.
_longitude_longitude = round(feed_entry.coordinates[1], 5)
127 self.
_activity_activity = feed_entry.activity
128 self.
_hazards_hazards = feed_entry.hazards
129 self.
_feed_last_update_feed_last_update = dt_util.as_utc(last_update)
if last_update
else None
131 dt_util.as_utc(last_update_successful)
if last_update_successful
else None
136 """Return the state of the sensor."""
141 """Return the icon to use in the frontend, if any."""
146 """Return the name of the entity."""
147 return f
"Volcano {self._title}"
151 """Return the unit of measurement."""
156 """Return the device state attributes."""
161 (ATTR_ACTIVITY, self.
_activity_activity),
162 (ATTR_HAZARDS, self.
_hazards_hazards),
164 (ATTR_LATITUDE, self.
_latitude_latitude),
165 (ATTR_DISTANCE, self.
_distance_distance),
169 if value
or isinstance(value, bool)
def __init__(self, config_entry_id, feed_manager, external_id, unit_system)
None async_added_to_hass(self)
_feed_last_update_successful
def _update_callback(self)
def native_unit_of_measurement(self)
def _update_from_feed(self, feed_entry, last_update, last_update_successful)
def extra_state_attributes(self)
None async_will_remove_from_hass(self)
None async_schedule_update_ha_state(self, bool force_refresh=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)