From 1b7fb9ae12261a1b907cd7eb6f54789a6ee78156 Mon Sep 17 00:00:00 2001 From: ribbal <30695106+ribbal@users.noreply.github.com> Date: Tue, 23 Jul 2024 18:21:58 +0100 Subject: [PATCH] Create additional sensors in Hive integration (#122453) * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add missing sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors * add temperature and mode sensors --- homeassistant/components/hive/icons.json | 9 +++- homeassistant/components/hive/sensor.py | 53 +++++++++++++++++++--- homeassistant/components/hive/strings.json | 18 ++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/hive/icons.json b/homeassistant/components/hive/icons.json index 186724cd563..2704317779c 100644 --- a/homeassistant/components/hive/icons.json +++ b/homeassistant/components/hive/icons.json @@ -6,9 +6,14 @@ }, "hot_water": { "default": "mdi:hand-water" + } + }, + "sensor": { + "heating": { + "default": "mdi:radiator" }, - "temperature": { - "default": "mdi:thermometer" + "hot_water": { + "default": "mdi:hand-water" } } }, diff --git a/homeassistant/components/hive/sensor.py b/homeassistant/components/hive/sensor.py index 4e81b1a5d85..3e594c19058 100644 --- a/homeassistant/components/hive/sensor.py +++ b/homeassistant/components/hive/sensor.py @@ -1,5 +1,7 @@ """Support for the Hive sensors.""" +from collections.abc import Callable +from dataclasses import dataclass from datetime import timedelta from typing import Any @@ -20,6 +22,7 @@ from homeassistant.const import ( ) 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 @@ -27,27 +30,61 @@ from .const import DOMAIN PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) -SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( + +@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, ), - SensorEntityDescription( + HiveSensorEntityDescription( key="Power", native_unit_of_measurement=UnitOfPower.WATT, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.POWER, entity_category=EntityCategory.DIAGNOSTIC, ), - SensorEntityDescription( + 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", "on", "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, + ), ) @@ -73,11 +110,13 @@ async def async_setup_entry( class HiveSensorEntity(HiveEntity, SensorEntity): """Hive Sensor Entity.""" + entity_description: HiveSensorEntityDescription + def __init__( self, hive: Hive, hive_device: dict[str, Any], - entity_description: SensorEntityDescription, + entity_description: HiveSensorEntityDescription, ) -> None: """Initialise hive sensor.""" super().__init__(hive, hive_device) @@ -87,4 +126,6 @@ class HiveSensorEntity(HiveEntity, SensorEntity): """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.device["status"]["state"] + self._attr_native_value = self.entity_description.fn( + self.device["status"]["state"] + ) diff --git a/homeassistant/components/hive/strings.json b/homeassistant/components/hive/strings.json index 277a1aac754..c3252238131 100644 --- a/homeassistant/components/hive/strings.json +++ b/homeassistant/components/hive/strings.json @@ -100,5 +100,23 @@ } } } + }, + "entity": { + "sensor": { + "heating": { + "state": { + "on": "[%key:common::state::on%]", + "off": "[%key:common::state::off%]", + "schedule": "Schedule" + } + }, + "hot_water": { + "state": { + "on": "[%key:common::state::on%]", + "off": "[%key:common::state::off%]", + "schedule": "[%key:component::hive::entity::sensor::heating::state::schedule%]" + } + } + } } }