core/homeassistant/components/hive/sensor.py

132 lines
4.0 KiB
Python
Raw Normal View History

2021-06-11 11:35:03 +00:00
"""Support for the Hive sensors."""
from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
2024-07-21 14:21:45 +00:00
from typing import Any
from apyhiveapi import Hive
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
2022-06-29 10:39:35 +00:00
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
PERCENTAGE,
EntityCategory,
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import HiveEntity
from .const import DOMAIN
PARALLEL_UPDATES = 0
SCAN_INTERVAL = timedelta(seconds=15)
@dataclass(frozen=True)
class HiveSensorEntityDescription(SensorEntityDescription):
"""Describes Hive sensor entity."""
fn: Callable[[StateType], StateType] = lambda x: x
SENSOR_TYPES: tuple[HiveSensorEntityDescription, ...] = (
HiveSensorEntityDescription(
key="Battery",
native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.BATTERY,
entity_category=EntityCategory.DIAGNOSTIC,
),
HiveSensorEntityDescription(
2022-06-29 10:39:35 +00:00
key="Power",
native_unit_of_measurement=UnitOfPower.WATT,
2022-06-29 10:39:35 +00:00
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.POWER,
entity_category=EntityCategory.DIAGNOSTIC,
2022-06-29 10:39:35 +00:00
),
HiveSensorEntityDescription(
key="Current_Temperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
entity_category=EntityCategory.DIAGNOSTIC,
),
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,
options=["schedule", "manual", "off"],
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,
),
)
2018-01-15 22:24:12 +00:00
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Hive thermostat based on a config entry."""
hive = hass.data[DOMAIN][entry.entry_id]
devices = hive.session.deviceList.get("sensor")
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
class HiveSensorEntity(HiveEntity, SensorEntity):
2018-01-15 22:24:12 +00:00
"""Hive Sensor Entity."""
entity_description: HiveSensorEntityDescription
2024-07-21 14:21:45 +00:00
def __init__(
self,
hive: Hive,
hive_device: dict[str, Any],
entity_description: HiveSensorEntityDescription,
2024-07-21 14:21:45 +00:00
) -> None:
"""Initialise hive sensor."""
super().__init__(hive, hive_device)
self.entity_description = entity_description
2022-08-30 18:55:01 +00:00
async def async_update(self) -> None:
"""Update all Node data from Hive."""
await self.hive.session.updateData(self.device)
self.device = await self.hive.sensor.getSensor(self.device)
self._attr_native_value = self.entity_description.fn(
self.device["status"]["state"]
)