Google Assistant: make it possible to set a custom domain/type (#10188)

* Google Assistant: make it possible to set a custom domain/type

* add test for switch platform

* update custom type test
pull/10237/merge
Per Osbäck 2017-11-01 04:38:34 +01:00 committed by Paulus Schoutsen
parent 80a9539f97
commit b6324b511c
5 changed files with 67 additions and 4 deletions

View File

@ -5,6 +5,7 @@ GOOGLE_ASSISTANT_API_ENDPOINT = '/api/google_assistant'
ATTR_GOOGLE_ASSISTANT = 'google_assistant'
ATTR_GOOGLE_ASSISTANT_NAME = 'google_assistant_name'
ATTR_GOOGLE_ASSISTANT_TYPE = 'google_assistant_type'
CONF_EXPOSE_BY_DEFAULT = 'expose_by_default'
CONF_EXPOSED_DOMAINS = 'exposed_domains'

View File

@ -19,7 +19,7 @@ from homeassistant.components import (
)
from .const import (
ATTR_GOOGLE_ASSISTANT_NAME,
ATTR_GOOGLE_ASSISTANT_NAME, ATTR_GOOGLE_ASSISTANT_TYPE,
COMMAND_BRIGHTNESS, COMMAND_ONOFF, COMMAND_ACTIVATESCENE,
TRAIT_ONOFF, TRAIT_BRIGHTNESS, TRAIT_COLOR_TEMP,
TRAIT_RGB_COLOR, TRAIT_SCENE,
@ -64,7 +64,8 @@ def make_actions_response(request_id: str, payload: dict) -> dict:
def entity_to_device(entity: Entity):
"""Convert a hass entity into an google actions device."""
class_data = MAPPING_COMPONENT.get(entity.domain)
class_data = MAPPING_COMPONENT.get(
entity.attributes.get(ATTR_GOOGLE_ASSISTANT_TYPE) or entity.domain)
if class_data is None:
return None

View File

@ -15,6 +15,30 @@ DEMO_DEVICES = [{
'action.devices.types.LIGHT',
'willReportState':
False
}, {
'id':
'switch.ac',
'name': {
'name': 'AC'
},
'traits': [
'action.devices.traits.OnOff'
],
'type': 'action.devices.types.SWITCH',
'willReportState':
False
}, {
'id':
'switch.decorative_lights',
'name': {
'name': 'Decorative Lights'
},
'traits': [
'action.devices.traits.OnOff'
],
'type': 'action.devices.types.LIGHT', # This is used for custom type
'willReportState':
False
}, {
'id':
'light.ceiling_lights',
@ -54,6 +78,14 @@ DEMO_DEVICES = [{
'traits': ['action.devices.traits.Scene'],
'type': 'action.devices.types.SCENE',
'willReportState': False
}, {
'id': 'group.all_switches',
'name': {
'name': 'all switches'
},
'traits': ['action.devices.traits.Scene'],
'type': 'action.devices.types.SCENE',
'willReportState': False
}, {
'id':
'cover.living_room_window',

View File

@ -6,7 +6,7 @@ import pytest
from homeassistant import setup, const, core
from homeassistant.components import (
http, async_setup, light, cover, media_player, fan
http, async_setup, light, cover, media_player, fan, switch
)
from homeassistant.components import google_assistant as ga
from tests.common import get_test_instance_port
@ -62,6 +62,12 @@ def hass_fixture(loop, hass):
'platform': 'demo'
}]
}))
loop.run_until_complete(
setup.async_setup_component(hass, switch.DOMAIN, {
'switch': [{
'platform': 'demo'
}]
}))
loop.run_until_complete(
setup.async_setup_component(hass, cover.DOMAIN, {
'cover': [{
@ -93,6 +99,16 @@ def hass_fixture(loop, hass):
ceiling_lights_entity.state,
attributes=attrs)
# By setting the google_assistant_type = 'light'
# we can override how a device is reported to GA
switch_light = hass.states.get('switch.decorative_lights')
attrs = dict(switch_light.attributes)
attrs[ga.const.ATTR_GOOGLE_ASSISTANT_TYPE] = "light"
hass.states.async_set(
switch_light.entity_id,
switch_light.state,
attributes=attrs)
return hass
@ -188,6 +204,8 @@ def test_execute_request(hass_fixture, assistant_client):
"commands": [{
"devices": [{
"id": "light.ceiling_lights",
}, {
"id": "switch.decorative_lights",
}, {
"id": "light.bed_light",
}],
@ -209,6 +227,7 @@ def test_execute_request(hass_fixture, assistant_client):
body = yield from result.json()
assert body.get('requestId') == reqid
commands = body['payload']['commands']
assert len(commands) == 2
assert len(commands) == 3
ceiling = hass_fixture.states.get('light.ceiling_lights')
assert ceiling.state == 'off'
assert hass_fixture.states.get('switch.decorative_lights').state == 'off'

View File

@ -15,6 +15,16 @@ DETERMINE_SERVICE_TESTS = [{ # Test light brightness
const.SERVICE_TURN_ON,
{'entity_id': 'light.test', 'brightness': 242}
)
}, { # Test switch to light custom type
'entity_id': 'switch.decorative_lights',
'command': ga.const.COMMAND_ONOFF,
'params': {
'on': True
},
'expected': (
const.SERVICE_TURN_ON,
{'entity_id': 'switch.decorative_lights'}
)
}, { # Test light on / off
'entity_id': 'light.test',
'command': ga.const.COMMAND_ONOFF,