mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
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
This commit is contained in:
parent
80a9539f97
commit
b6324b511c
@ -5,6 +5,7 @@ GOOGLE_ASSISTANT_API_ENDPOINT = '/api/google_assistant'
|
|||||||
|
|
||||||
ATTR_GOOGLE_ASSISTANT = 'google_assistant'
|
ATTR_GOOGLE_ASSISTANT = 'google_assistant'
|
||||||
ATTR_GOOGLE_ASSISTANT_NAME = 'google_assistant_name'
|
ATTR_GOOGLE_ASSISTANT_NAME = 'google_assistant_name'
|
||||||
|
ATTR_GOOGLE_ASSISTANT_TYPE = 'google_assistant_type'
|
||||||
|
|
||||||
CONF_EXPOSE_BY_DEFAULT = 'expose_by_default'
|
CONF_EXPOSE_BY_DEFAULT = 'expose_by_default'
|
||||||
CONF_EXPOSED_DOMAINS = 'exposed_domains'
|
CONF_EXPOSED_DOMAINS = 'exposed_domains'
|
||||||
|
@ -19,7 +19,7 @@ from homeassistant.components import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_GOOGLE_ASSISTANT_NAME,
|
ATTR_GOOGLE_ASSISTANT_NAME, ATTR_GOOGLE_ASSISTANT_TYPE,
|
||||||
COMMAND_BRIGHTNESS, COMMAND_ONOFF, COMMAND_ACTIVATESCENE,
|
COMMAND_BRIGHTNESS, COMMAND_ONOFF, COMMAND_ACTIVATESCENE,
|
||||||
TRAIT_ONOFF, TRAIT_BRIGHTNESS, TRAIT_COLOR_TEMP,
|
TRAIT_ONOFF, TRAIT_BRIGHTNESS, TRAIT_COLOR_TEMP,
|
||||||
TRAIT_RGB_COLOR, TRAIT_SCENE,
|
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):
|
def entity_to_device(entity: Entity):
|
||||||
"""Convert a hass entity into an google actions device."""
|
"""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:
|
if class_data is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -15,6 +15,30 @@ DEMO_DEVICES = [{
|
|||||||
'action.devices.types.LIGHT',
|
'action.devices.types.LIGHT',
|
||||||
'willReportState':
|
'willReportState':
|
||||||
False
|
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':
|
'id':
|
||||||
'light.ceiling_lights',
|
'light.ceiling_lights',
|
||||||
@ -54,6 +78,14 @@ DEMO_DEVICES = [{
|
|||||||
'traits': ['action.devices.traits.Scene'],
|
'traits': ['action.devices.traits.Scene'],
|
||||||
'type': 'action.devices.types.SCENE',
|
'type': 'action.devices.types.SCENE',
|
||||||
'willReportState': False
|
'willReportState': False
|
||||||
|
}, {
|
||||||
|
'id': 'group.all_switches',
|
||||||
|
'name': {
|
||||||
|
'name': 'all switches'
|
||||||
|
},
|
||||||
|
'traits': ['action.devices.traits.Scene'],
|
||||||
|
'type': 'action.devices.types.SCENE',
|
||||||
|
'willReportState': False
|
||||||
}, {
|
}, {
|
||||||
'id':
|
'id':
|
||||||
'cover.living_room_window',
|
'cover.living_room_window',
|
||||||
|
@ -6,7 +6,7 @@ import pytest
|
|||||||
|
|
||||||
from homeassistant import setup, const, core
|
from homeassistant import setup, const, core
|
||||||
from homeassistant.components import (
|
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 homeassistant.components import google_assistant as ga
|
||||||
from tests.common import get_test_instance_port
|
from tests.common import get_test_instance_port
|
||||||
@ -62,6 +62,12 @@ def hass_fixture(loop, hass):
|
|||||||
'platform': 'demo'
|
'platform': 'demo'
|
||||||
}]
|
}]
|
||||||
}))
|
}))
|
||||||
|
loop.run_until_complete(
|
||||||
|
setup.async_setup_component(hass, switch.DOMAIN, {
|
||||||
|
'switch': [{
|
||||||
|
'platform': 'demo'
|
||||||
|
}]
|
||||||
|
}))
|
||||||
loop.run_until_complete(
|
loop.run_until_complete(
|
||||||
setup.async_setup_component(hass, cover.DOMAIN, {
|
setup.async_setup_component(hass, cover.DOMAIN, {
|
||||||
'cover': [{
|
'cover': [{
|
||||||
@ -93,6 +99,16 @@ def hass_fixture(loop, hass):
|
|||||||
ceiling_lights_entity.state,
|
ceiling_lights_entity.state,
|
||||||
attributes=attrs)
|
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
|
return hass
|
||||||
|
|
||||||
|
|
||||||
@ -188,6 +204,8 @@ def test_execute_request(hass_fixture, assistant_client):
|
|||||||
"commands": [{
|
"commands": [{
|
||||||
"devices": [{
|
"devices": [{
|
||||||
"id": "light.ceiling_lights",
|
"id": "light.ceiling_lights",
|
||||||
|
}, {
|
||||||
|
"id": "switch.decorative_lights",
|
||||||
}, {
|
}, {
|
||||||
"id": "light.bed_light",
|
"id": "light.bed_light",
|
||||||
}],
|
}],
|
||||||
@ -209,6 +227,7 @@ def test_execute_request(hass_fixture, assistant_client):
|
|||||||
body = yield from result.json()
|
body = yield from result.json()
|
||||||
assert body.get('requestId') == reqid
|
assert body.get('requestId') == reqid
|
||||||
commands = body['payload']['commands']
|
commands = body['payload']['commands']
|
||||||
assert len(commands) == 2
|
assert len(commands) == 3
|
||||||
ceiling = hass_fixture.states.get('light.ceiling_lights')
|
ceiling = hass_fixture.states.get('light.ceiling_lights')
|
||||||
assert ceiling.state == 'off'
|
assert ceiling.state == 'off'
|
||||||
|
assert hass_fixture.states.get('switch.decorative_lights').state == 'off'
|
||||||
|
@ -15,6 +15,16 @@ DETERMINE_SERVICE_TESTS = [{ # Test light brightness
|
|||||||
const.SERVICE_TURN_ON,
|
const.SERVICE_TURN_ON,
|
||||||
{'entity_id': 'light.test', 'brightness': 242}
|
{'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
|
}, { # Test light on / off
|
||||||
'entity_id': 'light.test',
|
'entity_id': 'light.test',
|
||||||
'command': ga.const.COMMAND_ONOFF,
|
'command': ga.const.COMMAND_ONOFF,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user