From 30a3e9af2bfefa1a4c6c9720743e358f52dff5b6 Mon Sep 17 00:00:00 2001 From: treetip Date: Wed, 26 Jun 2024 16:54:13 +0300 Subject: [PATCH] Add profile duration sensor for Vallox integration (#120240) --- homeassistant/components/vallox/sensor.py | 21 +++++++++ homeassistant/components/vallox/strings.json | 3 ++ tests/components/vallox/conftest.py | 6 +-- tests/components/vallox/test_sensor.py | 45 ++++++++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/vallox/sensor.py b/homeassistant/components/vallox/sensor.py index 281bc002f68..0bb509a9c5a 100644 --- a/homeassistant/components/vallox/sensor.py +++ b/homeassistant/components/vallox/sensor.py @@ -18,6 +18,7 @@ from homeassistant.const import ( REVOLUTIONS_PER_MINUTE, EntityCategory, UnitOfTemperature, + UnitOfTime, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -127,6 +128,18 @@ class ValloxCellStateSensor(ValloxSensorEntity): return VALLOX_CELL_STATE_TO_STR.get(super_native_value) +class ValloxProfileDurationSensor(ValloxSensorEntity): + """Child class for profile duration reporting.""" + + @property + def native_value(self) -> StateType: + """Return the value reported by the sensor.""" + + return self.coordinator.data.get_remaining_profile_duration( + self.coordinator.data.profile + ) + + @dataclass(frozen=True) class ValloxSensorEntityDescription(SensorEntityDescription): """Describes Vallox sensor entity.""" @@ -253,6 +266,14 @@ SENSOR_ENTITIES: tuple[ValloxSensorEntityDescription, ...] = ( native_unit_of_measurement=CONCENTRATION_PARTS_PER_MILLION, entity_registry_enabled_default=False, ), + ValloxSensorEntityDescription( + key="profile_duration", + translation_key="profile_duration", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTime.MINUTES, + entity_type=ValloxProfileDurationSensor, + ), ) diff --git a/homeassistant/components/vallox/strings.json b/homeassistant/components/vallox/strings.json index 072b59b78e0..4df57b81bb5 100644 --- a/homeassistant/components/vallox/strings.json +++ b/homeassistant/components/vallox/strings.json @@ -87,6 +87,9 @@ }, "efficiency": { "name": "Efficiency" + }, + "profile_duration": { + "name": "Profile duration" } }, "switch": { diff --git a/tests/components/vallox/conftest.py b/tests/components/vallox/conftest.py index 9f65734b926..a6ea95944b3 100644 --- a/tests/components/vallox/conftest.py +++ b/tests/components/vallox/conftest.py @@ -112,9 +112,9 @@ def default_metrics(): "A_CYC_UUID5": 10, "A_CYC_UUID6": 11, "A_CYC_UUID7": 12, - "A_CYC_BOOST_TIMER": 30, - "A_CYC_FIREPLACE_TIMER": 30, - "A_CYC_EXTRA_TIMER": 30, + "A_CYC_BOOST_TIMER": 0, + "A_CYC_FIREPLACE_TIMER": 0, + "A_CYC_EXTRA_TIMER": 0, "A_CYC_MODE": 0, "A_CYC_STATE": 0, "A_CYC_FILTER_CHANGED_YEAR": 24, diff --git a/tests/components/vallox/test_sensor.py b/tests/components/vallox/test_sensor.py index d7af7bbb576..dd8d8026d06 100644 --- a/tests/components/vallox/test_sensor.py +++ b/tests/components/vallox/test_sensor.py @@ -135,3 +135,48 @@ async def test_cell_state_sensor( # Assert sensor = hass.states.get("sensor.vallox_cell_state") assert sensor.state == expected_state + + +@pytest.mark.parametrize( + ("metrics", "expected_state"), + [ + ( + {"A_CYC_STATE": 0}, + "unknown", + ), + ( + {"A_CYC_STATE": 1}, + "unknown", + ), + ( + {"A_CYC_EXTRA_TIMER": 10}, + "10", + ), + ( + {"A_CYC_FIREPLACE_TIMER": 9}, + "9", + ), + ( + {"A_CYC_BOOST_TIMER": 8}, + "8", + ), + ], +) +async def test_profile_duration_sensor( + metrics, + expected_state, + mock_entry: MockConfigEntry, + hass: HomeAssistant, + setup_fetch_metric_data_mock, +) -> None: + """Test profile sensor in different states.""" + # Arrange + setup_fetch_metric_data_mock(metrics=metrics) + + # Act + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + + # Assert + sensor = hass.states.get("sensor.vallox_profile_duration") + assert sensor.state == expected_state