mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Correct Google generative AI config entry migration (#147856)
This commit is contained in:
parent
5554e38171
commit
c42fc818bf
@ -308,4 +308,50 @@ async def async_migrate_integration(hass: HomeAssistant) -> None:
|
|||||||
title=DEFAULT_TITLE,
|
title=DEFAULT_TITLE,
|
||||||
options={},
|
options={},
|
||||||
version=2,
|
version=2,
|
||||||
|
minor_version=2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_migrate_entry(
|
||||||
|
hass: HomeAssistant, entry: GoogleGenerativeAIConfigEntry
|
||||||
|
) -> bool:
|
||||||
|
"""Migrate entry."""
|
||||||
|
LOGGER.debug("Migrating from version %s:%s", entry.version, entry.minor_version)
|
||||||
|
|
||||||
|
if entry.version > 2:
|
||||||
|
# This means the user has downgraded from a future version
|
||||||
|
return False
|
||||||
|
|
||||||
|
if entry.version == 2 and entry.minor_version == 1:
|
||||||
|
# Add TTS subentry which was missing in 2025.7.0b0
|
||||||
|
if not any(
|
||||||
|
subentry.subentry_type == "tts" for subentry in entry.subentries.values()
|
||||||
|
):
|
||||||
|
hass.config_entries.async_add_subentry(
|
||||||
|
entry,
|
||||||
|
ConfigSubentry(
|
||||||
|
data=MappingProxyType(RECOMMENDED_TTS_OPTIONS),
|
||||||
|
subentry_type="tts",
|
||||||
|
title=DEFAULT_TTS_NAME,
|
||||||
|
unique_id=None,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Correct broken device migration in Home Assistant Core 2025.7.0b0-2025.7.0b1
|
||||||
|
device_registry = dr.async_get(hass)
|
||||||
|
for device in dr.async_entries_for_config_entry(
|
||||||
|
device_registry, entry.entry_id
|
||||||
|
):
|
||||||
|
device_registry.async_update_device(
|
||||||
|
device.id,
|
||||||
|
remove_config_entry_id=entry.entry_id,
|
||||||
|
remove_config_subentry_id=None,
|
||||||
|
)
|
||||||
|
|
||||||
|
hass.config_entries.async_update_entry(entry, minor_version=2)
|
||||||
|
|
||||||
|
LOGGER.debug(
|
||||||
|
"Migration to version %s:%s successful", entry.version, entry.minor_version
|
||||||
|
)
|
||||||
|
|
||||||
|
return True
|
||||||
|
@ -92,6 +92,7 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Handle a config flow for Google Generative AI Conversation."""
|
"""Handle a config flow for Google Generative AI Conversation."""
|
||||||
|
|
||||||
VERSION = 2
|
VERSION = 2
|
||||||
|
MINOR_VERSION = 2
|
||||||
|
|
||||||
async def async_step_api(
|
async def async_step_api(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
@ -13,7 +13,7 @@ from homeassistant.components.google_generative_ai_conversation.const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
RECOMMENDED_TTS_OPTIONS,
|
RECOMMENDED_TTS_OPTIONS,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState, ConfigSubentryData
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
@ -473,6 +473,7 @@ async def test_migration_from_v1_to_v2(
|
|||||||
assert len(entries) == 1
|
assert len(entries) == 1
|
||||||
entry = entries[0]
|
entry = entries[0]
|
||||||
assert entry.version == 2
|
assert entry.version == 2
|
||||||
|
assert entry.minor_version == 2
|
||||||
assert not entry.options
|
assert not entry.options
|
||||||
assert entry.title == DEFAULT_TITLE
|
assert entry.title == DEFAULT_TITLE
|
||||||
assert len(entry.subentries) == 3
|
assert len(entry.subentries) == 3
|
||||||
@ -618,6 +619,7 @@ async def test_migration_from_v1_to_v2_with_multiple_keys(
|
|||||||
|
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
assert entry.version == 2
|
assert entry.version == 2
|
||||||
|
assert entry.minor_version == 2
|
||||||
assert not entry.options
|
assert not entry.options
|
||||||
assert entry.title == DEFAULT_TITLE
|
assert entry.title == DEFAULT_TITLE
|
||||||
assert len(entry.subentries) == 2
|
assert len(entry.subentries) == 2
|
||||||
@ -716,6 +718,7 @@ async def test_migration_from_v1_to_v2_with_same_keys(
|
|||||||
assert len(entries) == 1
|
assert len(entries) == 1
|
||||||
entry = entries[0]
|
entry = entries[0]
|
||||||
assert entry.version == 2
|
assert entry.version == 2
|
||||||
|
assert entry.minor_version == 2
|
||||||
assert not entry.options
|
assert not entry.options
|
||||||
assert entry.title == DEFAULT_TITLE
|
assert entry.title == DEFAULT_TITLE
|
||||||
assert len(entry.subentries) == 3
|
assert len(entry.subentries) == 3
|
||||||
@ -784,6 +787,218 @@ async def test_migration_from_v1_to_v2_with_same_keys(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("device_changes", "extra_subentries", "expected_device_subentries"),
|
||||||
|
[
|
||||||
|
# Scenario where we have a v2.1 config entry migrated by HA Core 2025.7.0b0:
|
||||||
|
# Wrong device registry, no TTS subentry
|
||||||
|
(
|
||||||
|
{"add_config_entry_id": "mock_entry_id", "add_config_subentry_id": None},
|
||||||
|
[],
|
||||||
|
{"mock_entry_id": {None, "mock_id_1"}},
|
||||||
|
),
|
||||||
|
# Scenario where we have a v2.1 config entry migrated by HA Core 2025.7.0b1:
|
||||||
|
# Wrong device registry, TTS subentry created
|
||||||
|
(
|
||||||
|
{"add_config_entry_id": "mock_entry_id", "add_config_subentry_id": None},
|
||||||
|
[
|
||||||
|
ConfigSubentryData(
|
||||||
|
data=RECOMMENDED_TTS_OPTIONS,
|
||||||
|
subentry_id="mock_id_3",
|
||||||
|
subentry_type="tts",
|
||||||
|
title=DEFAULT_TTS_NAME,
|
||||||
|
unique_id=None,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
{"mock_entry_id": {None, "mock_id_1"}},
|
||||||
|
),
|
||||||
|
# Scenario where we have a v2.1 config entry migrated by HA Core 2025.7.0b2
|
||||||
|
# or later: Correct device registry, TTS subentry created
|
||||||
|
(
|
||||||
|
{},
|
||||||
|
[
|
||||||
|
ConfigSubentryData(
|
||||||
|
data=RECOMMENDED_TTS_OPTIONS,
|
||||||
|
subentry_id="mock_id_3",
|
||||||
|
subentry_type="tts",
|
||||||
|
title=DEFAULT_TTS_NAME,
|
||||||
|
unique_id=None,
|
||||||
|
)
|
||||||
|
],
|
||||||
|
{"mock_entry_id": {"mock_id_1"}},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_migration_from_v2_1_to_v2_2(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
device_changes: dict[str, str],
|
||||||
|
extra_subentries: list[ConfigSubentryData],
|
||||||
|
expected_device_subentries: dict[str, set[str | None]],
|
||||||
|
) -> None:
|
||||||
|
"""Test migration from version 2.1 to version 2.2.
|
||||||
|
|
||||||
|
This tests we clean up the broken migration in Home Assistant Core
|
||||||
|
2025.7.0b0-2025.7.0b1:
|
||||||
|
- Fix device registry (Fixed in Home Assistant Core 2025.7.0b2)
|
||||||
|
- Add TTS subentry (Added in Home Assistant Core 2025.7.0b1)
|
||||||
|
"""
|
||||||
|
# Create a v2.1 config entry with 2 subentries, devices and entities
|
||||||
|
options = {
|
||||||
|
"recommended": True,
|
||||||
|
"llm_hass_api": ["assist"],
|
||||||
|
"prompt": "You are a helpful assistant",
|
||||||
|
"chat_model": "models/gemini-2.0-flash",
|
||||||
|
}
|
||||||
|
mock_config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={CONF_API_KEY: "1234"},
|
||||||
|
entry_id="mock_entry_id",
|
||||||
|
version=2,
|
||||||
|
minor_version=1,
|
||||||
|
subentries_data=[
|
||||||
|
ConfigSubentryData(
|
||||||
|
data=options,
|
||||||
|
subentry_id="mock_id_1",
|
||||||
|
subentry_type="conversation",
|
||||||
|
title="Google Generative AI",
|
||||||
|
unique_id=None,
|
||||||
|
),
|
||||||
|
ConfigSubentryData(
|
||||||
|
data=options,
|
||||||
|
subentry_id="mock_id_2",
|
||||||
|
subentry_type="conversation",
|
||||||
|
title="Google Generative AI 2",
|
||||||
|
unique_id=None,
|
||||||
|
),
|
||||||
|
*extra_subentries,
|
||||||
|
],
|
||||||
|
title="Google Generative AI",
|
||||||
|
)
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
device_1 = device_registry.async_get_or_create(
|
||||||
|
config_entry_id=mock_config_entry.entry_id,
|
||||||
|
config_subentry_id="mock_id_1",
|
||||||
|
identifiers={(DOMAIN, "mock_id_1")},
|
||||||
|
name="Google Generative AI",
|
||||||
|
manufacturer="Google",
|
||||||
|
model="Generative AI",
|
||||||
|
entry_type=dr.DeviceEntryType.SERVICE,
|
||||||
|
)
|
||||||
|
device_1 = device_registry.async_update_device(device_1.id, **device_changes)
|
||||||
|
assert device_1.config_entries_subentries == expected_device_subentries
|
||||||
|
entity_registry.async_get_or_create(
|
||||||
|
"conversation",
|
||||||
|
DOMAIN,
|
||||||
|
"mock_id_1",
|
||||||
|
config_entry=mock_config_entry,
|
||||||
|
config_subentry_id="mock_id_1",
|
||||||
|
device_id=device_1.id,
|
||||||
|
suggested_object_id="google_generative_ai_conversation",
|
||||||
|
)
|
||||||
|
|
||||||
|
device_2 = device_registry.async_get_or_create(
|
||||||
|
config_entry_id=mock_config_entry.entry_id,
|
||||||
|
config_subentry_id="mock_id_2",
|
||||||
|
identifiers={(DOMAIN, "mock_id_2")},
|
||||||
|
name="Google Generative AI 2",
|
||||||
|
manufacturer="Google",
|
||||||
|
model="Generative AI",
|
||||||
|
entry_type=dr.DeviceEntryType.SERVICE,
|
||||||
|
)
|
||||||
|
entity_registry.async_get_or_create(
|
||||||
|
"conversation",
|
||||||
|
DOMAIN,
|
||||||
|
"mock_id_2",
|
||||||
|
config_entry=mock_config_entry,
|
||||||
|
config_subentry_id="mock_id_2",
|
||||||
|
device_id=device_2.id,
|
||||||
|
suggested_object_id="google_generative_ai_conversation_2",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run migration
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.google_generative_ai_conversation.async_setup_entry",
|
||||||
|
return_value=True,
|
||||||
|
):
|
||||||
|
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
entries = hass.config_entries.async_entries(DOMAIN)
|
||||||
|
assert len(entries) == 1
|
||||||
|
entry = entries[0]
|
||||||
|
assert entry.version == 2
|
||||||
|
assert entry.minor_version == 2
|
||||||
|
assert not entry.options
|
||||||
|
assert entry.title == DEFAULT_TITLE
|
||||||
|
assert len(entry.subentries) == 3
|
||||||
|
conversation_subentries = [
|
||||||
|
subentry
|
||||||
|
for subentry in entry.subentries.values()
|
||||||
|
if subentry.subentry_type == "conversation"
|
||||||
|
]
|
||||||
|
assert len(conversation_subentries) == 2
|
||||||
|
for subentry in conversation_subentries:
|
||||||
|
assert subentry.subentry_type == "conversation"
|
||||||
|
assert subentry.data == options
|
||||||
|
assert "Google Generative AI" in subentry.title
|
||||||
|
tts_subentries = [
|
||||||
|
subentry
|
||||||
|
for subentry in entry.subentries.values()
|
||||||
|
if subentry.subentry_type == "tts"
|
||||||
|
]
|
||||||
|
assert len(tts_subentries) == 1
|
||||||
|
assert tts_subentries[0].data == RECOMMENDED_TTS_OPTIONS
|
||||||
|
assert tts_subentries[0].title == DEFAULT_TTS_NAME
|
||||||
|
|
||||||
|
subentry = conversation_subentries[0]
|
||||||
|
|
||||||
|
entity = entity_registry.async_get("conversation.google_generative_ai_conversation")
|
||||||
|
assert entity.unique_id == subentry.subentry_id
|
||||||
|
assert entity.config_subentry_id == subentry.subentry_id
|
||||||
|
assert entity.config_entry_id == entry.entry_id
|
||||||
|
|
||||||
|
assert not device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, mock_config_entry.entry_id)}
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
device := device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, subentry.subentry_id)}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert device.identifiers == {(DOMAIN, subentry.subentry_id)}
|
||||||
|
assert device.id == device_1.id
|
||||||
|
assert device.config_entries == {mock_config_entry.entry_id}
|
||||||
|
assert device.config_entries_subentries == {
|
||||||
|
mock_config_entry.entry_id: {subentry.subentry_id}
|
||||||
|
}
|
||||||
|
|
||||||
|
subentry = conversation_subentries[1]
|
||||||
|
|
||||||
|
entity = entity_registry.async_get(
|
||||||
|
"conversation.google_generative_ai_conversation_2"
|
||||||
|
)
|
||||||
|
assert entity.unique_id == subentry.subentry_id
|
||||||
|
assert entity.config_subentry_id == subentry.subentry_id
|
||||||
|
assert entity.config_entry_id == entry.entry_id
|
||||||
|
assert not device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, mock_config_entry.entry_id)}
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
device := device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, subentry.subentry_id)}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
assert device.identifiers == {(DOMAIN, subentry.subentry_id)}
|
||||||
|
assert device.id == device_2.id
|
||||||
|
assert device.config_entries == {mock_config_entry.entry_id}
|
||||||
|
assert device.config_entries_subentries == {
|
||||||
|
mock_config_entry.entry_id: {subentry.subentry_id}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_devices(
|
async def test_devices(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_config_entry: MockConfigEntry,
|
mock_config_entry: MockConfigEntry,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user