Skip to content

Commit

Permalink
Deprecate deprecated sensor constants (home-assistant#106120)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Dec 20, 2023
1 parent 494a897 commit d47ec91
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
17 changes: 14 additions & 3 deletions homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dataclasses import dataclass
from datetime import UTC, date, datetime, timedelta
from decimal import Decimal, InvalidOperation as DecimalInvalidOperation
from functools import partial
import logging
from math import ceil, floor, isfinite, log10
from typing import Any, Final, Self, cast, final
Expand Down Expand Up @@ -57,6 +58,10 @@
PLATFORM_SCHEMA,
PLATFORM_SCHEMA_BASE,
)
from homeassistant.helpers.deprecation import (
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_platform import EntityPlatform
Expand All @@ -66,6 +71,9 @@
from homeassistant.util.enum import try_parse_enum

from .const import ( # noqa: F401
_DEPRECATED_STATE_CLASS_MEASUREMENT,
_DEPRECATED_STATE_CLASS_TOTAL,
_DEPRECATED_STATE_CLASS_TOTAL_INCREASING,
ATTR_LAST_RESET,
ATTR_OPTIONS,
ATTR_STATE_CLASS,
Expand All @@ -76,9 +84,6 @@
DEVICE_CLASSES_SCHEMA,
DOMAIN,
NON_NUMERIC_DEVICE_CLASSES,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL,
STATE_CLASS_TOTAL_INCREASING,
STATE_CLASSES,
STATE_CLASSES_SCHEMA,
UNIT_CONVERTERS,
Expand Down Expand Up @@ -110,6 +115,12 @@
"SensorStateClass",
]

# As we import deprecated constants from the const module, we need to add these two functions
# otherwise this module will be logged for using deprecated constants and not the custom component
# Both can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(dir_with_deprecated_constants, module_globals=globals())

# mypy: disallow-any-generics


Expand Down
22 changes: 19 additions & 3 deletions homeassistant/components/sensor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from enum import StrEnum
from functools import partial
from typing import Final

import voluptuous as vol
Expand Down Expand Up @@ -35,6 +36,11 @@
UnitOfVolume,
UnitOfVolumetricFlux,
)
from homeassistant.helpers.deprecation import (
DeprecatedConstantEnum,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from homeassistant.util.unit_conversion import (
BaseUnitConverter,
DataRateConverter,
Expand Down Expand Up @@ -451,11 +457,21 @@ class SensorStateClass(StrEnum):

# STATE_CLASS* is deprecated as of 2021.12
# use the SensorStateClass enum instead.
STATE_CLASS_MEASUREMENT: Final = "measurement"
STATE_CLASS_TOTAL: Final = "total"
STATE_CLASS_TOTAL_INCREASING: Final = "total_increasing"
_DEPRECATED_STATE_CLASS_MEASUREMENT: Final = DeprecatedConstantEnum(
SensorStateClass.MEASUREMENT, "2025.1"
)
_DEPRECATED_STATE_CLASS_TOTAL: Final = DeprecatedConstantEnum(
SensorStateClass.TOTAL, "2025.1"
)
_DEPRECATED_STATE_CLASS_TOTAL_INCREASING: Final = DeprecatedConstantEnum(
SensorStateClass.TOTAL_INCREASING, "2025.1"
)
STATE_CLASSES: Final[list[str]] = [cls.value for cls in SensorStateClass]

# Both can be removed if no deprecated constant are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(dir_with_deprecated_constants, module_globals=globals())

UNIT_CONVERTERS: dict[SensorDeviceClass | str | None, type[BaseUnitConverter]] = {
SensorDeviceClass.ATMOSPHERIC_PRESSURE: PressureConverter,
SensorDeviceClass.CURRENT: ElectricCurrentConverter,
Expand Down
16 changes: 16 additions & 0 deletions tests/components/sensor/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from collections.abc import Generator
from datetime import UTC, date, datetime
from decimal import Decimal
from types import ModuleType
from typing import Any

import pytest

from homeassistant.components import sensor
from homeassistant.components.number import NumberDeviceClass
from homeassistant.components.sensor import (
DEVICE_CLASS_STATE_CLASSES,
Expand Down Expand Up @@ -50,6 +52,7 @@
MockModule,
MockPlatform,
async_mock_restore_state_shutdown_restart,
import_and_test_deprecated_constant_enum,
mock_config_flow,
mock_integration,
mock_platform,
Expand Down Expand Up @@ -2519,3 +2522,16 @@ async def test_entity_category_config_raises_error(
)

assert not hass.states.get("sensor.test")


@pytest.mark.parametrize(("enum"), list(sensor.SensorStateClass))
@pytest.mark.parametrize(("module"), [sensor, sensor.const])
def test_deprecated_constants(
caplog: pytest.LogCaptureFixture,
enum: sensor.SensorStateClass,
module: ModuleType,
) -> None:
"""Test deprecated constants."""
import_and_test_deprecated_constant_enum(
caplog, module, enum, "STATE_CLASS_", "2025.1"
)

0 comments on commit d47ec91

Please sign in to comment.