mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Remove deprecated unit system properties (#86643)
* Remove deprecated unit system properties * Fix tests
This commit is contained in:
parent
3ff3834cae
commit
561fc2d771
@ -23,7 +23,6 @@ from homeassistant.const import (
|
|||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
UnitOfVolume,
|
UnitOfVolume,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.frame import report
|
|
||||||
|
|
||||||
from .unit_conversion import (
|
from .unit_conversion import (
|
||||||
DistanceConverter,
|
DistanceConverter,
|
||||||
@ -121,35 +120,6 @@ class UnitSystem:
|
|||||||
self.wind_speed_unit = wind_speed
|
self.wind_speed_unit = wind_speed
|
||||||
self._conversions = conversions
|
self._conversions = conversions
|
||||||
|
|
||||||
@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,
|
|
||||||
)
|
|
||||||
if self is IMPERIAL_SYSTEM:
|
|
||||||
# kept for compatibility reasons, with associated warning above
|
|
||||||
return _CONF_UNIT_SYSTEM_IMPERIAL
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_metric(self) -> bool:
|
|
||||||
"""Determine if this is the metric unit system."""
|
|
||||||
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:
|
def temperature(self, temperature: float, from_unit: str) -> float:
|
||||||
"""Convert the given temperature to this unit system."""
|
"""Convert the given temperature to this unit system."""
|
||||||
if not isinstance(temperature, Number):
|
if not isinstance(temperature, Number):
|
||||||
|
@ -9,6 +9,7 @@ from homeassistant.components import config
|
|||||||
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
from homeassistant.components.websocket_api.const import TYPE_RESULT
|
||||||
from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL
|
from homeassistant.const import CONF_UNIT_SYSTEM, CONF_UNIT_SYSTEM_IMPERIAL
|
||||||
from homeassistant.util import dt as dt_util, location
|
from homeassistant.util import dt as dt_util, location
|
||||||
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@ -55,7 +56,7 @@ async def test_websocket_core_update(hass, client):
|
|||||||
assert hass.config.longitude != 50
|
assert hass.config.longitude != 50
|
||||||
assert hass.config.elevation != 25
|
assert hass.config.elevation != 25
|
||||||
assert hass.config.location_name != "Huis"
|
assert hass.config.location_name != "Huis"
|
||||||
assert hass.config.units.name != CONF_UNIT_SYSTEM_IMPERIAL
|
assert hass.config.units is not US_CUSTOMARY_SYSTEM
|
||||||
assert hass.config.time_zone != "America/New_York"
|
assert hass.config.time_zone != "America/New_York"
|
||||||
assert hass.config.external_url != "https://www.example.com"
|
assert hass.config.external_url != "https://www.example.com"
|
||||||
assert hass.config.internal_url != "http://example.com"
|
assert hass.config.internal_url != "http://example.com"
|
||||||
@ -91,7 +92,7 @@ async def test_websocket_core_update(hass, client):
|
|||||||
assert hass.config.longitude == 50
|
assert hass.config.longitude == 50
|
||||||
assert hass.config.elevation == 25
|
assert hass.config.elevation == 25
|
||||||
assert hass.config.location_name == "Huis"
|
assert hass.config.location_name == "Huis"
|
||||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL
|
assert hass.config.units is US_CUSTOMARY_SYSTEM
|
||||||
assert hass.config.external_url == "https://www.example.com"
|
assert hass.config.external_url == "https://www.example.com"
|
||||||
assert hass.config.internal_url == "http://example.local"
|
assert hass.config.internal_url == "http://example.local"
|
||||||
assert hass.config.currency == "USD"
|
assert hass.config.currency == "USD"
|
||||||
|
@ -412,7 +412,7 @@ async def test_loading_configuration_from_storage(hass, hass_storage):
|
|||||||
assert hass.config.longitude == 13
|
assert hass.config.longitude == 13
|
||||||
assert hass.config.elevation == 10
|
assert hass.config.elevation == 10
|
||||||
assert hass.config.location_name == "Home"
|
assert hass.config.location_name == "Home"
|
||||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_METRIC
|
assert hass.config.units is METRIC_SYSTEM
|
||||||
assert hass.config.time_zone == "Europe/Copenhagen"
|
assert hass.config.time_zone == "Europe/Copenhagen"
|
||||||
assert hass.config.external_url == "https://www.example.com"
|
assert hass.config.external_url == "https://www.example.com"
|
||||||
assert hass.config.internal_url == "http://example.local"
|
assert hass.config.internal_url == "http://example.local"
|
||||||
@ -446,7 +446,7 @@ async def test_loading_configuration_from_storage_with_yaml_only(hass, hass_stor
|
|||||||
assert hass.config.longitude == 13
|
assert hass.config.longitude == 13
|
||||||
assert hass.config.elevation == 10
|
assert hass.config.elevation == 10
|
||||||
assert hass.config.location_name == "Home"
|
assert hass.config.location_name == "Home"
|
||||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_METRIC
|
assert hass.config.units is METRIC_SYSTEM
|
||||||
assert hass.config.time_zone == "Europe/Copenhagen"
|
assert hass.config.time_zone == "Europe/Copenhagen"
|
||||||
assert len(hass.config.allowlist_external_dirs) == 3
|
assert len(hass.config.allowlist_external_dirs) == 3
|
||||||
assert "/etc" in hass.config.allowlist_external_dirs
|
assert "/etc" in hass.config.allowlist_external_dirs
|
||||||
@ -517,7 +517,7 @@ async def test_override_stored_configuration(hass, hass_storage):
|
|||||||
assert hass.config.longitude == 13
|
assert hass.config.longitude == 13
|
||||||
assert hass.config.elevation == 10
|
assert hass.config.elevation == 10
|
||||||
assert hass.config.location_name == "Home"
|
assert hass.config.location_name == "Home"
|
||||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_METRIC
|
assert hass.config.units is METRIC_SYSTEM
|
||||||
assert hass.config.time_zone == "Europe/Copenhagen"
|
assert hass.config.time_zone == "Europe/Copenhagen"
|
||||||
assert len(hass.config.allowlist_external_dirs) == 3
|
assert len(hass.config.allowlist_external_dirs) == 3
|
||||||
assert "/etc" in hass.config.allowlist_external_dirs
|
assert "/etc" in hass.config.allowlist_external_dirs
|
||||||
@ -550,7 +550,7 @@ async def test_loading_configuration(hass):
|
|||||||
assert hass.config.longitude == 50
|
assert hass.config.longitude == 50
|
||||||
assert hass.config.elevation == 25
|
assert hass.config.elevation == 25
|
||||||
assert hass.config.location_name == "Huis"
|
assert hass.config.location_name == "Huis"
|
||||||
assert hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL
|
assert hass.config.units is US_CUSTOMARY_SYSTEM
|
||||||
assert hass.config.time_zone == "America/New_York"
|
assert hass.config.time_zone == "America/New_York"
|
||||||
assert hass.config.external_url == "https://www.example.com"
|
assert hass.config.external_url == "https://www.example.com"
|
||||||
assert hass.config.internal_url == "http://example.local"
|
assert hass.config.internal_url == "http://example.local"
|
||||||
|
@ -311,51 +311,6 @@ def test_properties():
|
|||||||
assert METRIC_SYSTEM.accumulated_precipitation_unit == UnitOfLength.MILLIMETERS
|
assert METRIC_SYSTEM.accumulated_precipitation_unit == UnitOfLength.MILLIMETERS
|
||||||
|
|
||||||
|
|
||||||
@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 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(
|
|
||||||
"unit_system, expected_name, expected_private_name",
|
|
||||||
[
|
|
||||||
(METRIC_SYSTEM, _CONF_UNIT_SYSTEM_METRIC, _CONF_UNIT_SYSTEM_METRIC),
|
|
||||||
(IMPERIAL_SYSTEM, _CONF_UNIT_SYSTEM_IMPERIAL, _CONF_UNIT_SYSTEM_US_CUSTOMARY),
|
|
||||||
(
|
|
||||||
US_CUSTOMARY_SYSTEM,
|
|
||||||
_CONF_UNIT_SYSTEM_IMPERIAL,
|
|
||||||
_CONF_UNIT_SYSTEM_US_CUSTOMARY,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
def test_deprecated_name(
|
|
||||||
caplog: pytest.LogCaptureFixture,
|
|
||||||
unit_system: UnitSystem,
|
|
||||||
expected_name: str,
|
|
||||||
expected_private_name: str,
|
|
||||||
) -> None:
|
|
||||||
"""Test the name is deprecated."""
|
|
||||||
assert unit_system.name == expected_name
|
|
||||||
assert unit_system._name == expected_private_name
|
|
||||||
assert (
|
|
||||||
"Detected code that accesses the `name` property of the unit system."
|
|
||||||
in caplog.text
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"key, expected_system",
|
"key, expected_system",
|
||||||
[
|
[
|
||||||
|
Loading…
x
Reference in New Issue
Block a user