Use functions to get value and unit in Abode (#99084)

This commit is contained in:
Joost Lekkerkerker 2023-08-29 17:37:26 +02:00 committed by GitHub
parent 10c53dd284
commit b9fd2ee3b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 19 deletions

View File

@ -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)