From a2da1c7db56747049d513e599bbf3be20bfa64c5 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 10 Nov 2022 17:28:19 +0100 Subject: [PATCH] Create repairs issue if an outdated currency code is configured in core store (#81772) * Create repairs issue if an outdated currency code is configured in core store * Update homeassistant/config.py Co-authored-by: Aarni Koskela Co-authored-by: Aarni Koskela --- homeassistant/config.py | 34 +++++++++++++++++----------------- tests/test_config.py | 27 +++++++++++++++++++++------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/homeassistant/config.py b/homeassistant/config.py index 8e06c2c47a2..c58f94ca197 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -48,14 +48,9 @@ from .const import ( LEGACY_CONF_WHITELIST_EXTERNAL_DIRS, __version__, ) -from .core import ( - DOMAIN as CONF_CORE, - ConfigSource, - HomeAssistant, - async_get_hass, - callback, -) +from .core import DOMAIN as CONF_CORE, ConfigSource, HomeAssistant, callback from .exceptions import HomeAssistantError +from .generated.currencies import HISTORIC_CURRENCIES from .helpers import ( config_per_platform, config_validation as cv, @@ -208,22 +203,25 @@ CUSTOMIZE_CONFIG_SCHEMA = vol.Schema( ) +def _raise_issue_if_historic_currency(hass: HomeAssistant, currency: str) -> None: + if currency in HISTORIC_CURRENCIES: + ir.async_create_issue( + hass, + "homeassistant", + "historic_currency", + is_fixable=False, + severity=ir.IssueSeverity.WARNING, + translation_key="historic_currency", + translation_placeholders={"currency": currency}, + ) + + def _validate_currency(data: Any) -> Any: - hass = async_get_hass() try: return cv.currency(data) except vol.InInvalid: with suppress(vol.InInvalid): currency = cv.historic_currency(data) - ir.async_create_issue( - hass, - "homeassistant", - "historic_currency", - is_fixable=False, - severity=ir.IssueSeverity.WARNING, - translation_key="historic_currency", - translation_placeholders={"currency": currency}, - ) return currency raise @@ -580,6 +578,8 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non if key in config: setattr(hac, attr, config[key]) + _raise_issue_if_historic_currency(hass, hass.config.currency) + if CONF_TIME_ZONE in config: hac.set_time_zone(config[CONF_TIME_ZONE]) diff --git a/tests/test_config.py b/tests/test_config.py index 75ad227a641..ef364638725 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1207,13 +1207,28 @@ def test_identify_config_schema(domain, schema, expected): ) -def test_core_config_schema_historic_currency(hass): +async def test_core_config_schema_historic_currency(hass): """Test core config schema.""" - config_util.CORE_CONFIG_SCHEMA( - { - "currency": "LTT", - } - ) + await config_util.async_process_ha_core_config(hass, {"currency": "LTT"}) + + issue_registry = ir.async_get(hass) + issue = issue_registry.async_get_issue("homeassistant", "historic_currency") + assert issue + assert issue.translation_placeholders == {"currency": "LTT"} + + +async def test_core_store_historic_currency(hass, hass_storage): + """Test core config store.""" + core_data = { + "data": { + "currency": "LTT", + }, + "key": "core.config", + "version": 1, + "minor_version": 1, + } + hass_storage["core.config"] = dict(core_data) + await config_util.async_process_ha_core_config(hass, {}) issue_registry = ir.async_get(hass) issue = issue_registry.async_get_issue("homeassistant", "historic_currency")