2019-05-14 21:30:26 +00:00
|
|
|
"""Support for Genius Hub sensor devices."""
|
2019-07-01 17:19:14 +00:00
|
|
|
from datetime import timedelta
|
2019-08-20 17:43:39 +00:00
|
|
|
from typing import Any, Awaitable, Dict
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
from homeassistant.const import DEVICE_CLASS_BATTERY
|
2019-07-01 17:19:14 +00:00
|
|
|
from homeassistant.util.dt import utc_from_timestamp, utcnow
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
from . import DOMAIN, GeniusEntity
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GH_HAS_BATTERY = ["Room Thermostat", "Genius Valve", "Room Sensor", "Radiator Valve"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-05-21 12:23:38 +00:00
|
|
|
GH_LEVEL_MAPPING = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"error": "Errors",
|
|
|
|
"warning": "Warnings",
|
|
|
|
"information": "Information",
|
2019-05-21 12:23:38 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Set up the Genius Hub sensor entities."""
|
2019-07-31 19:25:30 +00:00
|
|
|
client = hass.data[DOMAIN]["client"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
sensors = [GeniusBattery(d) for d in client.device_objs if d.type in GH_HAS_BATTERY]
|
2019-07-31 19:25:30 +00:00
|
|
|
issues = [GeniusIssue(client, i) for i in list(GH_LEVEL_MAPPING)]
|
2019-05-21 12:23:38 +00:00
|
|
|
|
|
|
|
async_add_entities(sensors + issues, update_before_add=True)
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
class GeniusBattery(GeniusEntity):
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Representation of a Genius Hub sensor."""
|
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
def __init__(self, device) -> None:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-08-20 17:43:39 +00:00
|
|
|
super().__init__()
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
self._device = device
|
2019-09-03 15:10:56 +00:00
|
|
|
self._name = f"{device.type} {device.id}"
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def icon(self) -> str:
|
2019-07-01 17:19:14 +00:00
|
|
|
"""Return the icon of the sensor."""
|
2019-08-20 17:43:39 +00:00
|
|
|
|
|
|
|
values = self._device._raw["childValues"] # pylint: disable=protected-access
|
2019-07-01 17:19:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
last_comms = utc_from_timestamp(values["lastComms"]["val"])
|
|
|
|
if "WakeUp_Interval" in values:
|
|
|
|
interval = timedelta(seconds=values["WakeUp_Interval"]["val"])
|
2019-07-31 17:44:09 +00:00
|
|
|
else:
|
|
|
|
interval = timedelta(minutes=20)
|
2019-07-01 17:19:14 +00:00
|
|
|
|
|
|
|
if last_comms < utcnow() - interval * 3:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:battery-unknown"
|
2019-07-01 17:19:14 +00:00
|
|
|
|
2019-08-04 22:06:36 +00:00
|
|
|
battery_level = self._device.data["state"]["batteryLevel"]
|
2019-07-01 17:19:14 +00:00
|
|
|
if battery_level == 255:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:battery-unknown"
|
2019-07-01 17:19:14 +00:00
|
|
|
if battery_level < 40:
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:battery-alert"
|
2019-07-01 17:19:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
icon = "mdi:battery"
|
2019-07-01 17:19:14 +00:00
|
|
|
if battery_level <= 95:
|
2019-09-04 06:15:40 +00:00
|
|
|
icon += f"-{int(round(battery_level / 10 - 0.01)) * 10}"
|
2019-07-01 17:19:14 +00:00
|
|
|
|
|
|
|
return icon
|
|
|
|
|
2019-05-14 21:30:26 +00:00
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def device_class(self) -> str:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the unit of measurement of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "%"
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def state(self) -> str:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the state of the sensor."""
|
2019-08-04 22:06:36 +00:00
|
|
|
level = self._device.data["state"].get("batteryLevel", 255)
|
2019-05-26 12:01:29 +00:00
|
|
|
return level if level != 255 else 0
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def device_state_attributes(self) -> Dict[str, Any]:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the device state attributes."""
|
|
|
|
attrs = {}
|
2019-08-04 22:06:36 +00:00
|
|
|
attrs["assigned_zone"] = self._device.data["assignedZones"][0]["name"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
last_comms = self._device._raw["childValues"]["lastComms"]["val"]
|
2019-07-31 19:25:30 +00:00
|
|
|
attrs["last_comms"] = utc_from_timestamp(last_comms).isoformat()
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
return {**attrs}
|
2019-05-21 12:23:38 +00:00
|
|
|
|
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
class GeniusIssue(GeniusEntity):
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Representation of a Genius Hub sensor."""
|
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
def __init__(self, hub, level) -> None:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-08-20 17:43:39 +00:00
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self._hub = hub
|
2019-05-21 12:23:38 +00:00
|
|
|
self._name = GH_LEVEL_MAPPING[level]
|
|
|
|
self._level = level
|
|
|
|
self._issues = []
|
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def state(self) -> str:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Return the number of issues."""
|
|
|
|
return len(self._issues)
|
|
|
|
|
|
|
|
@property
|
2019-08-20 17:43:39 +00:00
|
|
|
def device_state_attributes(self) -> Dict[str, Any]:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Return the device state attributes."""
|
2019-09-03 15:10:56 +00:00
|
|
|
return {f"{self._level}_list": self._issues}
|
2019-05-21 12:23:38 +00:00
|
|
|
|
2019-08-20 17:43:39 +00:00
|
|
|
async def async_update(self) -> Awaitable[None]:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Process the sensor's state data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._issues = [
|
|
|
|
i["description"] for i in self._hub.issues if i["level"] == self._level
|
|
|
|
]
|