From fd09476b28fb2739f5c62b879ad734b89752a95e Mon Sep 17 00:00:00 2001 From: markhannon Date: Thu, 15 May 2025 18:12:18 +1000 Subject: [PATCH] Add sensor entity to Zimi integration (#144329) * Import sensor.py * Light design alignment * Fix merge error * Refactor with extend * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck * value_fn and inline refactoring * strings.json and translation_key * Add sensor_name * Revert "Add sensor_name" This reverts commit ad3da048e9c5a6ecdb15052c253de7dc46b1120f. * Default naming for sensors * Remove uneeded 'garage' and use default battery name * Bump to zcc-helper 3.5.2 which maps "Garage Controller" tp "Garage" in device.name * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck * Update homeassistant/components/zimi/sensor.py Co-authored-by: Josef Zweck * Update strings.json * Revert "Bump to zcc-helper 3.5.2 which maps "Garage Controller" tp "Garage" in device.name" This reverts commit 345ef8a4859c8d0e188462c9a69a4fab8f284b69. * Update homeassistant/components/zimi/sensor.py --------- Co-authored-by: Josef Zweck --- homeassistant/components/zimi/__init__.py | 2 +- homeassistant/components/zimi/entity.py | 7 +- homeassistant/components/zimi/sensor.py | 103 +++++++++++++++++++++ homeassistant/components/zimi/strings.json | 7 ++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 homeassistant/components/zimi/sensor.py diff --git a/homeassistant/components/zimi/__init__.py b/homeassistant/components/zimi/__init__.py index ab52c1491e1..a184ba71a52 100644 --- a/homeassistant/components/zimi/__init__.py +++ b/homeassistant/components/zimi/__init__.py @@ -16,7 +16,7 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from .const import DOMAIN from .helpers import async_connect_to_controller -PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SWITCH] +PLATFORMS = [Platform.FAN, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/zimi/entity.py b/homeassistant/components/zimi/entity.py index 68911992014..12d8f336bf0 100644 --- a/homeassistant/components/zimi/entity.py +++ b/homeassistant/components/zimi/entity.py @@ -21,7 +21,9 @@ class ZimiEntity(Entity): _attr_should_poll = False _attr_has_entity_name = True - def __init__(self, device: ControlPointDevice, api: ControlPoint) -> None: + def __init__( + self, device: ControlPointDevice, api: ControlPoint, use_device_name=True + ) -> None: """Initialize an HA Entity which is a ZimiDevice.""" self._device = device @@ -36,7 +38,8 @@ class ZimiEntity(Entity): suggested_area=device.room, via_device=(DOMAIN, api.mac), ) - self._attr_name = device.name.strip() + if use_device_name: + self._attr_name = device.name.strip() self._attr_suggested_area = device.room @property diff --git a/homeassistant/components/zimi/sensor.py b/homeassistant/components/zimi/sensor.py new file mode 100644 index 00000000000..2c681f8e69e --- /dev/null +++ b/homeassistant/components/zimi/sensor.py @@ -0,0 +1,103 @@ +"""Platform for sensor integration.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +import logging + +from zcc import ControlPoint +from zcc.device import ControlPointDevice + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, +) +from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.helpers.typing import StateType + +from . import ZimiConfigEntry +from .entity import ZimiEntity + + +@dataclass(frozen=True, kw_only=True) +class ZimiSensorEntityDescription(SensorEntityDescription): + """Class describing Zimi sensor entities.""" + + value_fn: Callable[[ControlPointDevice], StateType] + + +GARAGE_SENSOR_DESCRIPTIONS: tuple[ZimiSensorEntityDescription, ...] = ( + ZimiSensorEntityDescription( + key="door_temperature", + translation_key="door_temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + value_fn=lambda device: device.door_temp, + ), + ZimiSensorEntityDescription( + key="garage_battery", + native_unit_of_measurement=PERCENTAGE, + entity_category=EntityCategory.DIAGNOSTIC, + device_class=SensorDeviceClass.BATTERY, + value_fn=lambda device: device.battery_level, + ), + ZimiSensorEntityDescription( + key="garage_temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + value_fn=lambda device: device.garage_temp, + ), + ZimiSensorEntityDescription( + key="garage_humidty", + native_unit_of_measurement=PERCENTAGE, + device_class=SensorDeviceClass.HUMIDITY, + value_fn=lambda device: device.garage_humidity, + ), +) + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ZimiConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Zimi Sensor platform.""" + + api = config_entry.runtime_data + + async_add_entities( + ZimiSensor(device, description, api) + for device in api.sensors + for description in GARAGE_SENSOR_DESCRIPTIONS + ) + + +class ZimiSensor(ZimiEntity, SensorEntity): + """Representation of a Zimi sensor.""" + + entity_description: ZimiSensorEntityDescription + + def __init__( + self, + device: ControlPointDevice, + description: ZimiSensorEntityDescription, + api: ControlPoint, + ) -> None: + """Initialize an ZimiSensor with specified type.""" + + super().__init__(device, api, use_device_name=False) + + self.entity_description = description + self._attr_unique_id = device.identifier + "." + self.entity_description.key + + @property + def native_value(self) -> StateType: + """Return the state of the sensor.""" + + return self.entity_description.value_fn(self._device) diff --git a/homeassistant/components/zimi/strings.json b/homeassistant/components/zimi/strings.json index 530eb86ef05..e1c7944b25a 100644 --- a/homeassistant/components/zimi/strings.json +++ b/homeassistant/components/zimi/strings.json @@ -42,5 +42,12 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } + }, + "entity": { + "sensor": { + "door_temperature": { + "name": "Outside temperature" + } + } } }