2021-06-11 11:35:03 +00:00
|
|
|
"""Support for the Hive sensors."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2024-07-23 17:21:58 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
2021-02-09 21:03:49 +00:00
|
|
|
from datetime import timedelta
|
2024-07-21 14:21:45 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from apyhiveapi import Hive
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-05-29 10:08:50 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2022-06-29 10:39:35 +00:00
|
|
|
SensorStateClass,
|
2022-05-29 10:08:50 +00:00
|
|
|
)
|
2022-01-03 18:13:59 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-02-20 09:57:19 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
PERCENTAGE,
|
|
|
|
EntityCategory,
|
|
|
|
UnitOfPower,
|
|
|
|
UnitOfTemperature,
|
|
|
|
)
|
2022-01-03 18:13:59 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2024-07-23 17:21:58 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-02-09 21:03:49 +00:00
|
|
|
|
2021-03-15 11:27:10 +00:00
|
|
|
from . import HiveEntity
|
|
|
|
from .const import DOMAIN
|
2019-02-14 15:01:46 +00:00
|
|
|
|
2021-02-09 21:03:49 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=15)
|
2022-05-29 10:08:50 +00:00
|
|
|
|
2024-07-23 17:21:58 +00:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class HiveSensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Describes Hive sensor entity."""
|
|
|
|
|
|
|
|
fn: Callable[[StateType], StateType] = lambda x: x
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES: tuple[HiveSensorEntityDescription, ...] = (
|
|
|
|
HiveSensorEntityDescription(
|
2022-05-29 10:08:50 +00:00
|
|
|
key="Battery",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
2022-07-02 12:33:10 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2022-05-29 10:08:50 +00:00
|
|
|
),
|
2024-07-23 17:21:58 +00:00
|
|
|
HiveSensorEntityDescription(
|
2022-06-29 10:39:35 +00:00
|
|
|
key="Power",
|
2022-12-12 10:41:58 +00:00
|
|
|
native_unit_of_measurement=UnitOfPower.WATT,
|
2022-06-29 10:39:35 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.POWER,
|
2022-07-02 12:33:10 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2022-06-29 10:39:35 +00:00
|
|
|
),
|
2024-07-23 17:21:58 +00:00
|
|
|
HiveSensorEntityDescription(
|
2024-02-20 09:57:19 +00:00
|
|
|
key="Current_Temperature",
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
),
|
2024-07-23 17:21:58 +00:00
|
|
|
HiveSensorEntityDescription(
|
|
|
|
key="Heating_Current_Temperature",
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
|
|
),
|
|
|
|
HiveSensorEntityDescription(
|
|
|
|
key="Heating_Target_Temperature",
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
|
|
),
|
|
|
|
HiveSensorEntityDescription(
|
|
|
|
key="Heating_Mode",
|
|
|
|
device_class=SensorDeviceClass.ENUM,
|
2024-07-24 06:50:44 +00:00
|
|
|
options=["schedule", "manual", "off"],
|
2024-07-23 17:21:58 +00:00
|
|
|
translation_key="heating",
|
|
|
|
fn=lambda x: x.lower() if isinstance(x, str) else None,
|
|
|
|
),
|
|
|
|
HiveSensorEntityDescription(
|
|
|
|
key="Hotwater_Mode",
|
|
|
|
device_class=SensorDeviceClass.ENUM,
|
|
|
|
options=["schedule", "on", "off"],
|
|
|
|
translation_key="hot_water",
|
|
|
|
fn=lambda x: x.lower() if isinstance(x, str) else None,
|
|
|
|
),
|
2022-05-29 10:08:50 +00:00
|
|
|
)
|
2018-04-16 19:00:13 +00:00
|
|
|
|
2018-01-15 22:24:12 +00:00
|
|
|
|
2022-01-03 18:13:59 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2021-03-15 11:27:10 +00:00
|
|
|
"""Set up Hive thermostat based on a config entry."""
|
|
|
|
hive = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
devices = hive.session.deviceList.get("sensor")
|
2024-03-13 16:25:27 +00:00
|
|
|
if not devices:
|
|
|
|
return
|
|
|
|
async_add_entities(
|
|
|
|
(
|
|
|
|
HiveSensorEntity(hive, dev, description)
|
|
|
|
for dev in devices
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if dev["hiveType"] == description.key
|
|
|
|
),
|
|
|
|
True,
|
|
|
|
)
|
2018-01-15 22:24:12 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class HiveSensorEntity(HiveEntity, SensorEntity):
|
2018-01-15 22:24:12 +00:00
|
|
|
"""Hive Sensor Entity."""
|
|
|
|
|
2024-07-23 17:21:58 +00:00
|
|
|
entity_description: HiveSensorEntityDescription
|
|
|
|
|
2024-07-21 14:21:45 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hive: Hive,
|
|
|
|
hive_device: dict[str, Any],
|
2024-07-23 17:21:58 +00:00
|
|
|
entity_description: HiveSensorEntityDescription,
|
2024-07-21 14:21:45 +00:00
|
|
|
) -> None:
|
2022-05-29 10:08:50 +00:00
|
|
|
"""Initialise hive sensor."""
|
|
|
|
super().__init__(hive, hive_device)
|
|
|
|
self.entity_description = entity_description
|
2018-04-16 19:00:13 +00:00
|
|
|
|
2022-08-30 18:55:01 +00:00
|
|
|
async def async_update(self) -> None:
|
2018-05-13 10:09:28 +00:00
|
|
|
"""Update all Node data from Hive."""
|
2021-02-09 21:03:49 +00:00
|
|
|
await self.hive.session.updateData(self.device)
|
2021-03-15 11:27:10 +00:00
|
|
|
self.device = await self.hive.sensor.getSensor(self.device)
|
2024-07-23 17:21:58 +00:00
|
|
|
self._attr_native_value = self.entity_description.fn(
|
|
|
|
self.device["status"]["state"]
|
|
|
|
)
|