From b9fd2ee3b60a2e3b23fbfefcb02a05d62df1d524 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 29 Aug 2023 17:37:26 +0200 Subject: [PATCH] Use functions to get value and unit in Abode (#99084) --- homeassistant/components/abode/sensor.py | 54 +++++++++++++++--------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/abode/sensor.py b/homeassistant/components/abode/sensor.py index bf885485fc3a..d964655384b7 100644 --- a/homeassistant/components/abode/sensor.py +++ b/homeassistant/components/abode/sensor.py @@ -1,6 +1,8 @@ """Support for Abode Security System sensors.""" from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass from typing import cast from jaraco.abode.devices.sensor import Sensor as AbodeSense @@ -19,18 +21,38 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import AbodeDevice, AbodeSystem from .const import DOMAIN -SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( - SensorEntityDescription( + +@dataclass +class AbodeSensorDescriptionMixin: + """Mixin for Abode sensor.""" + + value_fn: Callable[[AbodeSense], float] + native_unit_of_measurement_fn: Callable[[AbodeSense], str] + + +@dataclass +class AbodeSensorDescription(SensorEntityDescription, AbodeSensorDescriptionMixin): + """Class describing Abode sensor entities.""" + + +SENSOR_TYPES: tuple[AbodeSensorDescription, ...] = ( + AbodeSensorDescription( key=CONST.TEMP_STATUS_KEY, device_class=SensorDeviceClass.TEMPERATURE, + native_unit_of_measurement_fn=lambda device: device.temp_unit, + value_fn=lambda device: cast(float, device.temp), ), - SensorEntityDescription( + AbodeSensorDescription( key=CONST.HUMI_STATUS_KEY, device_class=SensorDeviceClass.HUMIDITY, + native_unit_of_measurement_fn=lambda device: device.humidity_unit, + value_fn=lambda device: cast(float, device.humidity), ), - SensorEntityDescription( + AbodeSensorDescription( key=CONST.LUX_STATUS_KEY, device_class=SensorDeviceClass.ILLUMINANCE, + native_unit_of_measurement_fn=lambda _: LIGHT_LUX, + value_fn=lambda device: cast(float, device.lux), ), ) @@ -52,32 +74,26 @@ async def async_setup_entry( class AbodeSensor(AbodeDevice, SensorEntity): """A sensor implementation for Abode devices.""" + entity_description: AbodeSensorDescription _device: AbodeSense def __init__( self, data: AbodeSystem, device: AbodeSense, - description: SensorEntityDescription, + description: AbodeSensorDescription, ) -> None: """Initialize a sensor for an Abode device.""" super().__init__(data, device) self.entity_description = description self._attr_unique_id = f"{device.uuid}-{description.key}" - if description.key == CONST.TEMP_STATUS_KEY: - self._attr_native_unit_of_measurement = device.temp_unit - elif description.key == CONST.HUMI_STATUS_KEY: - self._attr_native_unit_of_measurement = device.humidity_unit - elif description.key == CONST.LUX_STATUS_KEY: - self._attr_native_unit_of_measurement = LIGHT_LUX @property - def native_value(self) -> float | None: + def native_value(self) -> float: """Return the state of the sensor.""" - if self.entity_description.key == CONST.TEMP_STATUS_KEY: - return cast(float, self._device.temp) - if self.entity_description.key == CONST.HUMI_STATUS_KEY: - return cast(float, self._device.humidity) - if self.entity_description.key == CONST.LUX_STATUS_KEY: - return cast(float, self._device.lux) - return None + return self.entity_description.value_fn(self._device) + + @property + def native_unit_of_measurement(self) -> str: + """Return the native unit of measurement.""" + return self.entity_description.native_unit_of_measurement_fn(self._device)