Google assistant: add blinds trait for covers (#22336)
* Update const.py * Update smart_home.py * Update trait.py * Update test_trait.py * Update smart_home.py * Update test_trait.py * Update trait.py * Update trait.py * Update test_trait.py * Update test_trait.py * Update __init__.py * Update test_trait.py * Change email * Trying to correct CLA * Update __init__.py * Update trait.py * Update trait.py * Update trait.py * Update trait.py * Update __init__.py * Update test_trait.py * Update test_google_assistant.py * Update trait.py * Update trait.py * Update test_trait.py * Update test_trait.py
This commit is contained in:
parent
95a7077b41
commit
b04fd08cea
5 changed files with 110 additions and 101 deletions
|
@ -48,6 +48,7 @@ TRAIT_TEMPERATURE_SETTING = PREFIX_TRAITS + 'TemperatureSetting'
|
|||
TRAIT_LOCKUNLOCK = PREFIX_TRAITS + 'LockUnlock'
|
||||
TRAIT_FANSPEED = PREFIX_TRAITS + 'FanSpeed'
|
||||
TRAIT_MODES = PREFIX_TRAITS + 'Modes'
|
||||
TRAIT_OPENCLOSE = PREFIX_TRAITS + 'OpenClose'
|
||||
|
||||
PREFIX_COMMANDS = 'action.devices.commands.'
|
||||
COMMAND_ONOFF = PREFIX_COMMANDS + 'OnOff'
|
||||
|
@ -66,6 +67,7 @@ COMMAND_THERMOSTAT_SET_MODE = PREFIX_COMMANDS + 'ThermostatSetMode'
|
|||
COMMAND_LOCKUNLOCK = PREFIX_COMMANDS + 'LockUnlock'
|
||||
COMMAND_FANSPEED = PREFIX_COMMANDS + 'SetFanSpeed'
|
||||
COMMAND_MODES = PREFIX_COMMANDS + 'SetModes'
|
||||
COMMAND_OPENCLOSE = PREFIX_COMMANDS + 'OpenClose'
|
||||
|
||||
TRAITS = []
|
||||
|
||||
|
@ -128,8 +130,6 @@ class BrightnessTrait(_Trait):
|
|||
"""Test if state is supported."""
|
||||
if domain == light.DOMAIN:
|
||||
return features & light.SUPPORT_BRIGHTNESS
|
||||
if domain == cover.DOMAIN:
|
||||
return features & cover.SUPPORT_SET_POSITION
|
||||
if domain == media_player.DOMAIN:
|
||||
return features & media_player.SUPPORT_VOLUME_SET
|
||||
|
||||
|
@ -149,11 +149,6 @@ class BrightnessTrait(_Trait):
|
|||
if brightness is not None:
|
||||
response['brightness'] = int(100 * (brightness / 255))
|
||||
|
||||
elif domain == cover.DOMAIN:
|
||||
position = self.state.attributes.get(cover.ATTR_CURRENT_POSITION)
|
||||
if position is not None:
|
||||
response['brightness'] = position
|
||||
|
||||
elif domain == media_player.DOMAIN:
|
||||
level = self.state.attributes.get(
|
||||
media_player.ATTR_MEDIA_VOLUME_LEVEL)
|
||||
|
@ -173,12 +168,6 @@ class BrightnessTrait(_Trait):
|
|||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
light.ATTR_BRIGHTNESS_PCT: params['brightness']
|
||||
}, blocking=True, context=data.context)
|
||||
elif domain == cover.DOMAIN:
|
||||
await self.hass.services.async_call(
|
||||
cover.DOMAIN, cover.SERVICE_SET_COVER_POSITION, {
|
||||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
cover.ATTR_POSITION: params['brightness']
|
||||
}, blocking=True, context=data.context)
|
||||
elif domain == media_player.DOMAIN:
|
||||
await self.hass.services.async_call(
|
||||
media_player.DOMAIN, media_player.SERVICE_VOLUME_SET, {
|
||||
|
@ -254,7 +243,6 @@ class OnOffTrait(_Trait):
|
|||
switch.DOMAIN,
|
||||
fan.DOMAIN,
|
||||
light.DOMAIN,
|
||||
cover.DOMAIN,
|
||||
media_player.DOMAIN,
|
||||
)
|
||||
|
||||
|
@ -264,22 +252,13 @@ class OnOffTrait(_Trait):
|
|||
|
||||
def query_attributes(self):
|
||||
"""Return OnOff query attributes."""
|
||||
if self.state.domain == cover.DOMAIN:
|
||||
return {'on': self.state.state != cover.STATE_CLOSED}
|
||||
return {'on': self.state.state != STATE_OFF}
|
||||
|
||||
async def execute(self, command, data, params):
|
||||
"""Execute an OnOff command."""
|
||||
domain = self.state.domain
|
||||
|
||||
if domain == cover.DOMAIN:
|
||||
service_domain = domain
|
||||
if params['on']:
|
||||
service = cover.SERVICE_OPEN_COVER
|
||||
else:
|
||||
service = cover.SERVICE_CLOSE_COVER
|
||||
|
||||
elif domain == group.DOMAIN:
|
||||
if domain == group.DOMAIN:
|
||||
service_domain = HA_DOMAIN
|
||||
service = SERVICE_TURN_ON if params['on'] else SERVICE_TURN_OFF
|
||||
|
||||
|
@ -1047,3 +1026,68 @@ class ModesTrait(_Trait):
|
|||
ATTR_ENTITY_ID: self.state.entity_id,
|
||||
media_player.ATTR_INPUT_SOURCE: source
|
||||
}, blocking=True, context=data.context)
|
||||
|
||||
|
||||
@register_trait
|
||||
class OpenCloseTrait(_Trait):
|
||||
"""Trait to open and close a cover.
|
||||
|
||||
https://developers.google.com/actions/smarthome/traits/openclose
|
||||
"""
|
||||
|
||||
name = TRAIT_OPENCLOSE
|
||||
commands = [
|
||||
COMMAND_OPENCLOSE
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def supported(domain, features):
|
||||
"""Test if state is supported."""
|
||||
return domain == cover.DOMAIN
|
||||
|
||||
def sync_attributes(self):
|
||||
"""Return opening direction."""
|
||||
return {}
|
||||
|
||||
def query_attributes(self):
|
||||
"""Return state query attributes."""
|
||||
domain = self.state.domain
|
||||
response = {}
|
||||
|
||||
if domain == cover.DOMAIN:
|
||||
position = self.state.attributes.get(cover.ATTR_CURRENT_POSITION)
|
||||
if position is not None:
|
||||
response['openPercent'] = position
|
||||
else:
|
||||
if self.state.state != cover.STATE_CLOSED:
|
||||
response['openPercent'] = 100
|
||||
else:
|
||||
response['openPercent'] = 0
|
||||
|
||||
return response
|
||||
|
||||
async def execute(self, command, data, params):
|
||||
"""Execute an Open, close, Set position command."""
|
||||
domain = self.state.domain
|
||||
|
||||
if domain == cover.DOMAIN:
|
||||
position = self.state.attributes.get(cover.ATTR_CURRENT_POSITION)
|
||||
if 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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue