From 5e7d5c6312ab1b16b835114708646ad964baff18 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Tue, 24 Sep 2024 19:36:09 +0200 Subject: [PATCH] Prevent KeyError when Matter device has invalid value for ModeSelect (#126672) --- homeassistant/components/matter/select.py | 2 +- tests/components/matter/test_select.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/matter/select.py b/homeassistant/components/matter/select.py index d91953610e9..1bba18b2c5b 100644 --- a/homeassistant/components/matter/select.py +++ b/homeassistant/components/matter/select.py @@ -105,7 +105,7 @@ class MatterModeSelectEntity(MatterSelectEntity): ) modes = {mode.mode: mode.label for mode in cluster.supportedModes} self._attr_options = list(modes.values()) - self._attr_current_option = modes[cluster.currentMode] + self._attr_current_option = modes.get(cluster.currentMode) # handle optional Description attribute as descriptive name for the mode if desc := getattr(cluster, "description", None): self._attr_name = desc diff --git a/tests/components/matter/test_select.py b/tests/components/matter/test_select.py index bda2a933d42..27ce6d32c22 100644 --- a/tests/components/matter/test_select.py +++ b/tests/components/matter/test_select.py @@ -97,8 +97,8 @@ async def test_attribute_select_entities( await trigger_subscription_callback(hass, matter_client) state = hass.states.get(entity_id) assert state.state == "on" - # test that an invalid value (e.g. 255) leads to an unknown state - set_node_attribute(light_node, 1, 6, 16387, 255) + # test that an invalid value (e.g. 253) leads to an unknown state + set_node_attribute(light_node, 1, 6, 16387, 253) await trigger_subscription_callback(hass, matter_client) state = hass.states.get(entity_id) assert state.state == "unknown"