Merge pull request #34655 from home-assistant/108.9

This commit is contained in:
Paulus Schoutsen 2020-04-24 10:59:28 -07:00 committed by GitHub
commit 79fdac6674
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 70 additions and 22 deletions

View File

@ -271,15 +271,25 @@ class AlexaConfig(alexa_config.AbstractConfig):
if not self.enabled or not self._cloud.is_logged_in: if not self.enabled or not self._cloud.is_logged_in:
return return
action = event.data["action"]
entity_id = event.data["entity_id"] entity_id = event.data["entity_id"]
if not self.should_expose(entity_id):
return
action = event.data["action"]
to_update = [] to_update = []
to_remove = [] to_remove = []
if action == "create" and self.should_expose(entity_id): if action == "create":
to_update.append(entity_id) to_update.append(entity_id)
elif action == "remove" and self.should_expose(entity_id): elif action == "remove":
to_remove.append(entity_id) to_remove.append(entity_id)
elif action == "update" and bool(
set(event.data["changes"]) & entity_registry.ENTITY_DESCRIBING_ATTRIBUTES
):
to_update.append(entity_id)
if "old_entity_id" in event.data:
to_remove.append(event.data["old_entity_id"])
try: try:
await self._sync_helper(to_update, to_remove) await self._sync_helper(to_update, to_remove)

View File

@ -33,12 +33,6 @@ class CloudGoogleConfig(AbstractConfig):
self._cur_entity_prefs = self._prefs.google_entity_configs self._cur_entity_prefs = self._prefs.google_entity_configs
self._sync_entities_lock = asyncio.Lock() self._sync_entities_lock = asyncio.Lock()
prefs.async_listen_updates(self._async_prefs_updated)
hass.bus.async_listen(
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
self._handle_entity_registry_updated,
)
@property @property
def enabled(self): def enabled(self):
"""Return if Google is enabled.""" """Return if Google is enabled."""
@ -83,6 +77,13 @@ class CloudGoogleConfig(AbstractConfig):
# Remove bad data that was there until 0.103.6 - Jan 6, 2020 # Remove bad data that was there until 0.103.6 - Jan 6, 2020
self._store.pop_agent_user_id(self._user) self._store.pop_agent_user_id(self._user)
self._prefs.async_listen_updates(self._async_prefs_updated)
self.hass.bus.async_listen(
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
self._handle_entity_registry_updated,
)
def should_expose(self, state): def should_expose(self, state):
"""If a state object should be exposed.""" """If a state object should be exposed."""
return self._should_expose_entity_id(state.entity_id) return self._should_expose_entity_id(state.entity_id)
@ -160,8 +161,14 @@ class CloudGoogleConfig(AbstractConfig):
if not self.enabled or not self._cloud.is_logged_in: if not self.enabled or not self._cloud.is_logged_in:
return return
# Only consider entity registry updates if info relevant for Google has changed
if event.data["action"] == "update" and not bool(
set(event.data["changes"]) & entity_registry.ENTITY_DESCRIBING_ATTRIBUTES
):
return
entity_id = event.data["entity_id"] entity_id = event.data["entity_id"]
# Schedule a sync if a change was made to an entity that Google knows about # Schedule a sync if a change was made to an entity that Google knows about
if self._should_expose_entity_id(entity_id): if self._should_expose_entity_id(entity_id):
await self.async_sync_entities_all() self.async_schedule_google_sync_all()

View File

@ -1,7 +1,7 @@
"""Constants used by Home Assistant components.""" """Constants used by Home Assistant components."""
MAJOR_VERSION = 0 MAJOR_VERSION = 0
MINOR_VERSION = 108 MINOR_VERSION = 108
PATCH_VERSION = "8" PATCH_VERSION = "9"
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}" __short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__ = f"{__short_version__}.{PATCH_VERSION}" __version__ = f"{__short_version__}.{PATCH_VERSION}"
REQUIRED_PYTHON_VER = (3, 7, 0) REQUIRED_PYTHON_VER = (3, 7, 0)

View File

@ -53,6 +53,18 @@ ATTR_RESTORED = "restored"
STORAGE_VERSION = 1 STORAGE_VERSION = 1
STORAGE_KEY = "core.entity_registry" STORAGE_KEY = "core.entity_registry"
# Attributes relevant to describing entity
# to external services.
ENTITY_DESCRIBING_ATTRIBUTES = {
"entity_id",
"name",
"original_name",
"capabilities",
"supported_features",
"device_class",
"unit_of_measurement",
}
@attr.s(slots=True, frozen=True) @attr.s(slots=True, frozen=True)
class RegistryEntry: class RegistryEntry:

View File

@ -165,10 +165,21 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
"action": "update", "action": "update",
"entity_id": "light.kitchen", "entity_id": "light.kitchen",
"changes": ["entity_id"], "changes": ["entity_id"],
"old_entity_id": "light.living_room",
}, },
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert to_update == ["light.kitchen"]
assert to_remove == ["light.living_room"]
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]},
)
await hass.async_block_till_done()
assert to_update == [] assert to_update == []
assert to_remove == [] assert to_remove == []

View File

@ -1,5 +1,7 @@
"""Test the Cloud Google Config.""" """Test the Cloud Google Config."""
from unittest.mock import Mock, patch from unittest.mock import Mock
from asynctest import patch
from homeassistant.components.cloud import GACTIONS_SCHEMA from homeassistant.components.cloud import GACTIONS_SCHEMA
from homeassistant.components.cloud.google_config import CloudGoogleConfig from homeassistant.components.cloud.google_config import CloudGoogleConfig
@ -104,30 +106,27 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
await config.async_connect_agent_user("mock-user-id") await config.async_connect_agent_user("mock-user-id")
with patch.object( with patch.object(
config, "async_sync_entities", side_effect=mock_coro config, "async_schedule_google_sync_all", side_effect=mock_coro
) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0): ) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0):
# Created entity
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "create", "entity_id": "light.kitchen"}, {"action": "create", "entity_id": "light.kitchen"},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(mock_sync.mock_calls) == 1 assert len(mock_sync.mock_calls) == 1
with patch.object( # Removed entity
config, "async_sync_entities", side_effect=mock_coro
) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "remove", "entity_id": "light.kitchen"}, {"action": "remove", "entity_id": "light.kitchen"},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(mock_sync.mock_calls) == 1 assert len(mock_sync.mock_calls) == 2
with patch.object( # Entity registry updated with relevant changes
config, "async_sync_entities", side_effect=mock_coro
) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, EVENT_ENTITY_REGISTRY_UPDATED,
{ {
@ -138,4 +137,13 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(mock_sync.mock_calls) == 1 assert len(mock_sync.mock_calls) == 3
# Entity registry updated with non-relevant changes
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]},
)
await hass.async_block_till_done()
assert len(mock_sync.mock_calls) == 3