Exclude hidden entities from cloud (#68557)

This commit is contained in:
Erik Montnemery 2022-03-23 12:42:45 +01:00 committed by GitHub
parent dc8e87a6f7
commit ff7d5c92d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 17 deletions

View File

@ -133,7 +133,10 @@ class CloudAlexaConfig(alexa_config.AbstractConfig):
entity_registry = er.async_get(self.hass) entity_registry = er.async_get(self.hass)
if registry_entry := entity_registry.async_get(entity_id): if registry_entry := entity_registry.async_get(entity_id):
auxiliary_entity = registry_entry.entity_category is not None auxiliary_entity = (
registry_entry.entity_category is not None
or registry_entry.hidden_by is not None
)
else: else:
auxiliary_entity = False auxiliary_entity = False

View File

@ -124,7 +124,10 @@ class CloudGoogleConfig(AbstractConfig):
entity_registry = er.async_get(self.hass) entity_registry = er.async_get(self.hass)
if registry_entry := entity_registry.async_get(entity_id): if registry_entry := entity_registry.async_get(entity_id):
auxiliary_entity = registry_entry.entity_category is not None auxiliary_entity = (
registry_entry.entity_category is not None
or registry_entry.hidden_by is not None
)
else: else:
auxiliary_entity = False auxiliary_entity = False

View File

@ -6,8 +6,8 @@ import pytest
from homeassistant.components.alexa import errors from homeassistant.components.alexa import errors
from homeassistant.components.cloud import ALEXA_SCHEMA, alexa_config from homeassistant.components.cloud import ALEXA_SCHEMA, alexa_config
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed, mock_registry from tests.common import async_fire_time_changed, mock_registry
@ -44,6 +44,20 @@ async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs, cloud_stub):
suggested_object_id="system_light", suggested_object_id="system_light",
entity_category="system", entity_category="system",
) )
entity_entry4 = entity_registry.async_get_or_create(
"light",
"test",
"light_hidden_integration_id",
suggested_object_id="hidden_integration_light",
hidden_by=er.RegistryEntryHider.INTEGRATION,
)
entity_entry5 = entity_registry.async_get_or_create(
"light",
"test",
"light_hidden_user_id",
suggested_object_id="hidden_user_light",
hidden_by=er.RegistryEntryHider.USER,
)
entity_conf = {"should_expose": False} entity_conf = {"should_expose": False}
await cloud_prefs.async_update( await cloud_prefs.async_update(
@ -61,20 +75,26 @@ async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs, cloud_stub):
assert not conf.should_expose(entity_entry1.entity_id) assert not conf.should_expose(entity_entry1.entity_id)
assert not conf.should_expose(entity_entry2.entity_id) assert not conf.should_expose(entity_entry2.entity_id)
assert not conf.should_expose(entity_entry3.entity_id) assert not conf.should_expose(entity_entry3.entity_id)
assert not conf.should_expose(entity_entry4.entity_id)
assert not conf.should_expose(entity_entry5.entity_id)
entity_conf["should_expose"] = True entity_conf["should_expose"] = True
assert conf.should_expose("light.kitchen") assert conf.should_expose("light.kitchen")
# config and diagnostic entities should not be exposed # categorized and hidden entities should not be exposed
assert not conf.should_expose(entity_entry1.entity_id) assert not conf.should_expose(entity_entry1.entity_id)
assert not conf.should_expose(entity_entry2.entity_id) assert not conf.should_expose(entity_entry2.entity_id)
assert not conf.should_expose(entity_entry3.entity_id) assert not conf.should_expose(entity_entry3.entity_id)
assert not conf.should_expose(entity_entry4.entity_id)
assert not conf.should_expose(entity_entry5.entity_id)
entity_conf["should_expose"] = None entity_conf["should_expose"] = None
assert conf.should_expose("light.kitchen") assert conf.should_expose("light.kitchen")
# config and diagnostic entities should not be exposed # categorized and hidden entities should not be exposed
assert not conf.should_expose(entity_entry1.entity_id) assert not conf.should_expose(entity_entry1.entity_id)
assert not conf.should_expose(entity_entry2.entity_id) assert not conf.should_expose(entity_entry2.entity_id)
assert not conf.should_expose(entity_entry3.entity_id) assert not conf.should_expose(entity_entry3.entity_id)
assert not conf.should_expose(entity_entry4.entity_id)
assert not conf.should_expose(entity_entry5.entity_id)
assert "alexa" not in hass.config.components assert "alexa" not in hass.config.components
await cloud_prefs.async_update( await cloud_prefs.async_update(
@ -324,7 +344,7 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
with patch_sync_helper() as (to_update, to_remove): with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.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()
@ -334,7 +354,7 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
with patch_sync_helper() as (to_update, to_remove): with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.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()
@ -344,7 +364,7 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
with patch_sync_helper() as (to_update, to_remove): with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.EVENT_ENTITY_REGISTRY_UPDATED,
{ {
"action": "update", "action": "update",
"entity_id": "light.kitchen", "entity_id": "light.kitchen",
@ -359,7 +379,7 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
with patch_sync_helper() as (to_update, to_remove): with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]}, {"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]},
) )
await hass.async_block_till_done() await hass.async_block_till_done()

View File

@ -9,7 +9,7 @@ from homeassistant.components.cloud.google_config import CloudGoogleConfig
from homeassistant.components.google_assistant import helpers as ga_helpers from homeassistant.components.google_assistant import helpers as ga_helpers
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import CoreState, State from homeassistant.core import CoreState, State
from homeassistant.helpers.entity_registry import EVENT_ENTITY_REGISTRY_UPDATED from homeassistant.helpers import entity_registry as er
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from tests.common import async_fire_time_changed, mock_registry from tests.common import async_fire_time_changed, mock_registry
@ -141,7 +141,7 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
): ):
# Created entity # Created entity
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.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()
@ -150,7 +150,7 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
# Removed entity # Removed entity
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.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()
@ -159,7 +159,7 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
# Entity registry updated with relevant changes # Entity registry updated with relevant changes
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.EVENT_ENTITY_REGISTRY_UPDATED,
{ {
"action": "update", "action": "update",
"entity_id": "light.kitchen", "entity_id": "light.kitchen",
@ -172,7 +172,7 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
# Entity registry updated with non-relevant changes # Entity registry updated with non-relevant changes
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.EVENT_ENTITY_REGISTRY_UPDATED,
{"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]}, {"action": "update", "entity_id": "light.kitchen", "changes": ["icon"]},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
@ -182,7 +182,7 @@ async def test_google_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
# When hass is not started yet we wait till started # When hass is not started yet we wait till started
hass.state = CoreState.starting hass.state = CoreState.starting
hass.bus.async_fire( hass.bus.async_fire(
EVENT_ENTITY_REGISTRY_UPDATED, er.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()
@ -243,6 +243,20 @@ async def test_google_config_expose_entity_prefs(hass, mock_conf, cloud_prefs):
suggested_object_id="system_light", suggested_object_id="system_light",
entity_category="system", entity_category="system",
) )
entity_entry4 = entity_registry.async_get_or_create(
"light",
"test",
"light_hidden_integration_id",
suggested_object_id="hidden_integration_light",
hidden_by=er.RegistryEntryHider.INTEGRATION,
)
entity_entry5 = entity_registry.async_get_or_create(
"light",
"test",
"light_hidden_user_id",
suggested_object_id="hidden_user_light",
hidden_by=er.RegistryEntryHider.USER,
)
entity_conf = {"should_expose": False} entity_conf = {"should_expose": False}
await cloud_prefs.async_update( await cloud_prefs.async_update(
@ -254,25 +268,33 @@ async def test_google_config_expose_entity_prefs(hass, mock_conf, cloud_prefs):
state_config = State(entity_entry1.entity_id, "on") state_config = State(entity_entry1.entity_id, "on")
state_diagnostic = State(entity_entry2.entity_id, "on") state_diagnostic = State(entity_entry2.entity_id, "on")
state_system = State(entity_entry3.entity_id, "on") state_system = State(entity_entry3.entity_id, "on")
state_hidden_integration = State(entity_entry4.entity_id, "on")
state_hidden_user = State(entity_entry5.entity_id, "on")
assert not mock_conf.should_expose(state) assert not mock_conf.should_expose(state)
assert not mock_conf.should_expose(state_config) assert not mock_conf.should_expose(state_config)
assert not mock_conf.should_expose(state_diagnostic) assert not mock_conf.should_expose(state_diagnostic)
assert not mock_conf.should_expose(state_system) assert not mock_conf.should_expose(state_system)
assert not mock_conf.should_expose(state_hidden_integration)
assert not mock_conf.should_expose(state_hidden_user)
entity_conf["should_expose"] = True entity_conf["should_expose"] = True
assert mock_conf.should_expose(state) assert mock_conf.should_expose(state)
# config and diagnostic entities should not be exposed # categorized and hidden entities should not be exposed
assert not mock_conf.should_expose(state_config) assert not mock_conf.should_expose(state_config)
assert not mock_conf.should_expose(state_diagnostic) assert not mock_conf.should_expose(state_diagnostic)
assert not mock_conf.should_expose(state_system) assert not mock_conf.should_expose(state_system)
assert not mock_conf.should_expose(state_hidden_integration)
assert not mock_conf.should_expose(state_hidden_user)
entity_conf["should_expose"] = None entity_conf["should_expose"] = None
assert mock_conf.should_expose(state) assert mock_conf.should_expose(state)
# config and diagnostic entities should not be exposed # categorized and hidden entities should not be exposed
assert not mock_conf.should_expose(state_config) assert not mock_conf.should_expose(state_config)
assert not mock_conf.should_expose(state_diagnostic) assert not mock_conf.should_expose(state_diagnostic)
assert not mock_conf.should_expose(state_system) assert not mock_conf.should_expose(state_system)
assert not mock_conf.should_expose(state_hidden_integration)
assert not mock_conf.should_expose(state_hidden_user)
await cloud_prefs.async_update( await cloud_prefs.async_update(
google_default_expose=["sensor"], google_default_expose=["sensor"],