Guard against missing states in Alexa state updates (#61152)

pull/61169/head
Erik Montnemery 2021-12-07 12:44:50 +01:00 committed by GitHub
parent 4563d95d40
commit af2e95d891
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -182,12 +182,13 @@ async def async_send_add_or_update_message(hass, config, entity_ids):
endpoints = [] endpoints = []
for entity_id in entity_ids: for entity_id in entity_ids:
domain = entity_id.split(".", 1)[0] if (domain := entity_id.split(".", 1)[0]) not in ENTITY_ADAPTERS:
if domain not in ENTITY_ADAPTERS:
continue continue
alexa_entity = ENTITY_ADAPTERS[domain](hass, config, hass.states.get(entity_id)) if (state := hass.states.get(entity_id)) is None:
continue
alexa_entity = ENTITY_ADAPTERS[domain](hass, config, state)
endpoints.append(alexa_entity.serialize_discovery()) endpoints.append(alexa_entity.serialize_discovery())
payload = {"endpoints": endpoints, "scope": {"type": "BearerToken", "token": token}} payload = {"endpoints": endpoints, "scope": {"type": "BearerToken", "token": token}}

View File

@ -117,10 +117,18 @@ async def test_send_add_or_update_message(hass, aioclient_mock):
{"friendly_name": "Test Contact Sensor", "device_class": "door"}, {"friendly_name": "Test Contact Sensor", "device_class": "door"},
) )
await state_report.async_send_add_or_update_message( hass.states.async_set(
hass, DEFAULT_CONFIG, ["binary_sensor.test_contact", "zwave.bla"] "zwave.bla",
"wow_such_unsupported",
) )
entities = [
"binary_sensor.test_contact",
"binary_sensor.non_existing", # Supported, but does not exist
"zwave.bla", # Unsupported
]
await state_report.async_send_add_or_update_message(hass, DEFAULT_CONFIG, entities)
assert len(aioclient_mock.mock_calls) == 1 assert len(aioclient_mock.mock_calls) == 1
call = aioclient_mock.mock_calls call = aioclient_mock.mock_calls