From 67e4f2c2027d472a4fbb07769c74384cabd44db8 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 28 Nov 2022 09:54:13 +0100 Subject: [PATCH] Raise repairs issue if country is not configured (#82685) --- .../components/homeassistant/strings.json | 4 ++ .../homeassistant/translations/en.json | 4 ++ homeassistant/config.py | 41 ++++++++++++++----- homeassistant/core.py | 9 ++++ tests/components/homeassistant/test_init.py | 1 + tests/test_config.py | 37 ++++++++++++++++- 6 files changed, 85 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/homeassistant/strings.json b/homeassistant/components/homeassistant/strings.json index d7c5216111c..06ecd4fe3ef 100644 --- a/homeassistant/components/homeassistant/strings.json +++ b/homeassistant/components/homeassistant/strings.json @@ -1,5 +1,9 @@ { "issues": { + "country_not_configured": { + "title": "The country has not been configured", + "description": "No country has been configured, please update the configuration by clicking on the \"learn more\" button below." + }, "historic_currency": { "title": "The configured currency is no longer in use", "description": "The currency {currency} is no longer in use, please reconfigure the currency configuration." diff --git a/homeassistant/components/homeassistant/translations/en.json b/homeassistant/components/homeassistant/translations/en.json index b41e5753995..4b94cc37d2e 100644 --- a/homeassistant/components/homeassistant/translations/en.json +++ b/homeassistant/components/homeassistant/translations/en.json @@ -1,5 +1,9 @@ { "issues": { + "country_not_configured": { + "description": "No country has been configured, please update the configuration by clicking on the \"learn more\" button below.", + "title": "The country has not been configured" + }, "historic_currency": { "description": "The currency {currency} is no longer in use, please reconfigure the currency configuration.", "title": "The configured currency is no longer in use" diff --git a/homeassistant/config.py b/homeassistant/config.py index 963f3ee9876..e203c45f795 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -206,16 +206,36 @@ 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}, - ) + if currency not in HISTORIC_CURRENCIES: + ir.async_delete_issue(hass, "homeassistant", "historic_currency") + return + + ir.async_create_issue( + hass, + "homeassistant", + "historic_currency", + is_fixable=False, + learn_more_url="homeassistant://config/general", + severity=ir.IssueSeverity.WARNING, + translation_key="historic_currency", + translation_placeholders={"currency": currency}, + ) + + +def _raise_issue_if_no_country(hass: HomeAssistant, country: str | None) -> None: + if country is not None: + ir.async_delete_issue(hass, "homeassistant", "country_not_configured") + return + + ir.async_create_issue( + hass, + "homeassistant", + "country_not_configured", + is_fixable=False, + learn_more_url="homeassistant://config/general", + severity=ir.IssueSeverity.WARNING, + translation_key="country_not_configured", + ) def _validate_currency(data: Any) -> Any: @@ -587,6 +607,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non setattr(hac, attr, config[key]) _raise_issue_if_historic_currency(hass, hass.config.currency) + _raise_issue_if_no_country(hass, hass.config.country) if CONF_TIME_ZONE in config: hac.set_time_zone(config[CONF_TIME_ZONE]) diff --git a/homeassistant/core.py b/homeassistant/core.py index 9172c1d60b4..c5a7599c369 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1976,10 +1976,19 @@ class Config: async def async_update(self, **kwargs: Any) -> None: """Update the configuration from a dictionary.""" + # pylint: disable-next=import-outside-toplevel + from .config import ( + _raise_issue_if_historic_currency, + _raise_issue_if_no_country, + ) + self._update(source=ConfigSource.STORAGE, **kwargs) await self._async_store() self.hass.bus.async_fire(EVENT_CORE_CONFIG_UPDATE, kwargs) + _raise_issue_if_historic_currency(self.hass, self.currency) + _raise_issue_if_no_country(self.hass, self.country) + async def async_load(self) -> None: """Load [homeassistant] core config.""" if not (data := await self._store.async_load()): diff --git a/tests/components/homeassistant/test_init.py b/tests/components/homeassistant/test_init.py index 8e89be00433..0c980bcf07e 100644 --- a/tests/components/homeassistant/test_init.py +++ b/tests/components/homeassistant/test_init.py @@ -179,6 +179,7 @@ class TestComponentsCore(unittest.TestCase): config.YAML_CONFIG_FILE: yaml.dump( { ha.DOMAIN: { + "country": "SE", # To avoid creating issue country_not_configured "latitude": 10, "longitude": 20, "customize": {"test.Entity": {"hello": "world"}}, diff --git a/tests/test_config.py b/tests/test_config.py index 17b3898eb2a..ea9c81eae1a 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1315,6 +1315,41 @@ async def test_core_store_historic_currency(hass, hass_storage): await config_util.async_process_ha_core_config(hass, {}) issue_registry = ir.async_get(hass) - issue = issue_registry.async_get_issue("homeassistant", "historic_currency") + issue_id = "historic_currency" + issue = issue_registry.async_get_issue("homeassistant", issue_id) assert issue assert issue.translation_placeholders == {"currency": "LTT"} + + await hass.config.async_update(**{"currency": "EUR"}) + issue = issue_registry.async_get_issue("homeassistant", issue_id) + assert not issue + + +async def test_core_config_schema_no_country(hass): + """Test core config schema.""" + await config_util.async_process_ha_core_config(hass, {}) + + issue_registry = ir.async_get(hass) + issue = issue_registry.async_get_issue("homeassistant", "country_not_configured") + assert issue + + +async def test_core_store_no_country(hass, hass_storage): + """Test core config store.""" + core_data = { + "data": {}, + "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_id = "country_not_configured" + issue = issue_registry.async_get_issue("homeassistant", issue_id) + assert issue + + await hass.config.async_update(**{"country": "SE"}) + issue = issue_registry.async_get_issue("homeassistant", issue_id) + assert not issue