Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions homeassistant/components/matter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, cast

from chip.clusters import Objects as clusters
Expand Down Expand Up @@ -44,7 +44,7 @@
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import slugify
from homeassistant.util import dt as dt_util, slugify

from .entity import MatterEntity, MatterEntityDescription
from .helpers import get_matter
Expand Down Expand Up @@ -942,6 +942,21 @@ def _update_from_device(self) -> None:
# don't discover this entry if the supported state list is empty
secondary_value_is_not=[],
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterSensorEntityDescription(
key="OperationalStateCountdownTime",
translation_key="estimated_end_time",
device_class=SensorDeviceClass.TIMESTAMP,
state_class=None,
# Add countdown to current datetime to get the estimated end time
device_to_ha=(
lambda x: dt_util.utcnow() + timedelta(seconds=x) if x > 0 else None
),
),
entity_class=MatterSensor,
required_attributes=(clusters.OperationalState.Attributes.CountdownTime,),
),
MatterDiscoverySchema(
platform=Platform.SENSOR,
entity_description=MatterListSensorEntityDescription(
Expand Down
3 changes: 3 additions & 0 deletions homeassistant/components/matter/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@
"docked": "Docked"
}
},
"estimated_end_time": {
"name": "Estimated end time"
},
"switch_current_position": {
"name": "Current switch position"
},
Expand Down
49 changes: 49 additions & 0 deletions tests/components/matter/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,55 @@
'state': '1.3',
})
# ---
# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.microwave_oven_estimated_end_time',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.TIMESTAMP: 'timestamp'>,
'original_icon': None,
'original_name': 'Estimated end time',
'platform': 'matter',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'estimated_end_time',
'unique_id': '00000000000004D2-000000000000009D-MatterNodeDevice-1-OperationalStateCountdownTime-96-2',
'unit_of_measurement': None,
})
# ---
# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'timestamp',
'friendly_name': 'Microwave Oven Estimated end time',
}),
'context': <ANY>,
'entity_id': 'sensor.microwave_oven_estimated_end_time',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2025-01-01T14:00:30+00:00',
})
# ---
# name: test_sensors[microwave_oven][sensor.microwave_oven_operational_state-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
Expand Down
16 changes: 16 additions & 0 deletions tests/components/matter/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)


@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00")
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "matter_devices")
async def test_sensors(
hass: HomeAssistant,
Expand Down Expand Up @@ -381,6 +382,21 @@ async def test_draft_electrical_measurement_sensor(
assert state.state == "unknown"


@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00")
@pytest.mark.parametrize("node_fixture", ["microwave_oven"])
async def test_countdown_time_sensor(
hass: HomeAssistant,
matter_client: MagicMock,
matter_node: MatterNode,
) -> None:
"""Test CountdownTime sensor."""
# OperationalState Cluster / CountdownTime (1/96/2)
state = hass.states.get("sensor.microwave_oven_estimated_end_time")
assert state
# 1/96/2 = 30 seconds, so 30 s should be added to the current time.
assert state.state == "2025-01-01T14:00:30+00:00"


@pytest.mark.parametrize("node_fixture", ["silabs_laundrywasher"])
async def test_list_sensor(
hass: HomeAssistant,
Expand Down
Loading