Skip to content

Commit 2afb1a6

Browse files
lboueMrEbbinghausTheJulianJES
authored
Add Matter Thermostat OccupancySensor (home-assistant#153166)
Co-authored-by: Björn Ebbinghaus <[email protected]> Co-authored-by: TheJulianJES <[email protected]>
1 parent c2f7f29 commit 2afb1a6

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

homeassistant/components/matter/binary_sensor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ def _update_from_device(self) -> None:
8888
entity_class=MatterBinarySensor,
8989
required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,),
9090
),
91+
MatterDiscoverySchema(
92+
platform=Platform.BINARY_SENSOR,
93+
entity_description=MatterBinarySensorEntityDescription(
94+
key="ThermostatOccupancySensor",
95+
device_class=BinarySensorDeviceClass.OCCUPANCY,
96+
# The first bit = if occupied
97+
device_to_ha=lambda x: (x & 1 == 1) if x is not None else None,
98+
),
99+
entity_class=MatterBinarySensor,
100+
required_attributes=(clusters.Thermostat.Attributes.Occupancy,),
101+
),
91102
MatterDiscoverySchema(
92103
platform=Platform.BINARY_SENSOR,
93104
entity_description=MatterBinarySensorEntityDescription(

tests/components/matter/fixtures/nodes/thermostat.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
"1/64/65531": [0, 65528, 65529, 65531, 65532, 65533],
319319
"1/513/0": 2830,
320320
"1/513/1": 1250,
321+
"1/513/2": 1,
321322
"1/513/3": null,
322323
"1/513/4": null,
323324
"1/513/5": null,

tests/components/matter/snapshots/test_binary_sensor.ambr

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,55 @@
12221222
'state': 'off',
12231223
})
12241224
# ---
1225+
# name: test_binary_sensors[thermostat][binary_sensor.longan_link_hvac_occupancy-entry]
1226+
EntityRegistryEntrySnapshot({
1227+
'aliases': set({
1228+
}),
1229+
'area_id': None,
1230+
'capabilities': None,
1231+
'config_entry_id': <ANY>,
1232+
'config_subentry_id': <ANY>,
1233+
'device_class': None,
1234+
'device_id': <ANY>,
1235+
'disabled_by': None,
1236+
'domain': 'binary_sensor',
1237+
'entity_category': None,
1238+
'entity_id': 'binary_sensor.longan_link_hvac_occupancy',
1239+
'has_entity_name': True,
1240+
'hidden_by': None,
1241+
'icon': None,
1242+
'id': <ANY>,
1243+
'labels': set({
1244+
}),
1245+
'name': None,
1246+
'options': dict({
1247+
}),
1248+
'original_device_class': <BinarySensorDeviceClass.OCCUPANCY: 'occupancy'>,
1249+
'original_icon': None,
1250+
'original_name': 'Occupancy',
1251+
'platform': 'matter',
1252+
'previous_unique_id': None,
1253+
'suggested_object_id': None,
1254+
'supported_features': 0,
1255+
'translation_key': None,
1256+
'unique_id': '00000000000004D2-0000000000000004-MatterNodeDevice-1-ThermostatOccupancySensor-513-2',
1257+
'unit_of_measurement': None,
1258+
})
1259+
# ---
1260+
# name: test_binary_sensors[thermostat][binary_sensor.longan_link_hvac_occupancy-state]
1261+
StateSnapshot({
1262+
'attributes': ReadOnlyDict({
1263+
'device_class': 'occupancy',
1264+
'friendly_name': 'Longan link HVAC Occupancy',
1265+
}),
1266+
'context': <ANY>,
1267+
'entity_id': 'binary_sensor.longan_link_hvac_occupancy',
1268+
'last_changed': <ANY>,
1269+
'last_reported': <ANY>,
1270+
'last_updated': <ANY>,
1271+
'state': 'on',
1272+
})
1273+
# ---
12251274
# name: test_binary_sensors[valve][binary_sensor.valve_general_fault-entry]
12261275
EntityRegistryEntrySnapshot({
12271276
'aliases': set({

tests/components/matter/test_binary_sensor.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Generator
44
from unittest.mock import MagicMock, patch
55

6+
from chip.clusters import Objects as clusters
67
from matter_server.client.models.node import MatterNode
78
from matter_server.common.models import EventType
89
import pytest
@@ -344,3 +345,31 @@ async def test_water_valve(
344345
state = hass.states.get("binary_sensor.valve_valve_leaking")
345346
assert state
346347
assert state.state == "on"
348+
349+
350+
@pytest.mark.parametrize("node_fixture", ["thermostat"])
351+
async def test_thermostat_occupancy(
352+
hass: HomeAssistant,
353+
matter_client: MagicMock,
354+
matter_node: MatterNode,
355+
) -> None:
356+
"""Test thermostat occupancy."""
357+
state = hass.states.get("binary_sensor.longan_link_hvac_occupancy")
358+
assert state
359+
assert state.state == "on"
360+
361+
# Test Occupancy attribute change
362+
occupancy_attribute = clusters.Thermostat.Attributes.Occupancy
363+
364+
set_node_attribute(
365+
matter_node,
366+
1,
367+
occupancy_attribute.cluster_id,
368+
occupancy_attribute.attribute_id,
369+
0,
370+
)
371+
await trigger_subscription_callback(hass, matter_client)
372+
373+
state = hass.states.get("binary_sensor.longan_link_hvac_occupancy")
374+
assert state
375+
assert state.state == "off"

0 commit comments

Comments
 (0)