Skip to content

Commit

Permalink
Deprecate unit_system name
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet committed Oct 13, 2022
1 parent e852c9b commit b960975
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2015,7 +2015,7 @@ async def async_store(self) -> None:
"latitude": self.latitude,
"longitude": self.longitude,
"elevation": self.elevation,
"unit_system": self.units.name,
"unit_system": self.units._name, # pylint: disable=protected-access
"location_name": self.location_name,
"time_zone": self.time_zone,
"external_url": self.external_url,
Expand Down
16 changes: 14 additions & 2 deletions homeassistant/util/unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
VOLUME_LITERS,
WIND_SPEED,
)
from homeassistant.helpers.frame import report

from .unit_conversion import (
DistanceConverter,
Expand Down Expand Up @@ -107,7 +108,7 @@ def __init__(
if errors:
raise ValueError(errors)

self.name = name
self._name = name
self.accumulated_precipitation_unit = accumulated_precipitation
self.temperature_unit = temperature
self.length_unit = length
Expand All @@ -116,10 +117,21 @@ def __init__(
self.volume_unit = volume
self.wind_speed_unit = wind_speed

@property
def name(self) -> str:
"""Return the name of the unit system."""
report(
"accesses the name of the unit system. "
"This is deprecated and will stop working in Home Assistant 2023.1. "
"Please adjust to use instance check instead.",
error_if_core=False,
)
return self._name

@property
def is_metric(self) -> bool:
"""Determine if this is the metric unit system."""
return self.name == CONF_UNIT_SYSTEM_METRIC
return self._name == CONF_UNIT_SYSTEM_METRIC

def temperature(self, temperature: float, from_unit: str) -> float:
"""Convert the given temperature to this unit system."""
Expand Down
7 changes: 7 additions & 0 deletions tests/util/test_unit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from homeassistant.const import (
ACCUMULATED_PRECIPITATION,
CONF_UNIT_SYSTEM_METRIC,
LENGTH,
LENGTH_KILOMETERS,
LENGTH_METERS,
Expand Down Expand Up @@ -298,3 +299,9 @@ def test_is_metric():
"""Test the is metric flag."""
assert METRIC_SYSTEM.is_metric
assert not IMPERIAL_SYSTEM.is_metric


def test_deprecated_name(caplog: pytest.LogCaptureFixture) -> None:
"""Test the name is deprecated."""
assert METRIC_SYSTEM.name == CONF_UNIT_SYSTEM_METRIC
assert "Detected code that accesses the name of the unit system." in caplog.text

0 comments on commit b960975

Please sign in to comment.