mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use value_fn in Switcher sensor platform (#143711)
This commit is contained in:
parent
f1b3b0c155
commit
eee18035cf
@ -2,7 +2,17 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from aioswitcher.device import DeviceCategory
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import cast
|
||||
|
||||
from aioswitcher.device import (
|
||||
DeviceCategory,
|
||||
SwitcherBase,
|
||||
SwitcherPowerBase,
|
||||
SwitcherThermostatBase,
|
||||
SwitcherTimedBase,
|
||||
)
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
@ -21,35 +31,48 @@ from .const import SIGNAL_DEVICE_ADD
|
||||
from .coordinator import SwitcherDataUpdateCoordinator
|
||||
from .entity import SwitcherEntity
|
||||
|
||||
POWER_SENSORS: list[SensorEntityDescription] = [
|
||||
SensorEntityDescription(
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class SwitcherSensorEntityDescription(SensorEntityDescription):
|
||||
"""Class to describe a Switcher sensor entity."""
|
||||
|
||||
value_fn: Callable[[SwitcherBase], StateType]
|
||||
|
||||
|
||||
POWER_SENSORS: list[SwitcherSensorEntityDescription] = [
|
||||
SwitcherSensorEntityDescription(
|
||||
key="power_consumption",
|
||||
native_unit_of_measurement=UnitOfPower.WATT,
|
||||
device_class=SensorDeviceClass.POWER,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda data: cast(SwitcherPowerBase, data).power_consumption,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
SwitcherSensorEntityDescription(
|
||||
key="electric_current",
|
||||
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
||||
device_class=SensorDeviceClass.CURRENT,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
value_fn=lambda data: cast(SwitcherPowerBase, data).electric_current,
|
||||
),
|
||||
]
|
||||
TIME_SENSORS: list[SensorEntityDescription] = [
|
||||
SensorEntityDescription(
|
||||
TIME_SENSORS: list[SwitcherSensorEntityDescription] = [
|
||||
SwitcherSensorEntityDescription(
|
||||
key="remaining_time",
|
||||
translation_key="remaining_time",
|
||||
value_fn=lambda data: cast(SwitcherTimedBase, data).remaining_time,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
SwitcherSensorEntityDescription(
|
||||
key="auto_off_set",
|
||||
translation_key="auto_shutdown",
|
||||
entity_registry_enabled_default=False,
|
||||
value_fn=lambda data: cast(SwitcherTimedBase, data).auto_shutdown,
|
||||
),
|
||||
]
|
||||
TEMPERATURE_SENSORS: list[SensorEntityDescription] = [
|
||||
SensorEntityDescription(
|
||||
TEMPERATURE_SENSORS: list[SwitcherSensorEntityDescription] = [
|
||||
SwitcherSensorEntityDescription(
|
||||
key="temperature",
|
||||
translation_key="temperature",
|
||||
value_fn=lambda data: cast(SwitcherThermostatBase, data).temperature,
|
||||
),
|
||||
]
|
||||
|
||||
@ -95,11 +118,11 @@ class SwitcherSensorEntity(SwitcherEntity, SensorEntity):
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: SwitcherDataUpdateCoordinator,
|
||||
description: SensorEntityDescription,
|
||||
description: SwitcherSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self.entity_description: SwitcherSensorEntityDescription = description
|
||||
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}"
|
||||
@ -108,4 +131,4 @@ class SwitcherSensorEntity(SwitcherEntity, SensorEntity):
|
||||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Return value of sensor."""
|
||||
return getattr(self.coordinator.data, self.entity_description.key) # type: ignore[no-any-return]
|
||||
return self.entity_description.value_fn(self.coordinator.data)
|
||||
|
Loading…
x
Reference in New Issue
Block a user