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 <akx@iki.fi>

Co-authored-by: Aarni Koskela <akx@iki.fi>
This commit is contained in:
Erik Montnemery 2022-11-10 17:28:19 +01:00 committed by GitHub
parent 25d54f407e
commit a2da1c7db5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 23 deletions

View File

@ -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])

View File

@ -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")