Add GDACS feed integration (#31235)
* initial version of gdacs integration * updated translations * generated files * added abbreviation * bumped library version * small feed entry attribute fixes * add unit tests * need to use original mdi name * bumped library version * improved entity name for earthquakes * round vulnerability number * typo * support for categories * testing support for categories * tie longitude and latitude together * validating categories * simplifying setup * passing domain as parameter * simplified test setup * moved test code * simplified test code * removed superfluous code * changed approach to unique identifier * changed code structure * simplified unit system handling * made schema a constant * comment added * simplifying code * added message if location already configured * removed unnecessary code * simplified test code * avoid mocking __init__ * pylint * simplified code * fetch categories from integration library * setting PARALLEL_UPDATES * setting PARALLEL_UPDATES to zero/unlimited * added quality scale
This commit is contained in:
parent
05b3c1f17d
commit
8d429d7676
18 changed files with 1240 additions and 0 deletions
140
homeassistant/components/gdacs/sensor.py
Normal file
140
homeassistant/components/gdacs/sensor.py
Normal file
|
@ -0,0 +1,140 @@
|
|||
"""Feed Entity Manager Sensor support for GDACS Feed."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import dt
|
||||
|
||||
from .const import DEFAULT_ICON, DOMAIN, FEED, SIGNAL_STATUS
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_STATUS = "status"
|
||||
ATTR_LAST_UPDATE = "last_update"
|
||||
ATTR_LAST_UPDATE_SUCCESSFUL = "last_update_successful"
|
||||
ATTR_LAST_TIMESTAMP = "last_timestamp"
|
||||
ATTR_CREATED = "created"
|
||||
ATTR_UPDATED = "updated"
|
||||
ATTR_REMOVED = "removed"
|
||||
|
||||
DEFAULT_UNIT_OF_MEASUREMENT = "alerts"
|
||||
|
||||
# An update of this entity is not making a web request, but uses internal data only.
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Set up the GDACS Feed platform."""
|
||||
manager = hass.data[DOMAIN][FEED][entry.entry_id]
|
||||
sensor = GdacsSensor(entry.entry_id, entry.title, manager)
|
||||
async_add_entities([sensor])
|
||||
_LOGGER.debug("Sensor setup done")
|
||||
|
||||
|
||||
class GdacsSensor(Entity):
|
||||
"""This is a status sensor for the GDACS integration."""
|
||||
|
||||
def __init__(self, config_entry_id, config_title, manager):
|
||||
"""Initialize entity."""
|
||||
self._config_entry_id = config_entry_id
|
||||
self._config_title = config_title
|
||||
self._manager = manager
|
||||
self._status = None
|
||||
self._last_update = None
|
||||
self._last_update_successful = None
|
||||
self._last_timestamp = None
|
||||
self._total = None
|
||||
self._created = None
|
||||
self._updated = None
|
||||
self._removed = None
|
||||
self._remove_signal_status = None
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Call when entity is added to hass."""
|
||||
self._remove_signal_status = async_dispatcher_connect(
|
||||
self.hass,
|
||||
SIGNAL_STATUS.format(self._config_entry_id),
|
||||
self._update_status_callback,
|
||||
)
|
||||
_LOGGER.debug("Waiting for updates %s", self._config_entry_id)
|
||||
# First update is manual because of how the feed entity manager is updated.
|
||||
await self.async_update()
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
"""Call when entity will be removed from hass."""
|
||||
if self._remove_signal_status:
|
||||
self._remove_signal_status()
|
||||
|
||||
@callback
|
||||
def _update_status_callback(self):
|
||||
"""Call status update method."""
|
||||
_LOGGER.debug("Received status update for %s", self._config_entry_id)
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed for GDACS status sensor."""
|
||||
return False
|
||||
|
||||
async def async_update(self):
|
||||
"""Update this entity from the data held in the feed manager."""
|
||||
_LOGGER.debug("Updating %s", self._config_entry_id)
|
||||
if self._manager:
|
||||
status_info = self._manager.status_info()
|
||||
if status_info:
|
||||
self._update_from_status_info(status_info)
|
||||
|
||||
def _update_from_status_info(self, status_info):
|
||||
"""Update the internal state from the provided information."""
|
||||
self._status = status_info.status
|
||||
self._last_update = (
|
||||
dt.as_utc(status_info.last_update) if status_info.last_update else None
|
||||
)
|
||||
if status_info.last_update_successful:
|
||||
self._last_update_successful = dt.as_utc(status_info.last_update_successful)
|
||||
else:
|
||||
self._last_update_successful = None
|
||||
self._last_timestamp = status_info.last_timestamp
|
||||
self._total = status_info.total
|
||||
self._created = status_info.created
|
||||
self._updated = status_info.updated
|
||||
self._removed = status_info.removed
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._total
|
||||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
"""Return the name of the entity."""
|
||||
return f"GDACS ({self._config_title})"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend, if any."""
|
||||
return DEFAULT_ICON
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement."""
|
||||
return DEFAULT_UNIT_OF_MEASUREMENT
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
attributes = {}
|
||||
for key, value in (
|
||||
(ATTR_STATUS, self._status),
|
||||
(ATTR_LAST_UPDATE, self._last_update),
|
||||
(ATTR_LAST_UPDATE_SUCCESSFUL, self._last_update_successful),
|
||||
(ATTR_LAST_TIMESTAMP, self._last_timestamp),
|
||||
(ATTR_CREATED, self._created),
|
||||
(ATTR_UPDATED, self._updated),
|
||||
(ATTR_REMOVED, self._removed),
|
||||
):
|
||||
if value or isinstance(value, bool):
|
||||
attributes[key] = value
|
||||
return attributes
|
Loading…
Add table
Add a link
Reference in a new issue