Split Vallox sensor descriptions into metric and computed sensors (#191598)

This commit is contained in:
Andre Richter
2026-11-10 14:16:41 +01:00
committed by GitHub
parent 7eacffcb83
commit 0742891d2d
2 changed files with 111 additions and 145 deletions
@@ -99,10 +99,7 @@ rules:
test-coverage:
status: todo
comment: >-
Missing for 100%:
(1) sensor.py lines 60 and 67 cannot be reached with data from a real
unit and need an integration cleanup.
(2) services.py line 55 goes away with action-setup.
Missing for 100%: services.py line 55, which goes away with action-setup.
# Gold
devices:
+110 -141
View File
@@ -1,9 +1,12 @@
"""Support for Vallox ventilation unit sensors."""
from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, time
from typing import override
from vallox_websocket_api import MetricData
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@@ -33,17 +36,30 @@ from .coordinator import ValloxConfigEntry, ValloxDataUpdateCoordinator
from .entity import ValloxEntity
@dataclass(frozen=True, kw_only=True)
class ValloxMetricSensorEntityDescription(SensorEntityDescription):
"""Describes a Vallox sensor that reports a single metric."""
metric_key: str
@dataclass(frozen=True, kw_only=True)
class ValloxComputedSensorEntityDescription(SensorEntityDescription):
"""Describes a Vallox sensor whose value is computed from the metrics."""
value_fn: Callable[[MetricData], StateType | datetime]
class ValloxSensorEntity(ValloxEntity, SensorEntity):
"""Representation of a Vallox sensor."""
entity_description: ValloxSensorEntityDescription
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(
self,
name: str,
coordinator: ValloxDataUpdateCoordinator,
description: ValloxSensorEntityDescription,
description: SensorEntityDescription,
) -> None:
"""Initialize the Vallox sensor."""
super().__init__(name, coordinator)
@@ -52,155 +68,59 @@ class ValloxSensorEntity(ValloxEntity, SensorEntity):
self._attr_unique_id = f"{self._device_uuid}-{description.key}"
@property
@override
def native_value(self) -> StateType | datetime:
"""Return the value reported by the sensor."""
if (metric_key := self.entity_description.metric_key) is None:
return None
value = self.coordinator.data.get(metric_key)
class ValloxMetricSensorEntity(ValloxSensorEntity):
"""Representation of a Vallox sensor that reports a single metric."""
if self.entity_description.round_ndigits is not None and isinstance(
value, float
):
value = round(value, self.entity_description.round_ndigits)
return value
class ValloxProfileSensor(ValloxSensorEntity):
"""Child class for profile reporting."""
entity_description: ValloxMetricSensorEntityDescription
@property
@override
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
vallox_profile = self.coordinator.data.profile
return VALLOX_PROFILE_TO_PRESET_MODE.get(vallox_profile)
return self.coordinator.data.get(self.entity_description.metric_key)
# There is a quirk with respect to the fan speed
# reporting. The device keeps on reporting the last valid
# fan speed from when the device was in regular operation
# mode, even if it left that state and has been shut off
# in the meantime.
#
# Therefore, first query the overall state of the device,
# and report zero percent fan speed in case
# it is not in regular operation mode.
class ValloxFanSpeedSensor(ValloxSensorEntity):
"""Child class for fan speed reporting."""
class ValloxComputedSensorEntity(ValloxSensorEntity):
"""Representation of a Vallox sensor whose value is computed from the metrics."""
entity_description: ValloxComputedSensorEntityDescription
@property
@override
def native_value(self) -> StateType | datetime:
"""Return the value reported by the sensor."""
if (mode := self.coordinator.data.get(METRIC_KEY_MODE)) is None:
return None
return super().native_value if mode == MODE_ON else 0
return self.entity_description.value_fn(self.coordinator.data)
class ValloxFilterRemainingSensor(ValloxSensorEntity):
"""Child class for filter remaining time reporting."""
@property
@override
def native_value(self) -> StateType | datetime:
"""Return the value reported by the sensor."""
next_filter_change_date = self.coordinator.data.next_filter_change_date
if next_filter_change_date is None:
return None
return datetime.combine(
next_filter_change_date,
time(hour=13, minute=0, second=0, tzinfo=dt_util.get_default_time_zone()),
)
def _fan_speed(data: MetricData, metric_key: str) -> StateType:
"""Return a fan speed, or 0 while the unit is off."""
if (mode := data.get(METRIC_KEY_MODE)) is None:
return None
# The unit keeps reporting its last fan speeds after it is turned off.
return data.get(metric_key) if mode == MODE_ON else 0
class ValloxCellStateSensor(ValloxSensorEntity):
"""Child class for cell state reporting."""
def _next_filter_change(data: MetricData) -> datetime | None:
"""Return when the filter is due for a change."""
if (next_filter_change_date := data.next_filter_change_date) is None:
return None
@property
@override
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
super_native_value = super().native_value
if not isinstance(super_native_value, int):
return None
return VALLOX_CELL_STATE_TO_STR.get(super_native_value)
return datetime.combine(
next_filter_change_date,
time(hour=13, minute=0, second=0, tzinfo=dt_util.get_default_time_zone()),
)
class ValloxProfileDurationSensor(ValloxSensorEntity):
"""Child class for profile duration reporting."""
@property
@override
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
return self.coordinator.data.get_remaining_profile_duration(
self.coordinator.data.profile
)
def _cell_state(data: MetricData) -> str | None:
"""Return the name of the cell state."""
if not isinstance(cell_state := data.get("A_CYC_CELL_STATE"), int):
return None
return VALLOX_CELL_STATE_TO_STR.get(cell_state)
@dataclass(frozen=True)
class ValloxSensorEntityDescription(SensorEntityDescription):
"""Describes Vallox sensor entity."""
metric_key: str | None = None
entity_type: type[ValloxSensorEntity] = ValloxSensorEntity
round_ndigits: int | None = None
SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
ValloxSensorEntityDescription(
key="current_profile",
translation_key="current_profile",
entity_type=ValloxProfileSensor,
),
ValloxSensorEntityDescription(
key="fan_speed",
translation_key="fan_speed",
metric_key="A_CYC_FAN_SPEED",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfRatio.PERCENTAGE,
entity_type=ValloxFanSpeedSensor,
),
ValloxSensorEntityDescription(
key="extract_fan_speed",
translation_key="extract_fan_speed",
metric_key="A_CYC_EXTR_FAN_SPEED",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
entity_type=ValloxFanSpeedSensor,
entity_registry_enabled_default=False,
),
ValloxSensorEntityDescription(
key="supply_fan_speed",
translation_key="supply_fan_speed",
metric_key="A_CYC_SUPP_FAN_SPEED",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
entity_type=ValloxFanSpeedSensor,
entity_registry_enabled_default=False,
),
ValloxSensorEntityDescription(
key="remaining_time_for_filter",
translation_key="remaining_time_for_filter",
device_class=SensorDeviceClass.TIMESTAMP,
entity_type=ValloxFilterRemainingSensor,
),
ValloxSensorEntityDescription(
key="cell_state",
translation_key="cell_state",
metric_key="A_CYC_CELL_STATE",
entity_type=ValloxCellStateSensor,
),
ValloxSensorEntityDescription(
METRIC_SENSOR_ENTITIES: tuple[ValloxMetricSensorEntityDescription, ...] = (
ValloxMetricSensorEntityDescription(
key="extract_air",
translation_key="extract_air",
metric_key="A_CYC_TEMP_EXTRACT_AIR",
@@ -208,7 +128,7 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="exhaust_air",
translation_key="exhaust_air",
metric_key="A_CYC_TEMP_EXHAUST_AIR",
@@ -216,7 +136,7 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="outdoor_air",
translation_key="outdoor_air",
metric_key="A_CYC_TEMP_OUTDOOR_AIR",
@@ -224,7 +144,7 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="supply_air",
translation_key="supply_air",
metric_key="A_CYC_TEMP_SUPPLY_AIR",
@@ -232,7 +152,7 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="supply_cell_air",
translation_key="supply_cell_air",
metric_key="A_CYC_TEMP_SUPPLY_CELL_AIR",
@@ -240,7 +160,7 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="optional_air",
translation_key="optional_air",
metric_key="A_CYC_TEMP_OPTIONAL",
@@ -249,23 +169,22 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
entity_registry_enabled_default=False,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="humidity",
metric_key="A_CYC_RH_VALUE",
device_class=SensorDeviceClass.HUMIDITY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfRatio.PERCENTAGE,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="efficiency",
translation_key="efficiency",
metric_key="A_CYC_EXTRACT_EFFICIENCY",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfRatio.PERCENTAGE,
entity_registry_enabled_default=False,
round_ndigits=0,
),
ValloxSensorEntityDescription(
ValloxMetricSensorEntityDescription(
key="co2",
metric_key="A_CYC_CO2_VALUE",
device_class=SensorDeviceClass.CO2,
@@ -273,13 +192,55 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = (
native_unit_of_measurement=UnitOfRatio.PARTS_PER_MILLION,
entity_registry_enabled_default=False,
),
ValloxSensorEntityDescription(
)
COMPUTED_SENSOR_ENTITIES: tuple[ValloxComputedSensorEntityDescription, ...] = (
ValloxComputedSensorEntityDescription(
key="current_profile",
translation_key="current_profile",
value_fn=lambda data: VALLOX_PROFILE_TO_PRESET_MODE.get(data.profile),
),
ValloxComputedSensorEntityDescription(
key="fan_speed",
translation_key="fan_speed",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfRatio.PERCENTAGE,
value_fn=lambda data: _fan_speed(data, "A_CYC_FAN_SPEED"),
),
ValloxComputedSensorEntityDescription(
key="extract_fan_speed",
translation_key="extract_fan_speed",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
entity_registry_enabled_default=False,
value_fn=lambda data: _fan_speed(data, "A_CYC_EXTR_FAN_SPEED"),
),
ValloxComputedSensorEntityDescription(
key="supply_fan_speed",
translation_key="supply_fan_speed",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
entity_registry_enabled_default=False,
value_fn=lambda data: _fan_speed(data, "A_CYC_SUPP_FAN_SPEED"),
),
ValloxComputedSensorEntityDescription(
key="remaining_time_for_filter",
translation_key="remaining_time_for_filter",
device_class=SensorDeviceClass.TIMESTAMP,
value_fn=_next_filter_change,
),
ValloxComputedSensorEntityDescription(
key="cell_state",
translation_key="cell_state",
value_fn=_cell_state,
),
ValloxComputedSensorEntityDescription(
key="profile_duration",
translation_key="profile_duration",
device_class=SensorDeviceClass.DURATION,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
entity_type=ValloxProfileDurationSensor,
value_fn=lambda data: data.get_remaining_profile_duration(data.profile),
),
)
@@ -294,6 +255,14 @@ async def async_setup_entry(
coordinator = entry.runtime_data
async_add_entities(
description.entity_type(name, coordinator, description)
for description in SENSOR_ENTITIES
[
*(
ValloxMetricSensorEntity(name, coordinator, description)
for description in METRIC_SENSOR_ENTITIES
),
*(
ValloxComputedSensorEntity(name, coordinator, description)
for description in COMPUTED_SENSOR_ENTITIES
),
]
)