2019-06-19 11:59:29 +00:00
|
|
|
"""Support for Queensland Bushfire Alert Feeds."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-06-19 11:59:29 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from georss_qld_bushfire_alert_client import QldBushfireAlertFeedManager
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.geo_location import PLATFORM_SCHEMA, GeolocationEvent
|
2019-06-19 11:59:29 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_RADIUS,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
2020-04-11 00:12:39 +00:00
|
|
|
LENGTH_KILOMETERS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 15:22:23 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2019-06-19 11:59:29 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send
|
2022-01-03 15:22:23 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-06-19 11:59:29 +00:00
|
|
|
from homeassistant.helpers.event import track_time_interval
|
2022-01-03 15:22:23 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CATEGORY = "category"
|
|
|
|
ATTR_EXTERNAL_ID = "external_id"
|
|
|
|
ATTR_PUBLICATION_DATE = "publication_date"
|
|
|
|
ATTR_STATUS = "status"
|
|
|
|
ATTR_UPDATED_DATE = "updated_date"
|
2019-06-19 11:59:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CATEGORIES = "categories"
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
DEFAULT_RADIUS_IN_KM = 20.0
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SIGNAL_DELETE_ENTITY = "qld_bushfire_delete_{}"
|
|
|
|
SIGNAL_UPDATE_ENTITY = "qld_bushfire_update_{}"
|
2019-06-19 11:59:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SOURCE = "qld_bushfire"
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
VALID_CATEGORIES = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"Emergency Warning",
|
|
|
|
"Watch and Act",
|
|
|
|
"Advice",
|
|
|
|
"Notification",
|
|
|
|
"Information",
|
2019-06-19 11:59:29 +00:00
|
|
|
]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_LATITUDE): cv.latitude,
|
|
|
|
vol.Optional(CONF_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS_IN_KM): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_CATEGORIES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(VALID_CATEGORIES)]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
|
2022-01-03 15:22:23 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Set up the Queensland Bushfire Alert Feed platform."""
|
|
|
|
scan_interval = config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL)
|
2019-07-31 19:25:30 +00:00
|
|
|
coordinates = (
|
|
|
|
config.get(CONF_LATITUDE, hass.config.latitude),
|
|
|
|
config.get(CONF_LONGITUDE, hass.config.longitude),
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
radius_in_km = config[CONF_RADIUS]
|
|
|
|
categories = config[CONF_CATEGORIES]
|
|
|
|
# Initialize the entity manager.
|
|
|
|
feed = QldBushfireFeedEntityManager(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, add_entities, scan_interval, coordinates, radius_in_km, categories
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
def start_feed_manager(event):
|
|
|
|
"""Start feed manager."""
|
|
|
|
feed.startup()
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_feed_manager)
|
|
|
|
|
|
|
|
|
|
|
|
class QldBushfireFeedEntityManager:
|
|
|
|
"""Feed Entity Manager for Qld Bushfire Alert GeoRSS feed."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass, add_entities, scan_interval, coordinates, radius_in_km, categories
|
|
|
|
):
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Initialize the Feed Entity Manager."""
|
|
|
|
self._hass = hass
|
|
|
|
self._feed_manager = QldBushfireAlertFeedManager(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._generate_entity,
|
|
|
|
self._update_entity,
|
|
|
|
self._remove_entity,
|
|
|
|
coordinates,
|
|
|
|
filter_radius=radius_in_km,
|
|
|
|
filter_categories=categories,
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
self._add_entities = add_entities
|
|
|
|
self._scan_interval = scan_interval
|
|
|
|
|
|
|
|
def startup(self):
|
|
|
|
"""Start up this manager."""
|
|
|
|
self._feed_manager.update()
|
|
|
|
self._init_regular_updates()
|
|
|
|
|
|
|
|
def _init_regular_updates(self):
|
|
|
|
"""Schedule regular updates at the specified interval."""
|
|
|
|
track_time_interval(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._hass, lambda now: self._feed_manager.update(), self._scan_interval
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
def get_entry(self, external_id):
|
|
|
|
"""Get feed entry by external id."""
|
|
|
|
return self._feed_manager.feed_entries.get(external_id)
|
|
|
|
|
|
|
|
def _generate_entity(self, external_id):
|
|
|
|
"""Generate new entity."""
|
|
|
|
new_entity = QldBushfireLocationEvent(self, external_id)
|
|
|
|
# Add new entities to HA.
|
|
|
|
self._add_entities([new_entity], True)
|
|
|
|
|
|
|
|
def _update_entity(self, external_id):
|
|
|
|
"""Update entity."""
|
|
|
|
dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY.format(external_id))
|
|
|
|
|
|
|
|
def _remove_entity(self, external_id):
|
|
|
|
"""Remove entity."""
|
|
|
|
dispatcher_send(self._hass, SIGNAL_DELETE_ENTITY.format(external_id))
|
|
|
|
|
|
|
|
|
|
|
|
class QldBushfireLocationEvent(GeolocationEvent):
|
|
|
|
"""This represents an external event with Qld Bushfire feed data."""
|
|
|
|
|
|
|
|
def __init__(self, feed_manager, external_id):
|
|
|
|
"""Initialize entity with data from feed entry."""
|
|
|
|
self._feed_manager = feed_manager
|
|
|
|
self._external_id = external_id
|
|
|
|
self._name = None
|
|
|
|
self._distance = None
|
|
|
|
self._latitude = None
|
|
|
|
self._longitude = None
|
|
|
|
self._attribution = None
|
|
|
|
self._category = None
|
|
|
|
self._publication_date = None
|
|
|
|
self._updated_date = None
|
|
|
|
self._status = None
|
|
|
|
self._remove_signal_delete = None
|
|
|
|
self._remove_signal_update = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Call when entity is added to hass."""
|
|
|
|
self._remove_signal_delete = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
SIGNAL_DELETE_ENTITY.format(self._external_id),
|
|
|
|
self._delete_callback,
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
self._remove_signal_update = async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
SIGNAL_UPDATE_ENTITY.format(self._external_id),
|
|
|
|
self._update_callback,
|
|
|
|
)
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _delete_callback(self):
|
|
|
|
"""Remove this entity."""
|
|
|
|
self._remove_signal_delete()
|
|
|
|
self._remove_signal_update()
|
2021-02-08 09:45:46 +00:00
|
|
|
self.hass.async_create_task(self.async_remove(force_remove=True))
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_callback(self):
|
|
|
|
"""Call update method."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed for Qld Bushfire Alert feed location events."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update this entity from the data held in the feed manager."""
|
|
|
|
_LOGGER.debug("Updating %s", self._external_id)
|
|
|
|
feed_entry = self._feed_manager.get_entry(self._external_id)
|
|
|
|
if feed_entry:
|
|
|
|
self._update_from_feed(feed_entry)
|
|
|
|
|
|
|
|
def _update_from_feed(self, feed_entry):
|
|
|
|
"""Update the internal state from the provided feed entry."""
|
|
|
|
self._name = feed_entry.title
|
|
|
|
self._distance = feed_entry.distance_to_home
|
|
|
|
self._latitude = feed_entry.coordinates[0]
|
|
|
|
self._longitude = feed_entry.coordinates[1]
|
|
|
|
self._attribution = feed_entry.attribution
|
|
|
|
self._category = feed_entry.category
|
|
|
|
self._publication_date = feed_entry.published
|
|
|
|
self._updated_date = feed_entry.updated
|
|
|
|
self._status = feed_entry.status
|
|
|
|
|
2019-09-05 15:11:48 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend."""
|
|
|
|
return "mdi:fire"
|
|
|
|
|
2019-06-19 11:59:29 +00:00
|
|
|
@property
|
|
|
|
def source(self) -> str:
|
|
|
|
"""Return source value of this external event."""
|
|
|
|
return SOURCE
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def name(self) -> str | None:
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def distance(self) -> float | None:
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Return distance value of this external event."""
|
|
|
|
return self._distance
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def latitude(self) -> float | None:
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Return latitude value of this external event."""
|
|
|
|
return self._latitude
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def longitude(self) -> float | None:
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Return longitude value of this external event."""
|
|
|
|
return self._longitude
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
2020-04-11 00:12:39 +00:00
|
|
|
return LENGTH_KILOMETERS
|
2019-06-19 11:59:29 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-06-19 11:59:29 +00:00
|
|
|
"""Return the device state attributes."""
|
|
|
|
attributes = {}
|
|
|
|
for key, value in (
|
2019-07-31 19:25:30 +00:00
|
|
|
(ATTR_EXTERNAL_ID, self._external_id),
|
|
|
|
(ATTR_CATEGORY, self._category),
|
|
|
|
(ATTR_ATTRIBUTION, self._attribution),
|
|
|
|
(ATTR_PUBLICATION_DATE, self._publication_date),
|
|
|
|
(ATTR_UPDATED_DATE, self._updated_date),
|
|
|
|
(ATTR_STATUS, self._status),
|
2019-06-19 11:59:29 +00:00
|
|
|
):
|
|
|
|
if value or isinstance(value, bool):
|
|
|
|
attributes[key] = value
|
|
|
|
return attributes
|