diff --git a/homeassistant/components/geniushub/sensor.py b/homeassistant/components/geniushub/sensor.py index ef148b48143..a52bd2d692f 100644 --- a/homeassistant/components/geniushub/sensor.py +++ b/homeassistant/components/geniushub/sensor.py @@ -1,11 +1,12 @@ """Support for Genius Hub sensor devices.""" -from datetime import datetime +from datetime import timedelta import logging from homeassistant.const import DEVICE_CLASS_BATTERY from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity +from homeassistant.util.dt import utc_from_timestamp, utcnow from . import DOMAIN @@ -58,6 +59,29 @@ class GeniusDevice(Entity): """Return the name of the sensor.""" return self._name + @property + def icon(self): + """Return the icon of the sensor.""" + values = self._device._info_raw['childValues'] # noqa; pylint: disable=protected-access + + last_comms = utc_from_timestamp(values['lastComms']['val']) + interval = timedelta(seconds=values['WakeUp_Interval']['val']) + + if last_comms < utcnow() - interval * 3: + return 'mdi:battery-unknown' + + battery_level = self._device.state['batteryLevel'] + if battery_level == 255: + return 'mdi:battery-unknown' + if battery_level < 40: + return 'mdi:battery-alert' + + icon = 'mdi:battery' + if battery_level <= 95: + icon += '-{}'.format(int(round(battery_level / 10 - .01)) * 10) + + return icon + @property def device_class(self): """Return the device class of the sensor.""" @@ -86,8 +110,7 @@ class GeniusDevice(Entity): attrs['assigned_zone'] = self._device.assignedZones[0]['name'] last_comms = self._device._info_raw['childValues']['lastComms']['val'] # noqa; pylint: disable=protected-access - attrs['last_comms'] = datetime.utcfromtimestamp( - last_comms).isoformat() + attrs['last_comms'] = utc_from_timestamp(last_comms).isoformat() return {**attrs}