From d128443a2adfebbcbf2c398837e4c854b2de2c54 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Thu, 3 Sep 2020 18:32:57 +0200 Subject: [PATCH] Google assistant openclose (#39612) * Make sure we set discreteOnlyOpenClose for binary sensors * Mark switches that are assumed state as commandOnlyOnOff * Drop stray extra line * Fix pylint error Co-authored-by: springstan <46536646+springstan@users.noreply.github.com> --- .../components/google_assistant/trait.py | 3 +++ .../google_assistant/test_smart_home.py | 5 ++++- tests/components/google_assistant/test_trait.py | 15 +++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/google_assistant/trait.py b/homeassistant/components/google_assistant/trait.py index 569b12e4730..5d2d59f2144 100644 --- a/homeassistant/components/google_assistant/trait.py +++ b/homeassistant/components/google_assistant/trait.py @@ -315,6 +315,8 @@ class OnOffTrait(_Trait): def sync_attributes(self): """Return OnOff attributes for a sync request.""" + if self.state.attributes.get(ATTR_ASSUMED_STATE, False): + return {"commandOnlyOnOff": True} return {} def query_attributes(self): @@ -1541,6 +1543,7 @@ class OpenCloseTrait(_Trait): response = {} if self.state.domain == binary_sensor.DOMAIN: response["queryOnlyOpenClose"] = True + response["discreteOnlyOpenClose"] = True return response def query_attributes(self): diff --git a/tests/components/google_assistant/test_smart_home.py b/tests/components/google_assistant/test_smart_home.py index 1aadb39d5a8..78d403c2038 100644 --- a/tests/components/google_assistant/test_smart_home.py +++ b/tests/components/google_assistant/test_smart_home.py @@ -795,7 +795,10 @@ async def test_device_class_binary_sensor(hass, device_class, google_type): "agentUserId": "test-agent", "devices": [ { - "attributes": {"queryOnlyOpenClose": True}, + "attributes": { + "queryOnlyOpenClose": True, + "discreteOnlyOpenClose": True, + }, "id": "binary_sensor.demo_sensor", "name": {"name": "Demo Sensor"}, "traits": ["action.devices.traits.OpenClose"], diff --git a/tests/components/google_assistant/test_trait.py b/tests/components/google_assistant/test_trait.py index ae51ba76ffc..ac0db986f42 100644 --- a/tests/components/google_assistant/test_trait.py +++ b/tests/components/google_assistant/test_trait.py @@ -208,6 +208,11 @@ async def test_onoff_switch(hass): assert trt_off.query_attributes() == {"on": False} + trt_assumed = trait.OnOffTrait( + hass, State("switch.bla", STATE_OFF, {"assumed_state": True}), BASIC_CONFIG + ) + assert trt_assumed.sync_attributes() == {"commandOnlyOnOff": True} + on_calls = async_mock_service(hass, switch.DOMAIN, SERVICE_TURN_ON) await trt_on.execute(trait.COMMAND_ONOFF, BASIC_DATA, {"on": True}, {}) assert len(on_calls) == 1 @@ -2022,7 +2027,10 @@ async def test_openclose_binary_sensor(hass, device_class): BASIC_CONFIG, ) - assert trt.sync_attributes() == {"queryOnlyOpenClose": True} + assert trt.sync_attributes() == { + "queryOnlyOpenClose": True, + "discreteOnlyOpenClose": True, + } assert trt.query_attributes() == {"openPercent": 100} @@ -2032,7 +2040,10 @@ async def test_openclose_binary_sensor(hass, device_class): BASIC_CONFIG, ) - assert trt.sync_attributes() == {"queryOnlyOpenClose": True} + assert trt.sync_attributes() == { + "queryOnlyOpenClose": True, + "discreteOnlyOpenClose": True, + } assert trt.query_attributes() == {"openPercent": 0}