From ab7b42c022c9128d696026296c9cc5e393a81fb2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 30 Aug 2020 15:19:56 +0200 Subject: [PATCH] Google: Recover from an entity raising while serializing query (#39381) Co-authored-by: Joakim Plate --- .../components/google_assistant/smart_home.py | 6 +- .../google_assistant/test_smart_home.py | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/google_assistant/smart_home.py b/homeassistant/components/google_assistant/smart_home.py index 50ef200d171..a9a97f047e9 100644 --- a/homeassistant/components/google_assistant/smart_home.py +++ b/homeassistant/components/google_assistant/smart_home.py @@ -137,7 +137,11 @@ async def async_devices_query(hass, data, payload): continue entity = GoogleEntity(hass, data.config, state) - devices[devid] = entity.query_serialize() + try: + devices[devid] = entity.query_serialize() + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Unexpected error serializing query for %s", state) + devices[devid] = {"online": False} return {"devices": devices} diff --git a/tests/components/google_assistant/test_smart_home.py b/tests/components/google_assistant/test_smart_home.py index 521f52a99eb..cf25c79efdb 100644 --- a/tests/components/google_assistant/test_smart_home.py +++ b/tests/components/google_assistant/test_smart_home.py @@ -1188,3 +1188,59 @@ async def test_sync_message_recovery(hass, caplog): } assert "Error serializing light.bad_light" in caplog.text + + +async def test_query_recover(hass, caplog): + """Test that we recover if an entity raises during query.""" + + hass.states.async_set( + "light.good", + "on", + { + "supported_features": hass.components.light.SUPPORT_BRIGHTNESS, + "brightness": 50, + }, + ) + hass.states.async_set( + "light.bad", + "on", + { + "supported_features": hass.components.light.SUPPORT_BRIGHTNESS, + "brightness": "shoe", + }, + ) + + result = await sh.async_handle_message( + hass, + BASIC_CONFIG, + "test-agent", + { + "requestId": REQ_ID, + "inputs": [ + { + "intent": "action.devices.QUERY", + "payload": { + "devices": [ + {"id": "light.good"}, + {"id": "light.bad"}, + ] + }, + } + ], + }, + const.SOURCE_CLOUD, + ) + + assert ( + f"Unexpected error serializing query for {hass.states.get('light.bad')}" + in caplog.text + ) + assert result == { + "requestId": REQ_ID, + "payload": { + "devices": { + "light.bad": {"online": False}, + "light.good": {"on": True, "online": True, "brightness": 19}, + } + }, + }