Prevent KeyError in Matter select entity (#126605)

This commit is contained in:
Marcel van der Veldt 2024-09-24 12:02:01 +02:00 committed by GitHub
parent c96d4991b9
commit f2092ef083
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View File

@ -229,18 +229,18 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.CONFIG,
translation_key="startup_on_off",
options=["On", "Off", "Toggle", "Previous"],
measurement_to_ha=lambda x: { # pylint: disable=unnecessary-lambda
measurement_to_ha={
0: "Off",
1: "On",
2: "Toggle",
None: "Previous",
}.get(x),
ha_to_native_value=lambda x: {
}.get,
ha_to_native_value={
"Off": 0,
"On": 1,
"Toggle": 2,
"Previous": None,
}[x],
}.get,
),
entity_class=MatterSelectEntity,
required_attributes=(clusters.OnOff.Attributes.StartUpOnOff,),

View File

@ -97,3 +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)
await trigger_subscription_callback(hass, matter_client)
state = hass.states.get(entity_id)
assert state.state == "unknown"