2019-05-14 21:30:26 +00:00
|
|
|
"""Support for Genius Hub sensor devices."""
|
2021-03-18 07:02:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-07-01 17:19:14 +00:00
|
|
|
from datetime import timedelta
|
2021-03-18 07:02:55 +00:00
|
|
|
from typing import Any
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2021-12-13 16:39:38 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
|
|
|
from homeassistant.const import PERCENTAGE
|
2021-04-23 08:34:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-01-07 15:48:54 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-10-02 16:27:13 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
from . import DOMAIN, GeniusDevice, GeniusEntity
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
GH_STATE_ATTR = "batteryLevel"
|
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-10-02 16:27:13 +00:00
|
|
|
async def async_setup_platform(
|
2022-01-07 15:48:54 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
2019-10-02 16:27:13 +00:00
|
|
|
) -> None:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Set up the Genius Hub sensor entities."""
|
2019-10-02 16:27:13 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
broker = hass.data[DOMAIN]["broker"]
|
|
|
|
|
2022-02-23 08:55:26 +00:00
|
|
|
entities: list[GeniusBattery | GeniusIssue] = [
|
2019-10-02 16:27:13 +00:00
|
|
|
GeniusBattery(broker, d, GH_STATE_ATTR)
|
|
|
|
for d in broker.client.device_objs
|
|
|
|
if GH_STATE_ATTR in d.data["state"]
|
|
|
|
]
|
2022-02-23 08:55:26 +00:00
|
|
|
entities.extend([GeniusIssue(broker, i) for i in list(GH_LEVEL_MAPPING)])
|
2019-05-21 12:23:38 +00:00
|
|
|
|
2022-02-23 08:55:26 +00:00
|
|
|
async_add_entities(entities, update_before_add=True)
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class GeniusBattery(GeniusDevice, SensorEntity):
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Representation of a Genius Hub sensor."""
|
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
def __init__(self, broker, device, state_attr) -> None:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-10-02 16:27:13 +00:00
|
|
|
super().__init__(broker, device)
|
|
|
|
|
|
|
|
self._state_attr = state_attr
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2022-02-23 08:55:26 +00:00
|
|
|
self._attr_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-10-02 16:27:13 +00:00
|
|
|
if "_state" in self._device.data: # only for v3 API
|
|
|
|
interval = timedelta(
|
|
|
|
seconds=self._device.data["_state"].get("wakeupInterval", 30 * 60)
|
|
|
|
)
|
2022-02-23 08:55:26 +00:00
|
|
|
if (
|
|
|
|
not self._last_comms
|
|
|
|
or self._last_comms < dt_util.utcnow() - interval * 3
|
|
|
|
):
|
2019-10-02 16:27:13 +00:00
|
|
|
return "mdi:battery-unknown"
|
|
|
|
|
|
|
|
battery_level = self._device.data["state"][self._state_attr]
|
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
|
2022-11-29 19:31:25 +00:00
|
|
|
def device_class(self) -> SensorDeviceClass:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the device class of the sensor."""
|
2021-12-13 16:39:38 +00:00
|
|
|
return SensorDeviceClass.BATTERY
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 12:23:56 +00:00
|
|
|
def native_unit_of_measurement(self) -> str:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the unit of measurement of the sensor."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 12:23:56 +00:00
|
|
|
def native_value(self) -> str:
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Return the state of the sensor."""
|
2019-10-02 16:27:13 +00:00
|
|
|
level = self._device.data["state"][self._state_attr]
|
2019-05-26 12:01:29 +00:00
|
|
|
return level if level != 255 else 0
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-05-21 12:23:38 +00:00
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class GeniusIssue(GeniusEntity, SensorEntity):
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Representation of a Genius Hub sensor."""
|
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
def __init__(self, broker, level) -> None:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-08-20 17:43:39 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2019-10-02 16:27:13 +00:00
|
|
|
self._hub = broker.client
|
2019-10-19 19:27:15 +00:00
|
|
|
self._unique_id = f"{broker.hub_uid}_{GH_LEVEL_MAPPING[level]}"
|
|
|
|
|
2022-02-23 08:55:26 +00:00
|
|
|
self._attr_name = f"GeniusHub {GH_LEVEL_MAPPING[level]}"
|
2019-05-21 12:23:38 +00:00
|
|
|
self._level = level
|
2022-02-23 08:55:26 +00:00
|
|
|
self._issues: list = []
|
2019-05-21 12:23:38 +00:00
|
|
|
|
|
|
|
@property
|
2022-02-23 08:55:26 +00:00
|
|
|
def native_value(self) -> int:
|
2019-05-21 12:23:38 +00:00
|
|
|
"""Return the number of issues."""
|
|
|
|
return len(self._issues)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 07:02:55 +00:00
|
|
|
def extra_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-10-02 16:27:13 +00:00
|
|
|
async def async_update(self) -> 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
|
|
|
|
]
|