Fix for stateless covers (#22962)

* Corrections and test

* Raise error if does not support setting position

* Raising error for stateless and assumed state covers

* Fix pylint

* Tests

* Update test_trait.py
This commit is contained in:
giefca 2019-04-15 04:52:00 +02:00 committed by Paulus Schoutsen
parent d722f4d64a
commit b804919eaa
2 changed files with 51 additions and 29 deletions

View file

@ -29,10 +29,15 @@ from homeassistant.const import (
ATTR_SUPPORTED_FEATURES,
ATTR_TEMPERATURE,
ATTR_ASSUMED_STATE,
STATE_UNKNOWN,
)
from homeassistant.core import DOMAIN as HA_DOMAIN
from homeassistant.util import color as color_util, temperature as temp_util
from .const import ERR_VALUE_OUT_OF_RANGE
from .const import (
ERR_VALUE_OUT_OF_RANGE,
ERR_NOT_SUPPORTED,
ERR_FUNCTION_NOT_SUPPORTED,
)
from .helpers import SmartHomeError
_LOGGER = logging.getLogger(__name__)
@ -1064,18 +1069,25 @@ class OpenCloseTrait(_Trait):
# Google will not issue an open command if the assumed state is
# open, even if that is currently incorrect.
if self.state.attributes.get(ATTR_ASSUMED_STATE):
response['openPercent'] = 50
else:
position = self.state.attributes.get(
cover.ATTR_CURRENT_POSITION
)
raise SmartHomeError(
ERR_NOT_SUPPORTED,
'Querying state is not supported')
if position is not None:
response['openPercent'] = position
elif self.state.state != cover.STATE_CLOSED:
response['openPercent'] = 100
else:
response['openPercent'] = 0
if self.state.state == STATE_UNKNOWN:
raise SmartHomeError(
ERR_NOT_SUPPORTED,
'Querying state is not supported')
position = self.state.attributes.get(
cover.ATTR_CURRENT_POSITION
)
if position is not None:
response['openPercent'] = position
elif self.state.state != cover.STATE_CLOSED:
response['openPercent'] = 100
else:
response['openPercent'] = 0
elif domain == binary_sensor.DOMAIN:
if self.state.state == STATE_ON:
@ -1091,22 +1103,23 @@ class OpenCloseTrait(_Trait):
if domain == cover.DOMAIN:
position = self.state.attributes.get(cover.ATTR_CURRENT_POSITION)
if position is not None:
if params['openPercent'] == 0:
await self.hass.services.async_call(
cover.DOMAIN, cover.SERVICE_CLOSE_COVER, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
elif params['openPercent'] == 100:
await self.hass.services.async_call(
cover.DOMAIN, cover.SERVICE_OPEN_COVER, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
elif position is not None:
await self.hass.services.async_call(
cover.DOMAIN, cover.SERVICE_SET_COVER_POSITION, {
ATTR_ENTITY_ID: self.state.entity_id,
cover.ATTR_POSITION: params['openPercent']
}, blocking=True, context=data.context)
else:
if self.state.state != cover.STATE_CLOSED:
if params['openPercent'] < 100:
await self.hass.services.async_call(
cover.DOMAIN, cover.SERVICE_CLOSE_COVER, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
else:
if params['openPercent'] > 0:
await self.hass.services.async_call(
cover.DOMAIN, cover.SERVICE_OPEN_COVER, {
ATTR_ENTITY_ID: self.state.entity_id
}, blocking=True, context=data.context)
raise SmartHomeError(
ERR_FUNCTION_NOT_SUPPORTED,
'Setting a position is not supported')