From 522a1eb3509cd1a50a8b0056535521b80595e02b Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Tue, 20 Oct 2026 16:54:17 +0300 Subject: [PATCH] Remove repair issue for Anthropic when the model is updated outside of repair flow (#187483) --- .../components/anthropic/__init__.py | 42 +++-- .../components/anthropic/config_flow.py | 5 +- .../components/anthropic/test_config_flow.py | 150 +++++++++++++-- tests/components/anthropic/test_init.py | 177 ++++++++++++++++++ 4 files changed, 336 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/anthropic/__init__.py b/homeassistant/components/anthropic/__init__.py index 701dc7b32cbc..ab9296dd7096 100644 --- a/homeassistant/components/anthropic/__init__.py +++ b/homeassistant/components/anthropic/__init__.py @@ -2,7 +2,7 @@ from functools import partial -from homeassistant.config_entries import ConfigSubentry +from homeassistant.config_entries import ConfigEntryState, ConfigSubentry from homeassistant.const import CONF_API_KEY, Platform from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import ( @@ -48,23 +48,31 @@ async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> def _async_check_model_lifecycle( hass: HomeAssistant, entry: AnthropicConfigEntry ) -> None: - """Create a repair for models deprecated or retired by the API.""" - for subentry in entry.subentries.values(): - if not (model := subentry.data.get(CONF_CHAT_MODEL)): + """Update the shared repair issue for deprecated or retired models.""" + for config_entry in hass.config_entries.async_entries(DOMAIN): + if ( + config_entry is not entry + and config_entry.state is not ConfigEntryState.LOADED + ): continue - model_info, found = entry.runtime_data.get_model_info(model) - if found and model_info.lifecycle in ("deprecated", "retired"): - ir.async_create_issue( - hass, - DOMAIN, - "model_deprecated", - is_fixable=True, - is_persistent=False, - learn_more_url="https://platform.claude.com/docs/en/about-claude/model-deprecations", - severity=ir.IssueSeverity.WARNING, - translation_key="model_deprecated", - ) - break + for subentry in config_entry.subentries.values(): + if not (model := subentry.data.get(CONF_CHAT_MODEL)): + continue + model_info, found = config_entry.runtime_data.get_model_info(model) + if found and model_info.lifecycle in ("deprecated", "retired"): + ir.async_create_issue( + hass, + DOMAIN, + "model_deprecated", + is_fixable=True, + is_persistent=False, + learn_more_url="https://platform.claude.com/docs/en/about-claude/model-deprecations", + severity=ir.IssueSeverity.WARNING, + translation_key="model_deprecated", + ) + return + + ir.async_delete_issue(hass, DOMAIN, "model_deprecated") async def async_unload_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) -> None: diff --git a/homeassistant/components/anthropic/config_flow.py b/homeassistant/components/anthropic/config_flow.py index 182f7f8f264f..7f6948576b02 100644 --- a/homeassistant/components/anthropic/config_flow.py +++ b/homeassistant/components/anthropic/config_flow.py @@ -400,6 +400,10 @@ class ConversationSubentryFlowHandler(ConfigSubentryFlow): self, user_input: dict[str, Any] | None = None ) -> SubentryFlowResult: """Manage additional options.""" + coordinator = self._get_entry().runtime_data + if user_input is None: + await coordinator.async_refresh() + errors: dict[str, str] = {} description_placeholders: dict[str, str] = {} @@ -425,7 +429,6 @@ class ConversationSubentryFlowHandler(ConfigSubentryFlow): if user_input is not None: self.options.update(user_input) - coordinator = self._get_entry().runtime_data self.model_info, status = coordinator.get_model_info( self.options[CONF_CHAT_MODEL] ) diff --git a/tests/components/anthropic/test_config_flow.py b/tests/components/anthropic/test_config_flow.py index e63e43f45952..1ff1abd6f6ae 100644 --- a/tests/components/anthropic/test_config_flow.py +++ b/tests/components/anthropic/test_config_flow.py @@ -1,6 +1,7 @@ """Test the Anthropic config flow.""" import datetime as dt +from typing import Literal from unittest.mock import AsyncMock, patch from anthropic import ( @@ -56,6 +57,27 @@ from homeassistant.data_entry_flow import FlowResultType, InvalidData from . import model_list +@pytest.fixture +def mock_model_catalogue(mock_model_list: AsyncMock) -> list[ModelInfo]: + """Allow tests to update the catalogue returned by the API.""" + models = list(model_list) + + def models_list( + *, + lifecycle: list[Literal["active", "deprecated", "retired"]] | None = None, + timeout: float = 10.0, + ) -> AsyncPage[ModelInfo]: + """Return the catalogue matching the requested lifecycle stages.""" + lifecycles = lifecycle if lifecycle is not None else ["active", "deprecated"] + return AsyncPage( + data=[model for model in models if model.lifecycle in lifecycles], + has_more=False, + ) + + mock_model_list.side_effect = models_list + return models + + async def test_form( hass: HomeAssistant, mock_setup_entry: AsyncMock, @@ -677,6 +699,7 @@ async def test_reconfigure_subentry_thinking_options( async def test_reconfigure_subentry_server_tools( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + mock_model_catalogue: list[ModelInfo], subentry_type: str, code_execution_supported: bool, web_search_supported: bool, @@ -717,7 +740,7 @@ async def test_reconfigure_subentry_server_tools( }, ) await hass.async_block_till_done() - mock_config_entry.runtime_data.async_set_updated_data([model]) + mock_model_catalogue[:] = [model] result = await mock_config_entry.start_subentry_reconfigure_flow( hass, subentry.subentry_id @@ -857,21 +880,111 @@ async def test_subentry_web_search_user_location( } +@pytest.mark.parametrize( + "subentry_type", + [ + pytest.param("conversation", id="conversation"), + pytest.param("ai_task_data", id="ai_task"), + ], +) +@pytest.mark.usefixtures("mock_init_component") +async def test_model_list_refresh( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_model_list: AsyncMock, + mock_model_catalogue: list[ModelInfo], + subentry_type: str, +) -> None: + """Refresh the catalogue before showing it and reuse it on submission.""" + mock_model_catalogue[:] = [ + model_list[0].model_copy( + update={"id": "new-model", "display_name": "New model"} + ) + ] + subentry = next( + subentry + for subentry in mock_config_entry.subentries.values() + if subentry.subentry_type == subentry_type + ) + result = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + mock_model_list.reset_mock() + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_RECOMMENDED: False} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "additional" + assert result["data_schema"].schema[CONF_CHAT_MODEL].config["options"] == [ + {"label": "New model", "value": "new-model"} + ] + assert mock_model_list.await_count == 2 + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_CHAT_MODEL: "new-model"} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "model" + assert mock_model_list.await_count == 2 + + +@pytest.mark.parametrize( + "subentry_type", + [ + pytest.param("conversation", id="conversation"), + pytest.param("ai_task_data", id="ai_task"), + ], +) +@pytest.mark.usefixtures("mock_init_component") +async def test_model_list_refresh_failure( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_model_list: AsyncMock, + subentry_type: str, +) -> None: + """Keep the cached catalogue available when its refresh fails.""" + subentry = next( + subentry + for subentry in mock_config_entry.subentries.values() + if subentry.subentry_type == subentry_type + ) + result = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + mock_model_list.reset_mock() + mock_model_list.side_effect = APIConnectionError(request=None) + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_RECOMMENDED: False} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "additional" + assert not result["errors"] + models = result["data_schema"].schema[CONF_CHAT_MODEL].config["options"] + assert len(models) == len(model_list) + assert models[0] == { + "label": model_list[0].display_name, + "value": model_list[0].id, + } + mock_model_list.assert_awaited_once_with(lifecycle=["active"], timeout=10.0) + + @pytest.mark.usefixtures("mock_init_component") async def test_model_list( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + mock_model_catalogue: list[ModelInfo], snapshot: SnapshotAssertion, ) -> None: """Test the model selector excludes retired models from the catalogue.""" - mock_config_entry.runtime_data.async_set_updated_data( - [ - *model_list, - model_list[0].model_copy( - update={"id": "retired-model", "lifecycle": "retired"} - ), - ] + mock_model_catalogue.append( + model_list[0].model_copy(update={"id": "retired-model", "lifecycle": "retired"}) ) + mock_config_entry.runtime_data.async_set_updated_data(mock_model_catalogue) subentry = next(iter(mock_config_entry.subentries.values())) options_flow = await mock_config_entry.start_subentry_reconfigure_flow( hass, subentry.subentry_id @@ -907,6 +1020,7 @@ async def test_model_list( async def test_model_list_structured_outputs( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + mock_model_catalogue: list[ModelInfo], subentry_type: str, expected_values: list[str], ) -> None: @@ -920,9 +1034,7 @@ async def test_model_list_structured_outputs( unknown_model = model_list[0].model_copy( update={"id": "unknown-model", "capabilities": None} ) - mock_config_entry.runtime_data.async_set_updated_data( - [supported_model, unsupported_model, unknown_model] - ) + mock_model_catalogue[:] = [supported_model, unsupported_model, unknown_model] subentry = next( subentry for subentry in mock_config_entry.subentries.values() @@ -979,16 +1091,15 @@ async def test_model_list_structured_outputs( async def test_model_list_sdk_lifecycle( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + mock_model_catalogue: list[ModelInfo], sdk_dates: dict[str, str], expected_values: list[str], ) -> None: """Filter models using the lifecycle of the alias submitted by the selector.""" - mock_config_entry.runtime_data.async_set_updated_data( - [ - model_list[0].model_copy(update={"id": "active-model"}), - model_list[0].model_copy(update={"id": "sdk-model-20260101"}), - ] - ) + mock_model_catalogue[:] = [ + model_list[0].model_copy(update={"id": "active-model"}), + model_list[0].model_copy(update={"id": "sdk-model-20260101"}), + ] subentry = next(iter(mock_config_entry.subentries.values())) options_flow = await mock_config_entry.start_subentry_reconfigure_flow( hass, subentry.subentry_id @@ -1017,15 +1128,14 @@ async def test_model_list_sdk_lifecycle( async def test_retired_model( hass: HomeAssistant, mock_config_entry: MockConfigEntry, + mock_model_catalogue: list[ModelInfo], cached_model_count: int, ) -> None: """Reject retired models entered as a custom model ID.""" retired_model = model_list[0].model_copy( update={"id": "retired-model", "lifecycle": "retired"} ) - mock_config_entry.runtime_data.async_set_updated_data( - [retired_model][:cached_model_count] - ) + mock_model_catalogue[:] = [retired_model][:cached_model_count] result = await hass.config_entries.subentries.async_init( (mock_config_entry.entry_id, "conversation"), context={"source": config_entries.SOURCE_USER}, diff --git a/tests/components/anthropic/test_init.py b/tests/components/anthropic/test_init.py index a440b534faab..91f97faed6f4 100644 --- a/tests/components/anthropic/test_init.py +++ b/tests/components/anthropic/test_init.py @@ -250,6 +250,183 @@ async def test_reconfigure_deprecated_model_repair( assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None +@pytest.mark.parametrize( + "subentry_type", + [ + pytest.param("conversation", id="conversation"), + pytest.param("ai_task_data", id="ai_task"), + ], +) +@pytest.mark.parametrize( + "lifecycle", + [ + pytest.param("deprecated", id="deprecated"), + pytest.param("retired", id="retired"), + ], +) +async def test_reconfigure_clears_model_repair( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_model_list: AsyncMock, + issue_registry: ir.IssueRegistry, + subentry_type: str, + lifecycle: Literal["deprecated", "retired"], +) -> None: + """Clear a repair discovered by the model picker after replacing that model.""" + configured_model = model_list[0].model_copy(update={"id": "claude-configured"}) + replacement_model = model_list[0].model_copy(update={"id": "claude-replacement"}) + subentry = next( + subentry + for subentry in mock_config_entry.subentries.values() + if subentry.subentry_type == subentry_type + ) + hass.config_entries.async_update_subentry( + mock_config_entry, subentry, data={CONF_CHAT_MODEL: configured_model.id} + ) + mock_model_list.side_effect = partial( + _model_page, [configured_model, replacement_model] + ) + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is None + + mock_model_list.side_effect = partial( + _model_page, + [ + configured_model.model_copy(update={"lifecycle": lifecycle}), + replacement_model, + ], + ) + result = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_RECOMMENDED: False} + ) + assert result["step_id"] == "additional" + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_CHAT_MODEL: replacement_model.id} + ) + assert result["step_id"] == "model" + result = await hass.config_entries.subentries.async_configure(result["flow_id"], {}) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.LOADED + assert subentry.data[CONF_CHAT_MODEL] == replacement_model.id + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is None + + +async def test_model_repair_retained_for_other_subentries( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_model_list: AsyncMock, + issue_registry: ir.IssueRegistry, +) -> None: + """Retain the shared repair until all affected accounts and subentries are fixed.""" + deprecated_model = model_list[0].model_copy( + update={"id": "claude-deprecated", "lifecycle": "deprecated"} + ) + active_model = model_list[0].model_copy(update={"id": "claude-active"}) + for subentry in mock_config_entry.subentries.values(): + hass.config_entries.async_update_subentry( + mock_config_entry, subentry, data={CONF_CHAT_MODEL: deprecated_model.id} + ) + mock_model_list.side_effect = partial(_model_page, [deprecated_model, active_model]) + assert await async_setup_component(hass, DOMAIN, {}) + await hass.async_block_till_done() + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + + disabled_entry = MockConfigEntry( + domain=DOMAIN, + title="Disabled account", + data={CONF_API_KEY: "disabled-api-key"}, + version=2, + disabled_by=ConfigEntryDisabler.USER, + subentries_data=[ + { + "data": {CONF_CHAT_MODEL: deprecated_model.id}, + "subentry_type": "conversation", + "title": "Disabled conversation", + "unique_id": None, + } + ], + ) + disabled_entry.add_to_hass(hass) + assert disabled_entry.state is ConfigEntryState.NOT_LOADED + + healthy_entry = MockConfigEntry( + domain=DOMAIN, + title="Other account", + data={CONF_API_KEY: "other-api-key"}, + version=2, + subentries_data=[ + { + "data": {CONF_CHAT_MODEL: active_model.id}, + "subentry_type": "conversation", + "title": "Other conversation", + "unique_id": None, + } + ], + ) + healthy_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(healthy_entry.entry_id) + await hass.async_block_till_done() + assert healthy_entry.state is ConfigEntryState.LOADED + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + + mock_model_list.side_effect = partial( + _model_page, + [ + deprecated_model, + active_model.model_copy(update={"display_name": "Updated active model"}), + ], + ) + healthy_subentry = next(iter(healthy_entry.subentries.values())) + result = await healthy_entry.start_subentry_reconfigure_flow( + hass, healthy_subentry.subentry_id + ) + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_RECOMMENDED: False} + ) + assert result["step_id"] == "additional" + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_CHAT_MODEL: active_model.id} + ) + result = await hass.config_entries.subentries.async_configure(result["flow_id"], {}) + assert result["reason"] == "reconfigure_successful" + await hass.async_block_till_done() + assert healthy_entry.state is ConfigEntryState.LOADED + assert issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + + for subentry, expected_issue in zip( + mock_config_entry.subentries.values(), (True, False), strict=True + ): + result = await mock_config_entry.start_subentry_reconfigure_flow( + hass, subentry.subentry_id + ) + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_RECOMMENDED: False} + ) + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {CONF_CHAT_MODEL: active_model.id} + ) + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], {} + ) + assert result["reason"] == "reconfigure_successful" + await hass.async_block_till_done() + assert mock_config_entry.state is ConfigEntryState.LOADED + assert subentry.data[CONF_CHAT_MODEL] == active_model.id + assert ( + issue_registry.async_get_issue(DOMAIN, "model_deprecated") is not None + ) is expected_issue + + @pytest.mark.usefixtures("mock_setup_entry") async def test_downgrade_from_v3_to_v2( hass: HomeAssistant,