From 6a757662e47061dcd99faf4f16881784ed2e2d89 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 14 Oct 2022 13:44:18 +0200 Subject: [PATCH] Deprecate is_metric property of unit system (#80313) --- homeassistant/util/unit_system.py | 8 +++++++- tests/util/test_unit_system.py | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/homeassistant/util/unit_system.py b/homeassistant/util/unit_system.py index 347c8ae859b..5c1f6d647a1 100644 --- a/homeassistant/util/unit_system.py +++ b/homeassistant/util/unit_system.py @@ -131,7 +131,13 @@ class UnitSystem: @property def is_metric(self) -> bool: """Determine if this is the metric unit system.""" - return self._name == CONF_UNIT_SYSTEM_METRIC + report( + "accesses the `is_metric` 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 is METRIC_SYSTEM 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 72a4456a219..8c7a9cf2fc5 100644 --- a/tests/util/test_unit_system.py +++ b/tests/util/test_unit_system.py @@ -296,10 +296,22 @@ def test_properties(): assert METRIC_SYSTEM.accumulated_precipitation_unit == LENGTH_MILLIMETERS -def test_is_metric(): +@pytest.mark.parametrize( + "unit_system, expected_flag", + [ + (METRIC_SYSTEM, True), + (IMPERIAL_SYSTEM, False), + ], +) +def test_is_metric( + caplog: pytest.LogCaptureFixture, unit_system: UnitSystem, expected_flag: bool +): """Test the is metric flag.""" - assert METRIC_SYSTEM.is_metric - assert not IMPERIAL_SYSTEM.is_metric + assert unit_system.is_metric == expected_flag + assert ( + "Detected code that accesses the `is_metric` property of the unit system." + in caplog.text + ) @pytest.mark.parametrize(