Improve Google Assistant cover assumed state handling (#43255)

* Set command only cover
* No need for override position now that we support command only
* Return empty state response for assumed state

Fixes #43178
Reverts: #23498

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Joakim Plate 2020-11-17 21:55:46 +01:00 committed by GitHub
parent 212fb572e1
commit 1567fadda3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 27 deletions

View file

@ -1521,8 +1521,6 @@ class OpenCloseTrait(_Trait):
name = TRAIT_OPENCLOSE name = TRAIT_OPENCLOSE
commands = [COMMAND_OPENCLOSE] commands = [COMMAND_OPENCLOSE]
override_position = None
@staticmethod @staticmethod
def supported(domain, features, device_class): def supported(domain, features, device_class):
"""Test if state is supported.""" """Test if state is supported."""
@ -1548,6 +1546,10 @@ class OpenCloseTrait(_Trait):
if self.state.domain == binary_sensor.DOMAIN: if self.state.domain == binary_sensor.DOMAIN:
response["queryOnlyOpenClose"] = True response["queryOnlyOpenClose"] = True
response["discreteOnlyOpenClose"] = True response["discreteOnlyOpenClose"] = True
if self.state.attributes.get(ATTR_ASSUMED_STATE):
response["commandOnlyOpenClose"] = True
return response return response
def query_attributes(self): def query_attributes(self):
@ -1555,25 +1557,20 @@ class OpenCloseTrait(_Trait):
domain = self.state.domain domain = self.state.domain
response = {} response = {}
if self.override_position is not None: # When it's an assumed state, we will return empty state
response["openPercent"] = self.override_position # This shouldn't happen because we set `commandOnlyOpenClose`
# but Google still queries. Erroring here will cause device
elif domain == cover.DOMAIN: # to show up offline.
# When it's an assumed state, we will return that querying state if self.state.attributes.get(ATTR_ASSUMED_STATE):
# is not supported. return response
if self.state.attributes.get(ATTR_ASSUMED_STATE):
raise SmartHomeError(
ERR_NOT_SUPPORTED, "Querying state is not supported"
)
if domain == cover.DOMAIN:
if self.state.state == STATE_UNKNOWN: if self.state.state == STATE_UNKNOWN:
raise SmartHomeError( raise SmartHomeError(
ERR_NOT_SUPPORTED, "Querying state is not supported" ERR_NOT_SUPPORTED, "Querying state is not supported"
) )
position = self.override_position or self.state.attributes.get( position = self.state.attributes.get(cover.ATTR_CURRENT_POSITION)
cover.ATTR_CURRENT_POSITION
)
if position is not None: if position is not None:
response["openPercent"] = position response["openPercent"] = position
@ -1626,12 +1623,6 @@ class OpenCloseTrait(_Trait):
cover.DOMAIN, service, svc_params, blocking=True, context=data.context cover.DOMAIN, service, svc_params, blocking=True, context=data.context
) )
if (
self.state.attributes.get(ATTR_ASSUMED_STATE)
or self.state.state == STATE_UNKNOWN
):
self.override_position = params["openPercent"]
@register_trait @register_trait
class VolumeTrait(_Trait): class VolumeTrait(_Trait):

View file

@ -1886,7 +1886,8 @@ async def test_openclose_cover_unknown_state(hass):
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data == {ATTR_ENTITY_ID: "cover.bla"} assert calls[0].data == {ATTR_ENTITY_ID: "cover.bla"}
assert trt.query_attributes() == {"openPercent": 100} with pytest.raises(helpers.SmartHomeError):
trt.query_attributes()
async def test_openclose_cover_assumed_state(hass): async def test_openclose_cover_assumed_state(hass):
@ -1909,18 +1910,15 @@ async def test_openclose_cover_assumed_state(hass):
BASIC_CONFIG, BASIC_CONFIG,
) )
assert trt.sync_attributes() == {} assert trt.sync_attributes() == {"commandOnlyOpenClose": True}
with pytest.raises(helpers.SmartHomeError): assert trt.query_attributes() == {}
trt.query_attributes()
calls = async_mock_service(hass, cover.DOMAIN, cover.SERVICE_SET_COVER_POSITION) calls = async_mock_service(hass, cover.DOMAIN, cover.SERVICE_SET_COVER_POSITION)
await trt.execute(trait.COMMAND_OPENCLOSE, BASIC_DATA, {"openPercent": 40}, {}) await trt.execute(trait.COMMAND_OPENCLOSE, BASIC_DATA, {"openPercent": 40}, {})
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data == {ATTR_ENTITY_ID: "cover.bla", cover.ATTR_POSITION: 40} assert calls[0].data == {ATTR_ENTITY_ID: "cover.bla", cover.ATTR_POSITION: 40}
assert trt.query_attributes() == {"openPercent": 40}
async def test_openclose_cover_no_position(hass): async def test_openclose_cover_no_position(hass):
"""Test OpenClose trait support for cover domain.""" """Test OpenClose trait support for cover domain."""