Handle logout prefs update for Google/Alexa (#56045)

This commit is contained in:
Paulus Schoutsen 2021-09-10 09:08:43 -07:00 committed by GitHub
parent 443147e132
commit dec7877671
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 12 deletions

View File

@ -56,12 +56,6 @@ class AlexaConfig(alexa_config.AbstractConfig):
self._alexa_sync_unsub = None
self._endpoint = None
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 Alexa is enabled."""
@ -114,6 +108,12 @@ class AlexaConfig(alexa_config.AbstractConfig):
start.async_at_start(self.hass, hass_started)
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, entity_id):
"""If an entity should be exposed."""
if entity_id in CLOUD_NEVER_EXPOSED_ENTITIES:
@ -171,6 +171,15 @@ class AlexaConfig(alexa_config.AbstractConfig):
async def _async_prefs_updated(self, prefs):
"""Handle updated preferences."""
if not self._cloud.is_logged_in:
if self.is_reporting_states:
await self.async_disable_proactive_mode()
if self._alexa_sync_unsub:
self._alexa_sync_unsub()
self._alexa_sync_unsub = None
return
if ALEXA_DOMAIN not in self.hass.config.components and self.enabled:
await async_setup_component(self.hass, ALEXA_DOMAIN, {})

View File

@ -172,6 +172,13 @@ class CloudGoogleConfig(AbstractConfig):
async def _async_prefs_updated(self, prefs):
"""Handle updated preferences."""
if not self._cloud.is_logged_in:
if self.is_reporting_state:
self.async_disable_report_state()
if self.is_local_sdk_active:
self.async_disable_local_sdk()
return
if self.enabled and GOOGLE_DOMAIN not in self.hass.config.components:
await async_setup_component(self.hass, GOOGLE_DOMAIN, {})

View File

@ -28,6 +28,7 @@ async def test_alexa_config_expose_entity_prefs(hass, cloud_prefs, cloud_stub):
conf = alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
await conf.async_initialize()
assert not conf.should_expose("light.kitchen")
entity_conf["should_expose"] = True
@ -50,6 +51,7 @@ async def test_alexa_config_report_state(hass, cloud_prefs, cloud_stub):
conf = alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
await conf.async_initialize()
assert cloud_prefs.alexa_report_state is False
assert conf.should_report_state is False
@ -131,9 +133,9 @@ def patch_sync_helper():
async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs, cloud_stub):
"""Test Alexa config responds to updating exposed entities."""
alexa_config.AlexaConfig(
await alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
).async_initialize()
with patch_sync_helper() as (to_update, to_remove):
await cloud_prefs.async_update_alexa_entity_config(
@ -166,9 +168,9 @@ async def test_alexa_update_expose_trigger_sync(hass, cloud_prefs, cloud_stub):
async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
"""Test Alexa config responds to entity registry."""
alexa_config.AlexaConfig(
await alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, hass.data["cloud"]
)
).async_initialize()
with patch_sync_helper() as (to_update, to_remove):
hass.bus.async_fire(
@ -218,9 +220,9 @@ async def test_alexa_entity_registry_sync(hass, mock_cloud_login, cloud_prefs):
async def test_alexa_update_report_state(hass, cloud_prefs, cloud_stub):
"""Test Alexa config responds to reporting state."""
alexa_config.AlexaConfig(
await alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
).async_initialize()
with patch(
"homeassistant.components.cloud.alexa_config.AlexaConfig.async_sync_entities",
@ -244,3 +246,32 @@ def test_enabled_requires_valid_sub(hass, mock_expired_cloud_login, cloud_prefs)
)
assert not config.enabled
async def test_alexa_handle_logout(hass, cloud_prefs, cloud_stub):
"""Test Alexa config responds to logging out."""
aconf = alexa_config.AlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
await aconf.async_initialize()
with patch(
"homeassistant.components.alexa.config.async_enable_proactive_mode",
return_value=Mock(),
) as mock_enable:
await aconf.async_enable_proactive_mode()
# This will trigger a prefs update when we logout.
await cloud_prefs.get_cloud_user()
cloud_stub.is_logged_in = False
with patch.object(
cloud_stub.auth,
"async_check_token",
side_effect=AssertionError("Should not be called"),
):
await cloud_prefs.async_set_username(None)
await hass.async_block_till_done()
assert len(mock_enable.return_value.mock_calls) == 1

View File

@ -264,3 +264,32 @@ async def test_setup_integration(hass, mock_conf, cloud_prefs):
await cloud_prefs.async_update()
await hass.async_block_till_done()
assert "google_assistant" in hass.config.components
async def test_google_handle_logout(hass, cloud_prefs, mock_cloud_login):
"""Test Google config responds to logging out."""
gconf = CloudGoogleConfig(
hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, Mock(is_logged_in=False)
)
await gconf.async_initialize()
with patch(
"homeassistant.components.google_assistant.report_state.async_enable_report_state",
) as mock_enable:
gconf.async_enable_report_state()
assert len(mock_enable.mock_calls) == 1
# This will trigger a prefs update when we logout.
await cloud_prefs.get_cloud_user()
with patch.object(
hass.data["cloud"].auth,
"async_check_token",
side_effect=AssertionError("Should not be called"),
):
await cloud_prefs.async_set_username(None)
await hass.async_block_till_done()
assert len(mock_enable.return_value.mock_calls) == 1