Skip to content
Merged

108.9 #34655

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions homeassistant/components/cloud/alexa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,25 @@ async def _handle_entity_registry_updated(self, event):
if not self.enabled or not self._cloud.is_logged_in:
return

action = event.data["action"]
entity_id = event.data["entity_id"]

if not self.should_expose(entity_id):
return

action = event.data["action"]
to_update = []
to_remove = []

if action == "create" and self.should_expose(entity_id):
if action == "create":
to_update.append(entity_id)
elif action == "remove" and self.should_expose(entity_id):
elif action == "remove":
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:
await self._sync_helper(to_update, to_remove)
Expand Down
21 changes: 14 additions & 7 deletions homeassistant/components/cloud/google_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ def __init__(self, hass, config, cloud_user, prefs, cloud):
self._cur_entity_prefs = self._prefs.google_entity_configs
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
def enabled(self):
"""Return if Google is enabled."""
Expand Down Expand Up @@ -83,6 +77,13 @@ async def async_initialize(self):
# Remove bad data that was there until 0.103.6 - Jan 6, 2020
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):
"""If a state object should be exposed."""
return self._should_expose_entity_id(state.entity_id)
Expand Down Expand Up @@ -160,8 +161,14 @@ async def _handle_entity_registry_updated(self, event):
if not self.enabled or not self._cloud.is_logged_in:
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"]

# Schedule a sync if a change was made to an entity that Google knows about
if self._should_expose_entity_id(entity_id):
await self.async_sync_entities_all()
self.async_schedule_google_sync_all()
2 changes: 1 addition & 1 deletion homeassistant/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Constants used by Home Assistant components."""
MAJOR_VERSION = 0
MINOR_VERSION = 108
PATCH_VERSION = "8"
PATCH_VERSION = "9"
__short_version__ = f"{MAJOR_VERSION}.{MINOR_VERSION}"
__version__ = f"{__short_version__}.{PATCH_VERSION}"
REQUIRED_PYTHON_VER = (3, 7, 0)
Expand Down
12 changes: 12 additions & 0 deletions homeassistant/helpers/entity_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
STORAGE_VERSION = 1
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)
class RegistryEntry:
Expand Down
11 changes: 11 additions & 0 deletions tests/components/cloud/test_alexa_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,21 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
"action": "update",
"entity_id": "light.kitchen",
"changes": ["entity_id"],
"old_entity_id": "light.living_room",
},
)
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_remove == []

Expand Down
30 changes: 19 additions & 11 deletions tests/components/cloud/test_google_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""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.google_config import CloudGoogleConfig
Expand Down Expand Up @@ -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")

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):
# Created entity
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "create", "entity_id": "light.kitchen"},
)
await hass.async_block_till_done()

assert len(mock_sync.mock_calls) == 1
assert len(mock_sync.mock_calls) == 1

with patch.object(
config, "async_sync_entities", side_effect=mock_coro
) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0):
# Removed entity
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "remove", "entity_id": "light.kitchen"},
)
await hass.async_block_till_done()

assert len(mock_sync.mock_calls) == 1
assert len(mock_sync.mock_calls) == 2

with patch.object(
config, "async_sync_entities", side_effect=mock_coro
) as mock_sync, patch.object(ga_helpers, "SYNC_DELAY", 0):
# Entity registry updated with relevant changes
hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED,
{
Expand All @@ -138,4 +137,13 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
)
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