Skip to content

Commit 174ac9e

Browse files
authored
Deprecate single-use CONCENTRATION_PARTS_PER_CUBIC_METER constant (home-assistant#172553)
1 parent 772c426 commit 174ac9e

3 files changed

Lines changed: 62 additions & 6 deletions

File tree

homeassistant/components/accuweather/sensor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
SensorStateClass,
1212
)
1313
from homeassistant.const import (
14-
CONCENTRATION_PARTS_PER_CUBIC_METER,
1514
PERCENTAGE,
1615
UV_INDEX,
1716
UnitOfIrradiance,
@@ -47,6 +46,8 @@
4746

4847
PARALLEL_UPDATES = 1
4948

49+
PARTS_PER_CUBIC_METER = "p/m³"
50+
5051

5152
@dataclass(frozen=True, kw_only=True)
5253
class AccuWeatherSensorDescription(SensorEntityDescription):
@@ -81,7 +82,7 @@ class AccuWeatherSensorDescription(SensorEntityDescription):
8182
AccuWeatherSensorDescription(
8283
key="Grass",
8384
entity_registry_enabled_default=False,
84-
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
85+
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
8586
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
8687
attr_fn=lambda data: {
8788
ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]]
@@ -107,7 +108,7 @@ class AccuWeatherSensorDescription(SensorEntityDescription):
107108
AccuWeatherSensorDescription(
108109
key="Mold",
109110
entity_registry_enabled_default=False,
110-
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
111+
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
111112
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
112113
attr_fn=lambda data: {
113114
ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]]
@@ -116,7 +117,7 @@ class AccuWeatherSensorDescription(SensorEntityDescription):
116117
),
117118
AccuWeatherSensorDescription(
118119
key="Ragweed",
119-
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
120+
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
120121
entity_registry_enabled_default=False,
121122
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
122123
attr_fn=lambda data: {
@@ -184,7 +185,7 @@ class AccuWeatherSensorDescription(SensorEntityDescription):
184185
),
185186
AccuWeatherSensorDescription(
186187
key="Tree",
187-
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
188+
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
188189
entity_registry_enabled_default=False,
189190
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
190191
attr_fn=lambda data: {

homeassistant/const.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
"""Constants used by Home Assistant components."""
22

33
from enum import StrEnum
4+
from functools import partial
45
from typing import TYPE_CHECKING, Final
56

67
from .generated.entity_platforms import EntityPlatforms
8+
from .helpers.deprecation import (
9+
DeprecatedConstant,
10+
all_with_deprecated_constants,
11+
check_if_deprecated_constant,
12+
dir_with_deprecated_constants,
13+
)
714
from .util.event_type import EventType
815
from .util.hass_dict import HassKey
916
from .util.signal_type import SignalType
@@ -758,7 +765,9 @@ class UnitOfPrecipitationDepth(StrEnum):
758765
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: Final = "mg/m³"
759766
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER: Final = "μg/m³"
760767
CONCENTRATION_MICROGRAMS_PER_CUBIC_FOOT: Final = "μg/ft³"
761-
CONCENTRATION_PARTS_PER_CUBIC_METER: Final = "p/m³"
768+
_DEPRECATED_CONCENTRATION_PARTS_PER_CUBIC_METER = DeprecatedConstant(
769+
"p/m³", "p/m³", "2027.7"
770+
)
762771
CONCENTRATION_PARTS_PER_MILLION: Final = "ppm"
763772
CONCENTRATION_PARTS_PER_BILLION: Final = "ppb"
764773

@@ -992,3 +1001,10 @@ class EntityCategory(StrEnum):
9921001
# This is not a hard limit, but caches and other
9931002
# data structures will be pre-allocated to this size
9941003
MAX_EXPECTED_ENTITY_IDS: Final = 16384
1004+
1005+
# These can be removed if no deprecated constants are in this module anymore
1006+
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
1007+
__dir__ = partial(
1008+
dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
1009+
)
1010+
__all__ = all_with_deprecated_constants(globals())

tests/test_const.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Test const module."""
2+
3+
import pytest
4+
5+
from homeassistant import const
6+
7+
from .common import help_test_all, import_and_test_deprecated_constant
8+
9+
10+
def test_all() -> None:
11+
"""Test module.__all__ is correctly set."""
12+
help_test_all(const)
13+
14+
15+
@pytest.mark.parametrize(
16+
("replacement", "constant_name", "breaks_in_version"),
17+
[
18+
(
19+
"p/m³",
20+
"CONCENTRATION_PARTS_PER_CUBIC_METER",
21+
"2027.7",
22+
),
23+
],
24+
)
25+
def test_deprecated_constant(
26+
caplog: pytest.LogCaptureFixture,
27+
replacement: str,
28+
constant_name: str,
29+
breaks_in_version: str,
30+
) -> None:
31+
"""Test deprecated constants, where no replacement is provided."""
32+
import_and_test_deprecated_constant(
33+
caplog,
34+
const,
35+
constant_name,
36+
replacement,
37+
replacement,
38+
breaks_in_version,
39+
)

0 commit comments

Comments
 (0)