Prevent KeyError when Matter device has invalid value for ModeSelect (#126672)

This commit is contained in:
Marcel van der Veldt 2024-09-24 19:36:09 +02:00 committed by GitHub
parent 354ee35ee4
commit 5e7d5c6312
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 3 deletions

View File

@ -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

View File

@ -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"