Implement entity and domain exclude/include for Alexa (#10647)

* Implement entity and domain exclude/include for Alexa

* Switch to using generate_filter

* Use proper domain for turn on/off calls except for groups where we must use the generic homeassistant.turn_on/off

* travis fixes

* Untangle

* Lint
This commit is contained in:
Robbie Trencheny authored and Paulus Schoutsen committed 2017-11-17 22:00:11 -08:00
1 parent 35699273da
commit 425c027085
7 files changed
+233 -75

No files matched your search

+129 -29
View File
@@ -5,9 +5,12 @@ from uuid import uuid4
import pytest
from homeassistant.components.alexa import smart_home
from homeassistant.helpers import entityfilter
from tests.common import async_mock_service
DEFAULT_CONFIG = smart_home.Config(filter=lambda entity_id: True)
def get_new_request(namespace, name, endpoint=None):
"""Generate a new API message."""
@@ -91,7 +94,7 @@ def test_wrong_version(hass):
msg['directive']['header']['payloadVersion'] = '2'
with pytest.raises(AssertionError):
yield from smart_home.async_handle_message(hass, msg)
yield from smart_home.async_handle_message(hass, DEFAULT_CONFIG, msg)
@asyncio.coroutine
@@ -157,7 +160,8 @@ def test_discovery_request(hass):
'position': 85
})
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -319,6 +323,67 @@ def test_discovery_request(hass):
raise AssertionError("Unknown appliance!")
@asyncio.coroutine
def test_exclude_filters(hass):
"""Test exclusion filters."""
request = get_new_request('Alexa.Discovery', 'Discover')
# setup test devices
hass.states.async_set(
'switch.test', 'on', {'friendly_name': "Test switch"})
hass.states.async_set(
'script.deny', 'off', {'friendly_name': "Blocked script"})
hass.states.async_set(
'cover.deny', 'off', {'friendly_name': "Blocked cover"})
config = smart_home.Config(filter=entityfilter.generate_filter(
include_domains=[],
include_entities=[],
exclude_domains=['script'],
exclude_entities=['cover.deny'],
))
msg = yield from smart_home.async_handle_message(hass, config, request)
msg = msg['event']
assert len(msg['payload']['endpoints']) == 1
@asyncio.coroutine
def test_include_filters(hass):
"""Test inclusion filters."""
request = get_new_request('Alexa.Discovery', 'Discover')
# setup test devices
hass.states.async_set(
'switch.deny', 'on', {'friendly_name': "Blocked switch"})
hass.states.async_set(
'script.deny', 'off', {'friendly_name': "Blocked script"})
hass.states.async_set(
'automation.allow', 'off', {'friendly_name': "Allowed automation"})
hass.states.async_set(
'group.allow', 'off', {'friendly_name': "Allowed group"})
config = smart_home.Config(filter=entityfilter.generate_filter(
include_domains=['automation', 'group'],
include_entities=['script.deny'],
exclude_domains=[],
exclude_entities=[],
))
msg = yield from smart_home.async_handle_message(hass, config, request)
msg = msg['event']
assert len(msg['payload']['endpoints']) == 3
@asyncio.coroutine
def test_api_entity_not_exists(hass):
"""Test api turn on process without entity."""
@@ -326,7 +391,8 @@ def test_api_entity_not_exists(hass):
call_switch = async_mock_service(hass, 'switch', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -341,7 +407,8 @@ def test_api_entity_not_exists(hass):
def test_api_function_not_implemented(hass):
"""Test api call that is not implemented to us."""
request = get_new_request('Alexa.HAHAAH', 'Sweet')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -366,9 +433,15 @@ def test_api_turn_on(hass, domain):
'friendly_name': "Test {}".format(domain)
})
call = async_mock_service(hass, 'homeassistant', 'turn_on')
call_domain = domain
msg = yield from smart_home.async_handle_message(hass, request)
if domain == 'group':
call_domain = 'homeassistant'
call = async_mock_service(hass, call_domain, 'turn_on')
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -393,9 +466,15 @@ def test_api_turn_off(hass, domain):
'friendly_name': "Test {}".format(domain)
})
call = async_mock_service(hass, 'homeassistant', 'turn_off')
call_domain = domain
msg = yield from smart_home.async_handle_message(hass, request)
if domain == 'group':
call_domain = 'homeassistant'
call = async_mock_service(hass, call_domain, 'turn_off')
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -420,7 +499,8 @@ def test_api_set_brightness(hass):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -450,7 +530,8 @@ def test_api_adjust_brightness(hass, result, adjust):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -483,7 +564,8 @@ def test_api_set_color_rgb(hass):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -516,7 +598,8 @@ def test_api_set_color_xy(hass):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -544,7 +627,8 @@ def test_api_set_color_temperature(hass):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -572,7 +656,8 @@ def test_api_decrease_color_temp(hass, result, initial):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -600,7 +685,8 @@ def test_api_increase_color_temp(hass, result, initial):
call_light = async_mock_service(hass, 'light', 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -626,7 +712,8 @@ def test_api_activate(hass, domain):
call = async_mock_service(hass, domain, 'turn_on')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -651,7 +738,8 @@ def test_api_set_percentage_fan(hass):
call_fan = async_mock_service(hass, 'fan', 'set_speed')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -679,7 +767,8 @@ def test_api_set_percentage_cover(hass):
call_cover = async_mock_service(hass, 'cover', 'set_cover_position')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -709,7 +798,8 @@ def test_api_adjust_percentage_fan(hass, result, adjust):
call_fan = async_mock_service(hass, 'fan', 'set_speed')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -740,7 +830,8 @@ def test_api_adjust_percentage_cover(hass, result, adjust):
call_cover = async_mock_service(hass, 'cover', 'set_cover_position')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -766,7 +857,8 @@ def test_api_lock(hass, domain):
call = async_mock_service(hass, domain, 'lock')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -791,7 +883,8 @@ def test_api_play(hass, domain):
call = async_mock_service(hass, domain, 'media_play')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -816,7 +909,8 @@ def test_api_pause(hass, domain):
call = async_mock_service(hass, domain, 'media_pause')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -841,7 +935,8 @@ def test_api_stop(hass, domain):
call = async_mock_service(hass, domain, 'media_stop')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -866,7 +961,8 @@ def test_api_next(hass, domain):
call = async_mock_service(hass, domain, 'media_next_track')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -891,7 +987,8 @@ def test_api_previous(hass, domain):
call = async_mock_service(hass, domain, 'media_previous_track')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -918,7 +1015,8 @@ def test_api_set_volume(hass):
call_media_player = async_mock_service(hass, 'media_player', 'volume_set')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -948,7 +1046,8 @@ def test_api_adjust_volume(hass, result, adjust):
call_media_player = async_mock_service(hass, 'media_player', 'volume_set')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']
@@ -976,7 +1075,8 @@ def test_api_mute(hass, domain):
call = async_mock_service(hass, domain, 'volume_mute')
msg = yield from smart_home.async_handle_message(hass, request)
msg = yield from smart_home.async_handle_message(
hass, DEFAULT_CONFIG, request)
assert 'event' in msg
msg = msg['event']