From cfb0b00c0cf22dbb2a5dc9c832d4dab1fa58398d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 20 Mar 2018 18:09:34 -0700 Subject: [PATCH] Do not include unavailable entities in Google Assistant SYNC (#13358) --- .../components/google_assistant/smart_home.py | 9 +++++- homeassistant/components/light/demo.py | 3 +- .../google_assistant/test_smart_home.py | 28 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/google_assistant/smart_home.py b/homeassistant/components/google_assistant/smart_home.py index 834d40c367c..7e746d48bed 100644 --- a/homeassistant/components/google_assistant/smart_home.py +++ b/homeassistant/components/google_assistant/smart_home.py @@ -94,9 +94,16 @@ class _GoogleEntity: https://developers.google.com/actions/smarthome/create-app#actiondevicessync """ - traits = self.traits() state = self.state + # When a state is unavailable, the attributes that describe + # capabilities will be stripped. For example, a light entity will miss + # the min/max mireds. Therefore they will be excluded from a sync. + if state.state == STATE_UNAVAILABLE: + return None + + traits = self.traits() + # Found no supported traits for this entity if not traits: return None diff --git a/homeassistant/components/light/demo.py b/homeassistant/components/light/demo.py index 05aecd542e2..ba27cbd3ac5 100644 --- a/homeassistant/components/light/demo.py +++ b/homeassistant/components/light/demo.py @@ -52,6 +52,7 @@ class DemoLight(Light): self._white = white self._effect_list = effect_list self._effect = effect + self._available = True @property def should_poll(self) -> bool: @@ -73,7 +74,7 @@ class DemoLight(Light): """Return availability.""" # This demo light is always available, but well-behaving components # should implement this to inform Home Assistant accordingly. - return True + return self._available @property def brightness(self) -> int: diff --git a/tests/components/google_assistant/test_smart_home.py b/tests/components/google_assistant/test_smart_home.py index 6523c22fee1..d7684dc90e0 100644 --- a/tests/components/google_assistant/test_smart_home.py +++ b/tests/components/google_assistant/test_smart_home.py @@ -259,3 +259,31 @@ def test_serialize_input_boolean(): 'type': 'action.devices.types.SWITCH', 'willReportState': False, } + + +async def test_unavailable_state_doesnt_sync(hass): + """Test that an unavailable entity does not sync over.""" + light = DemoLight( + None, 'Demo Light', + state=False, + hs_color=(180, 75), + ) + light.hass = hass + light.entity_id = 'light.demo_light' + light._available = False + await light.async_update_ha_state() + + result = await sh.async_handle_message(hass, BASIC_CONFIG, { + "requestId": REQ_ID, + "inputs": [{ + "intent": "action.devices.SYNC" + }] + }) + + assert result == { + 'requestId': REQ_ID, + 'payload': { + 'agentUserId': 'test-agent', + 'devices': [] + } + }