Skip to content

Commit 8429f15

Browse files
authored
Fix status checks in Matter binary sensors (home-assistant#156276)
This PR fixes bitmap bit checking logic in Matter binary sensors by replacing equality comparisons with bitwise AND operations. The changes correct how the integration checks if specific bits are set in bitmap fields. Key changes: Changed equality checks (==) to bitwise AND operations (&) for checking bitmap bits Wrapped bitwise operations with bool() to ensure boolean return values Applied fixes consistently across PumpStatus, DishwasherAlarm, and RefrigeratorAlarm bitmaps
1 parent 7b4f5ad commit 8429f15

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

homeassistant/components/matter/binary_sensor.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -353,17 +353,13 @@ def _update_from_device(self) -> None:
353353
device_class=BinarySensorDeviceClass.PROBLEM,
354354
entity_category=EntityCategory.DIAGNOSTIC,
355355
# DeviceFault or SupplyFault bit enabled
356-
device_to_ha={
357-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault: True,
358-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault: True,
359-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedLow: False,
360-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedHigh: False,
361-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kLocalOverride: False,
362-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning: False,
363-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRemotePressure: False,
364-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRemoteFlow: False,
365-
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRemoteTemperature: False,
366-
}.get,
356+
device_to_ha=lambda x: bool(
357+
x
358+
& (
359+
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault
360+
| clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault
361+
)
362+
),
367363
),
368364
entity_class=MatterBinarySensor,
369365
required_attributes=(
@@ -377,9 +373,9 @@ def _update_from_device(self) -> None:
377373
key="PumpStatusRunning",
378374
translation_key="pump_running",
379375
device_class=BinarySensorDeviceClass.RUNNING,
380-
device_to_ha=lambda x: (
376+
device_to_ha=lambda x: bool(
381377
x
382-
== clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning
378+
& clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning
383379
),
384380
),
385381
entity_class=MatterBinarySensor,
@@ -395,8 +391,8 @@ def _update_from_device(self) -> None:
395391
translation_key="dishwasher_alarm_inflow",
396392
device_class=BinarySensorDeviceClass.PROBLEM,
397393
entity_category=EntityCategory.DIAGNOSTIC,
398-
device_to_ha=lambda x: (
399-
x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError
394+
device_to_ha=lambda x: bool(
395+
x & clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError
400396
),
401397
),
402398
entity_class=MatterBinarySensor,
@@ -410,8 +406,8 @@ def _update_from_device(self) -> None:
410406
translation_key="alarm_door",
411407
device_class=BinarySensorDeviceClass.PROBLEM,
412408
entity_category=EntityCategory.DIAGNOSTIC,
413-
device_to_ha=lambda x: (
414-
x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError
409+
device_to_ha=lambda x: bool(
410+
x & clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError
415411
),
416412
),
417413
entity_class=MatterBinarySensor,
@@ -481,8 +477,8 @@ def _update_from_device(self) -> None:
481477
translation_key="alarm_door",
482478
device_class=BinarySensorDeviceClass.PROBLEM,
483479
entity_category=EntityCategory.DIAGNOSTIC,
484-
device_to_ha=lambda x: (
485-
x == clusters.RefrigeratorAlarm.Bitmaps.AlarmBitmap.kDoorOpen
480+
device_to_ha=lambda x: bool(
481+
x & clusters.RefrigeratorAlarm.Bitmaps.AlarmBitmap.kDoorOpen
486482
),
487483
),
488484
entity_class=MatterBinarySensor,

tests/components/matter/test_binary_sensor.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,27 @@ async def test_pump(
239239
assert state
240240
assert state.state == "off"
241241

242-
# PumpStatus --> DeviceFault bit
242+
# Initial state: kRunning bit only (no fault bits) should be off
243243
state = hass.states.get("binary_sensor.mock_pump_problem")
244244
assert state
245-
assert state.state == "unknown"
245+
assert state.state == "off"
246246

247+
# Set DeviceFault bit
247248
set_node_attribute(matter_node, 1, 512, 16, 1)
248249
await trigger_subscription_callback(hass, matter_client)
249250

250251
state = hass.states.get("binary_sensor.mock_pump_problem")
251252
assert state
252253
assert state.state == "on"
253254

254-
# PumpStatus --> SupplyFault bit
255+
# Clear all bits - problem sensor should be off
256+
set_node_attribute(matter_node, 1, 512, 16, 0)
257+
await trigger_subscription_callback(hass, matter_client)
258+
state = hass.states.get("binary_sensor.mock_pump_problem")
259+
assert state
260+
assert state.state == "off"
261+
262+
# Set SupplyFault bit
255263
set_node_attribute(matter_node, 1, 512, 16, 2)
256264
await trigger_subscription_callback(hass, matter_client)
257265

@@ -270,13 +278,30 @@ async def test_dishwasher_alarm(
270278
state = hass.states.get("binary_sensor.dishwasher_door_alarm")
271279
assert state
272280

281+
# set DoorAlarm alarm
273282
set_node_attribute(matter_node, 1, 93, 2, 4)
274283
await trigger_subscription_callback(hass, matter_client)
275284

276285
state = hass.states.get("binary_sensor.dishwasher_door_alarm")
277286
assert state
278287
assert state.state == "on"
279288

289+
# clear DoorAlarm alarm
290+
set_node_attribute(matter_node, 1, 93, 2, 0)
291+
await trigger_subscription_callback(hass, matter_client)
292+
293+
state = hass.states.get("binary_sensor.dishwasher_inflow_alarm")
294+
assert state
295+
assert state.state == "off"
296+
297+
# set InflowError alarm
298+
set_node_attribute(matter_node, 1, 93, 2, 1)
299+
await trigger_subscription_callback(hass, matter_client)
300+
301+
state = hass.states.get("binary_sensor.dishwasher_inflow_alarm")
302+
assert state
303+
assert state.state == "on"
304+
280305

281306
@pytest.mark.parametrize("node_fixture", ["valve"])
282307
async def test_water_valve(

0 commit comments

Comments
 (0)