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:
parent
3d2f843c1d
commit
846575b7fb
1 changed files with 26 additions and 3 deletions
|
@ -1,11 +1,12 @@
|
||||||
"""Support for Genius Hub sensor devices."""
|
"""Support for Genius Hub sensor devices."""
|
||||||
from datetime import datetime
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.const import DEVICE_CLASS_BATTERY
|
from homeassistant.const import DEVICE_CLASS_BATTERY
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util.dt import utc_from_timestamp, utcnow
|
||||||
|
|
||||||
from . import DOMAIN
|
from . import DOMAIN
|
||||||
|
|
||||||
|
@ -58,6 +59,29 @@ class GeniusDevice(Entity):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self._name
|
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
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
"""Return the device class of the sensor."""
|
"""Return the device class of the sensor."""
|
||||||
|
@ -86,8 +110,7 @@ class GeniusDevice(Entity):
|
||||||
attrs['assigned_zone'] = self._device.assignedZones[0]['name']
|
attrs['assigned_zone'] = self._device.assignedZones[0]['name']
|
||||||
|
|
||||||
last_comms = self._device._info_raw['childValues']['lastComms']['val'] # noqa; pylint: disable=protected-access
|
last_comms = self._device._info_raw['childValues']['lastComms']['val'] # noqa; pylint: disable=protected-access
|
||||||
attrs['last_comms'] = datetime.utcfromtimestamp(
|
attrs['last_comms'] = utc_from_timestamp(last_comms).isoformat()
|
||||||
last_comms).isoformat()
|
|
||||||
|
|
||||||
return {**attrs}
|
return {**attrs}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue