Deprecate CONF_UNIT_SYSTEM_*** constants

This commit is contained in:
epenet
2022-10-14 12:01:33 +00:00
parent d75e449c52
commit f79f44c868
6 changed files with 38 additions and 31 deletions

View File

@@ -5,10 +5,9 @@ import voluptuous as vol
from homeassistant.components import websocket_api
from homeassistant.components.http import HomeAssistantView
from homeassistant.config import async_check_ha_config_file
from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METRIC
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import location
from homeassistant.util import location, unit_system
async def async_setup(hass):
@@ -41,7 +40,7 @@ class CheckConfigView(HomeAssistantView):
vol.Optional("latitude"): cv.latitude,
vol.Optional("longitude"): cv.longitude,
vol.Optional("elevation"): int,
vol.Optional("unit_system"): cv.unit_system,
vol.Optional("unit_system"): unit_system.validate_unit_system,
vol.Optional("location_name"): str,
vol.Optional("time_zone"): cv.time_zone,
vol.Optional("external_url"): vol.Any(cv.url_no_path, None),
@@ -77,10 +76,7 @@ async def websocket_detect_config(hass, connection, msg):
connection.send_result(msg["id"], info)
return
if location_info.use_metric:
info["unit_system"] = CONF_UNIT_SYSTEM_METRIC
else:
info["unit_system"] = CONF_UNIT_SYSTEM_IMPERIAL
info["unit_system"] = unit_system.get_default_key(location_info.use_metric)
if location_info.latitude:
info["latitude"] = location_info.latitude

View File

@@ -44,7 +44,6 @@ from .const import (
CONF_TIME_ZONE,
CONF_TYPE,
CONF_UNIT_SYSTEM,
CONF_UNIT_SYSTEM_IMPERIAL,
LEGACY_CONF_WHITELIST_EXTERNAL_DIRS,
__version__,
)
@@ -60,7 +59,7 @@ from .helpers.typing import ConfigType
from .loader import Integration, IntegrationNotFound
from .requirements import RequirementsNotFound, async_get_integration_with_requirements
from .util.package import is_docker_env
from .util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
from .util.unit_system import get_unit_system, validate_unit_system
from .util.yaml import SECRET_YAML, Secrets, load_yaml
_LOGGER = logging.getLogger(__name__)
@@ -204,7 +203,7 @@ CORE_CONFIG_SCHEMA = vol.All(
CONF_LONGITUDE: cv.longitude,
CONF_ELEVATION: vol.Coerce(int),
vol.Remove(CONF_TEMPERATURE_UNIT): cv.temperature_unit,
CONF_UNIT_SYSTEM: cv.unit_system,
CONF_UNIT_SYSTEM: validate_unit_system,
CONF_TIME_ZONE: cv.time_zone,
vol.Optional(CONF_INTERNAL_URL): cv.url,
vol.Optional(CONF_EXTERNAL_URL): cv.url,
@@ -602,10 +601,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> Non
hass.data[DATA_CUSTOMIZE] = EntityValues(cust_exact, cust_domain, cust_glob)
if CONF_UNIT_SYSTEM in config:
if config[CONF_UNIT_SYSTEM] == CONF_UNIT_SYSTEM_IMPERIAL:
hac.units = IMPERIAL_SYSTEM
else:
hac.units = METRIC_SYSTEM
hac.units = get_unit_system(config[CONF_UNIT_SYSTEM])
def _log_pkg_error(package: str, component: str, config: dict, message: str) -> None:

View File

@@ -396,7 +396,9 @@ ATTR_ICON: Final = "icon"
ATTR_UNIT_OF_MEASUREMENT: Final = "unit_of_measurement"
CONF_UNIT_SYSTEM_METRIC: Final = "metric"
"""Deprecated: please use a local constant."""
CONF_UNIT_SYSTEM_IMPERIAL: Final = "imperial"
"""Deprecated: please use a local constant."""
# Electrical attributes
ATTR_VOLTAGE: Final = "voltage"

View File

@@ -49,7 +49,6 @@ from .const import (
ATTR_FRIENDLY_NAME,
ATTR_SERVICE,
ATTR_SERVICE_DATA,
CONF_UNIT_SYSTEM_IMPERIAL,
EVENT_CALL_SERVICE,
EVENT_CORE_CONFIG_UPDATE,
EVENT_HOMEASSISTANT_CLOSE,
@@ -82,7 +81,7 @@ from .util.async_ import (
)
from .util.read_only_dict import ReadOnlyDict
from .util.timeout import TimeoutManager
from .util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM, UnitSystem
from .util.unit_system import METRIC_SYSTEM, UnitSystem, get_unit_system
# Typing imports that create a circular dependency
if TYPE_CHECKING:
@@ -1940,10 +1939,7 @@ class Config:
if elevation is not None:
self.elevation = elevation
if unit_system is not None:
if unit_system == CONF_UNIT_SYSTEM_IMPERIAL:
self.units = IMPERIAL_SYSTEM
else:
self.units = METRIC_SYSTEM
self.units = get_unit_system(unit_system)
if location_name is not None:
self.location_name = location_name
if time_zone is not None:

View File

@@ -71,8 +71,6 @@ from homeassistant.const import (
CONF_TARGET,
CONF_THEN,
CONF_TIMEOUT,
CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNIT_SYSTEM_METRIC,
CONF_UNTIL,
CONF_VALUE_TEMPLATE,
CONF_VARIABLES,
@@ -588,11 +586,6 @@ def temperature_unit(value: Any) -> str:
raise vol.Invalid("invalid temperature unit (expected C or F)")
unit_system = vol.All(
vol.Lower, vol.Any(CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL)
)
def template(value: Any | None) -> template_helper.Template:
"""Validate a jinja2 template."""
if value is None:

View File

@@ -2,11 +2,12 @@
from __future__ import annotations
from numbers import Number
from typing import Final
import voluptuous as vol
from homeassistant.const import (
ACCUMULATED_PRECIPITATION,
CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNIT_SYSTEM_METRIC,
LENGTH,
LENGTH_INCHES,
LENGTH_KILOMETERS,
@@ -41,6 +42,9 @@ from .unit_conversion import (
VolumeConverter,
)
_CONF_UNIT_SYSTEM_IMPERIAL: Final = "imperial"
_CONF_UNIT_SYSTEM_METRIC: Final = "metric"
LENGTH_UNITS = DistanceConverter.VALID_UNITS
MASS_UNITS: set[str] = {MASS_POUNDS, MASS_OUNCES, MASS_KILOGRAMS, MASS_GRAMS}
@@ -207,8 +211,28 @@ class UnitSystem:
}
def get_unit_system(key: str) -> UnitSystem:
"""Get unit system based on key."""
if key == _CONF_UNIT_SYSTEM_IMPERIAL:
return IMPERIAL_SYSTEM
if key == _CONF_UNIT_SYSTEM_METRIC:
return METRIC_SYSTEM
raise ValueError("Invalid unit system key")
def get_default_key(use_metric: bool) -> str:
"""Get default unit system based on location information."""
if use_metric:
return _CONF_UNIT_SYSTEM_METRIC
return _CONF_UNIT_SYSTEM_IMPERIAL
validate_unit_system = vol.All(
vol.Lower, vol.Any(_CONF_UNIT_SYSTEM_METRIC, _CONF_UNIT_SYSTEM_IMPERIAL)
)
METRIC_SYSTEM = UnitSystem(
CONF_UNIT_SYSTEM_METRIC,
_CONF_UNIT_SYSTEM_METRIC,
TEMP_CELSIUS,
LENGTH_KILOMETERS,
SPEED_METERS_PER_SECOND,
@@ -219,7 +243,7 @@ METRIC_SYSTEM = UnitSystem(
)
IMPERIAL_SYSTEM = UnitSystem(
CONF_UNIT_SYSTEM_IMPERIAL,
_CONF_UNIT_SYSTEM_IMPERIAL,
TEMP_FAHRENHEIT,
LENGTH_MILES,
SPEED_MILES_PER_HOUR,