Notify Alexa when exposed entities change (#24609)

This commit is contained in:
Paulus Schoutsen 2019-06-19 01:06:29 -07:00 committed by Pascal Vizeli
parent a89c8eeabe
commit 6d9f1b3fd3
12 changed files with 436 additions and 68 deletions

View file

@ -1,4 +1,5 @@
"""Test the cloud.iot module."""
import contextlib
from unittest.mock import patch, MagicMock
from aiohttp import web
@ -11,8 +12,10 @@ from homeassistant.components.cloud import (
DOMAIN, ALEXA_SCHEMA, client)
from homeassistant.components.cloud.const import (
PREF_ENABLE_ALEXA, PREF_ENABLE_GOOGLE)
from homeassistant.util.dt import utcnow
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from tests.components.alexa import test_smart_home as test_alexa
from tests.common import mock_coro
from tests.common import mock_coro, async_fire_time_changed
from . import mock_cloud_prefs
@ -292,3 +295,93 @@ async def test_alexa_config_report_state(hass, cloud_prefs):
assert cloud_prefs.alexa_report_state is False
assert conf.should_report_state is False
assert conf.is_reporting_states is False
@contextlib.contextmanager
def patch_sync_helper():
"""Patch sync helper.
In Py3.7 this would have been an async context manager.
"""
to_update = []
to_remove = []
with patch(
'homeassistant.components.cloud.client.SYNC_DELAY', 0
), patch(
'homeassistant.components.cloud.client.AlexaConfig._sync_helper',
side_effect=mock_coro
) as mock_helper:
yield to_update, to_remove
actual_to_update, actual_to_remove = mock_helper.mock_calls[0][1]
to_update.extend(actual_to_update)
to_remove.extend(actual_to_remove)
async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs):
"""Test Alexa config responds to updating exposed entities."""
client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
with patch_sync_helper() as (to_update, to_remove):
await cloud_prefs.async_update_alexa_entity_config(
entity_id='light.kitchen', should_expose=True
)
await hass.async_block_till_done()
async_fire_time_changed(hass, utcnow())
await hass.async_block_till_done()
assert to_update == ['light.kitchen']
assert to_remove == []
with patch_sync_helper() as (to_update, to_remove):
await cloud_prefs.async_update_alexa_entity_config(
entity_id='light.kitchen', should_expose=False
)
await cloud_prefs.async_update_alexa_entity_config(
entity_id='binary_sensor.door', should_expose=True
)
await cloud_prefs.async_update_alexa_entity_config(
entity_id='sensor.temp', should_expose=True
)
await hass.async_block_till_done()
async_fire_time_changed(hass, utcnow())
await hass.async_block_till_done()
assert sorted(to_update) == ['binary_sensor.door', 'sensor.temp']
assert to_remove == ['light.kitchen']
async def test_alexa_entity_registry_sync(hass, cloud_prefs):
"""Test Alexa config responds to entity registry."""
client.AlexaConfig(hass, ALEXA_SCHEMA({}), cloud_prefs, None)
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
'action': 'create',
'entity_id': 'light.kitchen',
})
await hass.async_block_till_done()
assert to_update == ['light.kitchen']
assert to_remove == []
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
'action': 'remove',
'entity_id': 'light.kitchen',
})
await hass.async_block_till_done()
assert to_update == []
assert to_remove == ['light.kitchen']
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(EVENT_ENTITY_REGISTRY_UPDATED, {
'action': 'update',
'entity_id': 'light.kitchen',
})
await hass.async_block_till_done()
assert to_update == []
assert to_remove == []