From 4dd0c079d533b16796fd4a86d1a5ceef8eefc41d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 14 Oct 2022 12:06:14 +0200 Subject: [PATCH] Deprecate name property of unit system (#80257) --- homeassistant/core.py | 4 +++- homeassistant/util/unit_system.py | 16 ++++++++++++++-- tests/util/test_unit_system.py | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index f4cefcb7fff..5a8ec07c1b9 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -2015,7 +2015,9 @@ class Config: "latitude": self.latitude, "longitude": self.longitude, "elevation": self.elevation, - "unit_system": self.units.name, + # We don't want any components 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 "location_name": self.location_name, "time_zone": self.time_zone, "external_url": self.external_url, diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 56c588ed1c9..347c8ae859b 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -31,6 +31,7 @@ from homeassistant.const import ( VOLUME_LITERS, WIND_SPEED, ) +from homeassistant.helpers.frame import report from .unit_conversion import ( DistanceConverter, @@ -107,7 +108,7 @@ class UnitSystem: 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 @@ -116,10 +117,21 @@ class UnitSystem: 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` property 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.""" diff --git a/tests/util/test_unit_system.py b/tests/util/test_unit_system.py index a284fd10017..72a4456a219 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -3,6 +3,8 @@ import pytest from homeassistant.const import ( ACCUMULATED_PRECIPITATION, + CONF_UNIT_SYSTEM_IMPERIAL, + CONF_UNIT_SYSTEM_METRIC, LENGTH, LENGTH_KILOMETERS, LENGTH_METERS, @@ -298,3 +300,21 @@ def test_is_metric(): """Test the is metric flag.""" assert METRIC_SYSTEM.is_metric assert not IMPERIAL_SYSTEM.is_metric + + +@pytest.mark.parametrize( + "unit_system, expected_name", + [ + (METRIC_SYSTEM, CONF_UNIT_SYSTEM_METRIC), + (IMPERIAL_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL), + ], +) +def test_deprecated_name( + caplog: pytest.LogCaptureFixture, unit_system: UnitSystem, expected_name: str +) -> None: + """Test the name is deprecated.""" + assert unit_system.name == expected_name + assert ( + "Detected code that accesses the `name` property of the unit system." + in caplog.text + )