Tweak geniushub battery icons according to device state (#24798)

* tweak battery icons according to device state/availability

* tweak battery icons according to device state/availability 2

* make dt objects aware

* make dt objects aware 2

* woops - use util.dt in favour of datetime

* woops - use util.dt in favour of datetime 2

* refactor battery icon code, remove parallel_updates
This commit is contained in:
David Bonnes 2019-07-01 18:19:14 +01:00 committed by Paulus Schoutsen
parent 3d2f843c1d
commit 846575b7fb

View file

@ -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}