From 8b08b5e8d23056856bbb4e7d7b1fd0d62ec69f80 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Fri, 22 Dec 2023 00:42:16 +0100 Subject: [PATCH] Deprecate legacy_templates (#105556) * Deprecate legacy_templates * Improve wording * Implement suggestion * simplify * Add deleting of the repair issues back * Update homeassistant/components/homeassistant/strings.json Co-authored-by: Martin Hjelmare * Test issue removal too --------- Co-authored-by: Martin Hjelmare --- .../components/homeassistant/strings.json | 8 +++++ homeassistant/config.py | 36 +++++++++++++++++++ tests/test_config.py | 24 +++++++++++++ 3 files changed, 68 insertions(+) diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json index 6981bdfe685..862ac12cefb 100644 --- a/homeassistant/components/homeassistant/strings.json +++ b/homeassistant/components/homeassistant/strings.json @@ -12,6 +12,14 @@ "title": "The configured currency is no longer in use", "description": "The currency {currency} is no longer in use, please reconfigure the currency configuration." }, + "legacy_templates_false": { + "title": "`legacy_templates` config key is being removed", + "description": "Nothing will change with your templates.\n\nRemove the `legacy_templates` key from the `homeassistant` configuration in your configuration.yaml file and restart Home Assistant to fix this issue." + }, + "legacy_templates_true": { + "title": "The support for legacy templates is being removed", + "description": "Please do the following steps:\n- Adopt your configuration to support template rendering to native python types.\n- Remove the `legacy_templates` key from the `homeassistant` configuration in your configuration.yaml file.\n- Restart Home Assistant to fix this issue." + }, "python_version": { "title": "Support for Python {current_python_version} is being removed", "description": "Support for running Home Assistant in the current used Python version {current_python_version} is deprecated and will be removed in Home Assistant {breaks_in_ha_version}. Please upgrade Python to {required_python_version} to prevent your Home Assistant instance from breaking." diff --git a/homeassistant/config.py b/homeassistant/config.py index d5b6864c937..949774d3361 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -272,6 +272,41 @@ def _raise_issue_if_no_country(hass: HomeAssistant, country: str | None) -> None ) +def _raise_issue_if_legacy_templates( + hass: HomeAssistant, legacy_templates: bool | None +) -> None: + # legacy_templates can have the following values: + # - None: Using default value (False) -> Delete repair issues + # - True: Create repair to adopt templates to new syntax + # - False: Create repair to tell user to remove config key + if legacy_templates: + ir.async_create_issue( + hass, + "homeassistant", + "legacy_templates_true", + is_fixable=False, + breaks_in_ha_version="2024.7.0", + severity=ir.IssueSeverity.WARNING, + translation_key="legacy_templates_true", + ) + return + + ir.async_delete_issue(hass, "homeassistant", "legacy_templates_true") + + if legacy_templates is False: + ir.async_create_issue( + hass, + "homeassistant", + "legacy_templates_false", + is_fixable=False, + breaks_in_ha_version="2024.7.0", + severity=ir.IssueSeverity.WARNING, + translation_key="legacy_templates_false", + ) + else: + ir.async_delete_issue(hass, "homeassistant", "legacy_templates_false") + + def _validate_currency(data: Any) -> Any: try: return cv.currency(data) @@ -840,6 +875,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non if key in config: setattr(hac, attr, config[key]) + _raise_issue_if_legacy_templates(hass, config.get(CONF_LEGACY_TEMPLATES)) _raise_issue_if_historic_currency(hass, hass.config.currency) _raise_issue_if_no_country(hass, hass.config.country) diff --git a/tests/test_config.py b/tests/test_config.py index 8ec509cd895..0f6b36d90b5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1980,6 +1980,30 @@ async def test_core_config_schema_no_country(hass: HomeAssistant) -> None: assert issue +@pytest.mark.parametrize( + ("config", "expected_issue"), + [ + ({}, None), + ({"legacy_templates": True}, "legacy_templates_true"), + ({"legacy_templates": False}, "legacy_templates_false"), + ], +) +async def test_core_config_schema_legacy_template( + hass: HomeAssistant, config: dict[str, Any], expected_issue: str | None +) -> None: + """Test legacy_template core config schema.""" + await config_util.async_process_ha_core_config(hass, config) + + issue_registry = ir.async_get(hass) + for issue_id in {"legacy_templates_true", "legacy_templates_false"}: + issue = issue_registry.async_get_issue("homeassistant", issue_id) + assert issue if issue_id == expected_issue else not issue + + await config_util.async_process_ha_core_config(hass, {}) + for issue_id in {"legacy_templates_true", "legacy_templates_false"}: + assert not issue_registry.async_get_issue("homeassistant", issue_id) + + async def test_core_store_no_country( hass: HomeAssistant, hass_storage: dict[str, Any] ) -> None: