Rename IMPERIAL_SYSTEM to US_CUSTOMARY_SYSTEM (#80253)

* Rename IMPERIAL_SYSTEM

* Deprecate is_metric property and adjust tests

* Adjust unit_system config validation

* Add yaml tests

* Add tests for private name

* Fix incorrect rebase

* Adjust docstring

* Add store migration

* Update unit_system.py

* Minimise test tweaks

* Fix tests

* Add conversion to migration

* Rename new key and adjust tests

* Adjust websocket_detect_config

* Move original_unit_system tracking to subclass
This commit is contained in:
epenet
2022-10-19 13:31:08 +02:00
committed by GitHub
parent f4951a4f31
commit 67d1dde69f
5 changed files with 125 additions and 22 deletions

View File

@@ -81,7 +81,13 @@ from .util.async_ import (
)
from .util.read_only_dict import ReadOnlyDict
from .util.timeout import TimeoutManager
from .util.unit_system import METRIC_SYSTEM, UnitSystem, get_unit_system
from .util.unit_system import (
_CONF_UNIT_SYSTEM_IMPERIAL,
_CONF_UNIT_SYSTEM_US_CUSTOMARY,
METRIC_SYSTEM,
UnitSystem,
get_unit_system,
)
# Typing imports that create a circular dependency
if TYPE_CHECKING:
@@ -107,6 +113,7 @@ CALLBACK_TYPE = Callable[[], None] # pylint: disable=invalid-name
CORE_STORAGE_KEY = "core.config"
CORE_STORAGE_VERSION = 1
CORE_STORAGE_MINOR_VERSION = 2
DOMAIN = "homeassistant"
@@ -1986,7 +1993,7 @@ class Config:
latitude=data.get("latitude"),
longitude=data.get("longitude"),
elevation=data.get("elevation"),
unit_system=data.get("unit_system"),
unit_system=data.get("unit_system_v2"),
location_name=data.get("location_name"),
time_zone=data.get("time_zone"),
external_url=data.get("external_url", _UNDEF),
@@ -2002,7 +2009,7 @@ class Config:
"elevation": self.elevation,
# We don't want any integrations to use the name of the unit system
# so we are using the private attribute here
"unit_system": self.units._name, # pylint: disable=protected-access
"unit_system_v2": self.units._name, # pylint: disable=protected-access
"location_name": self.location_name,
"time_zone": self.time_zone,
"external_url": self.external_url,
@@ -2027,4 +2034,30 @@ class Config:
CORE_STORAGE_KEY,
private=True,
atomic_writes=True,
minor_version=CORE_STORAGE_MINOR_VERSION,
)
self._original_unit_system: str | None = None # from old store 1.1
async def _async_migrate_func(
self,
old_major_version: int,
old_minor_version: int,
old_data: dict[str, Any],
) -> dict[str, Any]:
"""Migrate to the new version."""
data = old_data
if old_major_version == 1 and old_minor_version < 2:
# In 1.2, we remove support for "imperial", replaced by "us_customary"
# Using a new key to allow rollback
self._original_unit_system = data.get("unit_system")
data["unit_system_v2"] = self._original_unit_system
if data["unit_system_v2"] == _CONF_UNIT_SYSTEM_IMPERIAL:
data["unit_system_v2"] = _CONF_UNIT_SYSTEM_US_CUSTOMARY
if old_major_version > 1:
raise NotImplementedError
return data
async def async_save(self, data: dict[str, Any]) -> None:
if self._original_unit_system:
data["unit_system"] = self._original_unit_system
return await super().async_save(data)