Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-run expose entities migration if first time failed #92564

Merged
merged 3 commits into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion homeassistant/components/cloud/alexa_config.py
Expand Up @@ -220,8 +220,13 @@ async def async_initialize(self):

async def on_hass_started(hass):
if self._prefs.alexa_settings_version != ALEXA_SETTINGS_VERSION:
if self._prefs.alexa_settings_version < 2:
if self._prefs.alexa_settings_version < 2 or (
# Recover from a bug we had in 2023.5.0 where entities didn't get exposed
self._prefs.alexa_settings_version < 3
and len(async_get_assistant_settings(hass, CLOUD_ALEXA)) == 0
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should count number of exposed entities instead

):
self._migrate_alexa_entity_settings_v1()

await self._prefs.async_update(
alexa_settings_version=ALEXA_SETTINGS_VERSION
)
Expand Down
8 changes: 7 additions & 1 deletion homeassistant/components/cloud/google_config.py
Expand Up @@ -12,6 +12,7 @@
from homeassistant.components.google_assistant.helpers import AbstractConfig
from homeassistant.components.homeassistant.exposed_entities import (
async_expose_entity,
async_get_assistant_settings,
async_get_entity_settings,
async_listen_entity_updates,
async_set_assistant_option,
Expand Down Expand Up @@ -213,8 +214,13 @@ async def async_initialize(self):

async def on_hass_started(hass: HomeAssistant) -> None:
if self._prefs.google_settings_version != GOOGLE_SETTINGS_VERSION:
if self._prefs.google_settings_version < 2:
if self._prefs.google_settings_version < 2 or (
# Recover from a bug we had in 2023.5.0 where entities didn't get exposed
self._prefs.google_settings_version < 3
and len(async_get_assistant_settings(hass, CLOUD_GOOGLE)) == 0
):
self._migrate_google_entity_settings_v1()

await self._prefs.async_update(
google_settings_version=GOOGLE_SETTINGS_VERSION
)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/cloud/prefs.py
Expand Up @@ -41,8 +41,8 @@
STORAGE_VERSION = 1
STORAGE_VERSION_MINOR = 2

ALEXA_SETTINGS_VERSION = 2
GOOGLE_SETTINGS_VERSION = 2
ALEXA_SETTINGS_VERSION = 3
GOOGLE_SETTINGS_VERSION = 3


class CloudPreferencesStore(Store):
Expand Down
18 changes: 3 additions & 15 deletions tests/components/cloud/test_alexa_config.py
Expand Up @@ -542,11 +542,13 @@ async def test_alexa_handle_logout(
assert len(mock_enable.return_value.mock_calls) == 1


@pytest.mark.parametrize("alexa_settings_version", [1, 2])
async def test_alexa_config_migrate_expose_entity_prefs(
hass: HomeAssistant,
cloud_prefs: CloudPreferences,
cloud_stub,
entity_registry: er.EntityRegistry,
alexa_settings_version: int,
) -> None:
"""Test migrating Alexa entity config."""
hass.state = CoreState.starting
Expand All @@ -560,13 +562,6 @@ async def test_alexa_config_migrate_expose_entity_prefs(
suggested_object_id="exposed",
)

entity_migrated = entity_registry.async_get_or_create(
"light",
"test",
"light_migrated",
suggested_object_id="migrated",
)

entity_config = entity_registry.async_get_or_create(
"light",
"test",
Expand All @@ -593,9 +588,8 @@ async def test_alexa_config_migrate_expose_entity_prefs(
await cloud_prefs.async_update(
alexa_enabled=True,
alexa_report_state=False,
alexa_settings_version=1,
alexa_settings_version=alexa_settings_version,
)
expose_entity(hass, entity_migrated.entity_id, False)

cloud_prefs._prefs[PREF_ALEXA_ENTITY_CONFIGS]["light.unknown"] = {
PREF_SHOULD_EXPOSE: True
Expand All @@ -606,9 +600,6 @@ async def test_alexa_config_migrate_expose_entity_prefs(
cloud_prefs._prefs[PREF_ALEXA_ENTITY_CONFIGS][entity_exposed.entity_id] = {
PREF_SHOULD_EXPOSE: True
}
cloud_prefs._prefs[PREF_ALEXA_ENTITY_CONFIGS][entity_migrated.entity_id] = {
PREF_SHOULD_EXPOSE: True
}
conf = alexa_config.CloudAlexaConfig(
hass, ALEXA_SCHEMA({}), "mock-user-id", cloud_prefs, cloud_stub
)
Expand All @@ -627,9 +618,6 @@ async def test_alexa_config_migrate_expose_entity_prefs(
assert async_get_entity_settings(hass, entity_exposed.entity_id) == {
"cloud.alexa": {"should_expose": True}
}
assert async_get_entity_settings(hass, entity_migrated.entity_id) == {
"cloud.alexa": {"should_expose": True}
}
assert async_get_entity_settings(hass, entity_config.entity_id) == {
"cloud.alexa": {"should_expose": False}
}
Expand Down
18 changes: 3 additions & 15 deletions tests/components/cloud/test_google_config.py
Expand Up @@ -483,10 +483,12 @@ async def test_google_handle_logout(
assert len(mock_enable.return_value.mock_calls) == 1


@pytest.mark.parametrize("google_settings_version", [1, 2])
async def test_google_config_migrate_expose_entity_prefs(
hass: HomeAssistant,
cloud_prefs: CloudPreferences,
entity_registry: er.EntityRegistry,
google_settings_version: int,
) -> None:
"""Test migrating Google entity config."""
hass.state = CoreState.starting
Expand All @@ -507,13 +509,6 @@ async def test_google_config_migrate_expose_entity_prefs(
suggested_object_id="no_2fa_exposed",
)

entity_migrated = entity_registry.async_get_or_create(
"light",
"test",
"light_migrated",
suggested_object_id="migrated",
)

entity_config = entity_registry.async_get_or_create(
"light",
"test",
Expand All @@ -540,9 +535,8 @@ async def test_google_config_migrate_expose_entity_prefs(
await cloud_prefs.async_update(
google_enabled=True,
google_report_state=False,
google_settings_version=1,
google_settings_version=google_settings_version,
)
expose_entity(hass, entity_migrated.entity_id, False)

cloud_prefs._prefs[PREF_GOOGLE_ENTITY_CONFIGS]["light.unknown"] = {
PREF_SHOULD_EXPOSE: True,
Expand All @@ -558,9 +552,6 @@ async def test_google_config_migrate_expose_entity_prefs(
PREF_SHOULD_EXPOSE: True,
PREF_DISABLE_2FA: True,
}
cloud_prefs._prefs[PREF_GOOGLE_ENTITY_CONFIGS][entity_migrated.entity_id] = {
PREF_SHOULD_EXPOSE: True
}
conf = CloudGoogleConfig(
hass, GACTIONS_SCHEMA({}), "mock-user-id", cloud_prefs, Mock(is_logged_in=False)
)
Expand All @@ -579,9 +570,6 @@ async def test_google_config_migrate_expose_entity_prefs(
assert async_get_entity_settings(hass, entity_exposed.entity_id) == {
"cloud.google_assistant": {"should_expose": True}
}
assert async_get_entity_settings(hass, entity_migrated.entity_id) == {
"cloud.google_assistant": {"should_expose": True}
}
assert async_get_entity_settings(hass, entity_no_2fa_exposed.entity_id) == {
"cloud.google_assistant": {"disable_2fa": True, "should_expose": True}
}
Expand Down