Skip to content

Commit 6351c33

Browse files
Matter OperationalState CountdownTime (home-assistant#147705)
Co-authored-by: Martin Hjelmare <[email protected]>
1 parent 2ea20ee commit 6351c33

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

homeassistant/components/matter/sensor.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass, field
6-
from datetime import datetime
6+
from datetime import datetime, timedelta
77
from typing import TYPE_CHECKING, cast
88

99
from chip.clusters import Objects as clusters
@@ -44,7 +44,7 @@
4444
)
4545
from homeassistant.core import HomeAssistant, callback
4646
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
47-
from homeassistant.util import slugify
47+
from homeassistant.util import dt as dt_util, slugify
4848

4949
from .entity import MatterEntity, MatterEntityDescription
5050
from .helpers import get_matter
@@ -942,6 +942,21 @@ def _update_from_device(self) -> None:
942942
# don't discover this entry if the supported state list is empty
943943
secondary_value_is_not=[],
944944
),
945+
MatterDiscoverySchema(
946+
platform=Platform.SENSOR,
947+
entity_description=MatterSensorEntityDescription(
948+
key="OperationalStateCountdownTime",
949+
translation_key="estimated_end_time",
950+
device_class=SensorDeviceClass.TIMESTAMP,
951+
state_class=None,
952+
# Add countdown to current datetime to get the estimated end time
953+
device_to_ha=(
954+
lambda x: dt_util.utcnow() + timedelta(seconds=x) if x > 0 else None
955+
),
956+
),
957+
entity_class=MatterSensor,
958+
required_attributes=(clusters.OperationalState.Attributes.CountdownTime,),
959+
),
945960
MatterDiscoverySchema(
946961
platform=Platform.SENSOR,
947962
entity_description=MatterListSensorEntityDescription(

homeassistant/components/matter/strings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@
318318
"docked": "Docked"
319319
}
320320
},
321+
"estimated_end_time": {
322+
"name": "Estimated end time"
323+
},
321324
"switch_current_position": {
322325
"name": "Current switch position"
323326
},

tests/components/matter/snapshots/test_sensor.ambr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,6 +3443,55 @@
34433443
'state': '1.3',
34443444
})
34453445
# ---
3446+
# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-entry]
3447+
EntityRegistryEntrySnapshot({
3448+
'aliases': set({
3449+
}),
3450+
'area_id': None,
3451+
'capabilities': None,
3452+
'config_entry_id': <ANY>,
3453+
'config_subentry_id': <ANY>,
3454+
'device_class': None,
3455+
'device_id': <ANY>,
3456+
'disabled_by': None,
3457+
'domain': 'sensor',
3458+
'entity_category': None,
3459+
'entity_id': 'sensor.microwave_oven_estimated_end_time',
3460+
'has_entity_name': True,
3461+
'hidden_by': None,
3462+
'icon': None,
3463+
'id': <ANY>,
3464+
'labels': set({
3465+
}),
3466+
'name': None,
3467+
'options': dict({
3468+
}),
3469+
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
3470+
'original_icon': None,
3471+
'original_name': 'Estimated end time',
3472+
'platform': 'matter',
3473+
'previous_unique_id': None,
3474+
'suggested_object_id': None,
3475+
'supported_features': 0,
3476+
'translation_key': 'estimated_end_time',
3477+
'unique_id': '00000000000004D2-000000000000009D-MatterNodeDevice-1-OperationalStateCountdownTime-96-2',
3478+
'unit_of_measurement': None,
3479+
})
3480+
# ---
3481+
# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-state]
3482+
StateSnapshot({
3483+
'attributes': ReadOnlyDict({
3484+
'device_class': 'timestamp',
3485+
'friendly_name': 'Microwave Oven Estimated end time',
3486+
}),
3487+
'context': <ANY>,
3488+
'entity_id': 'sensor.microwave_oven_estimated_end_time',
3489+
'last_changed': <ANY>,
3490+
'last_reported': <ANY>,
3491+
'last_updated': <ANY>,
3492+
'state': '2025-01-01T14:00:30+00:00',
3493+
})
3494+
# ---
34463495
# name: test_sensors[microwave_oven][sensor.microwave_oven_operational_state-entry]
34473496
EntityRegistryEntrySnapshot({
34483497
'aliases': set({

tests/components/matter/test_sensor.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818

1919

20+
@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00")
2021
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "matter_devices")
2122
async def test_sensors(
2223
hass: HomeAssistant,
@@ -381,6 +382,21 @@ async def test_draft_electrical_measurement_sensor(
381382
assert state.state == "unknown"
382383

383384

385+
@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00")
386+
@pytest.mark.parametrize("node_fixture", ["microwave_oven"])
387+
async def test_countdown_time_sensor(
388+
hass: HomeAssistant,
389+
matter_client: MagicMock,
390+
matter_node: MatterNode,
391+
) -> None:
392+
"""Test CountdownTime sensor."""
393+
# OperationalState Cluster / CountdownTime (1/96/2)
394+
state = hass.states.get("sensor.microwave_oven_estimated_end_time")
395+
assert state
396+
# 1/96/2 = 30 seconds, so 30 s should be added to the current time.
397+
assert state.state == "2025-01-01T14:00:30+00:00"
398+
399+
384400
@pytest.mark.parametrize("node_fixture", ["silabs_laundrywasher"])
385401
async def test_list_sensor(
386402
hass: HomeAssistant,

0 commit comments

Comments
 (0)