diff --git a/homeassistant/components/geniushub/sensor.py b/homeassistant/components/geniushub/sensor.py index fc00c0db79ef..744906b8f2d0 100644 --- a/homeassistant/components/geniushub/sensor.py +++ b/homeassistant/components/geniushub/sensor.py @@ -14,6 +14,12 @@ _LOGGER = logging.getLogger(__name__) GH_HAS_BATTERY = [ 'Room Thermostat', 'Genius Valve', 'Room Sensor', 'Radiator Valve'] +GH_LEVEL_MAPPING = { + 'error': 'Errors', + 'warning': 'Warnings', + 'information': 'Information' +} + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): @@ -23,7 +29,10 @@ async def async_setup_platform(hass, config, async_add_entities, sensors = [GeniusDevice(client, d) for d in client.hub.device_objs if d.type in GH_HAS_BATTERY] - async_add_entities(sensors) + issues = [GeniusIssue(client, i) + for i in list(GH_LEVEL_MAPPING)] + + async_add_entities(sensors + issues, update_before_add=True) class GeniusDevice(Entity): @@ -80,3 +89,47 @@ class GeniusDevice(Entity): last_comms).isoformat() return {**attrs} + + +class GeniusIssue(Entity): + """Representation of a Genius Hub sensor.""" + + def __init__(self, client, level): + """Initialize the sensor.""" + self._hub = client.hub + self._name = GH_LEVEL_MAPPING[level] + self._level = level + self._issues = [] + + async def async_added_to_hass(self): + """Set up a listener when this entity is added to HA.""" + async_dispatcher_connect(self.hass, DOMAIN, self._refresh) + + @callback + def _refresh(self): + self.async_schedule_update_ha_state(force_refresh=True) + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def should_poll(self) -> bool: + """Return False as the geniushub devices should not be polled.""" + return False + + @property + def state(self): + """Return the number of issues.""" + return len(self._issues) + + @property + def device_state_attributes(self): + """Return the device state attributes.""" + return {'{}_list'.format(self._level): self._issues} + + async def async_update(self): + """Process the sensor's state data.""" + self._issues = [i['description'] + for i in self._hub.issues if i['level'] == self._level]