Deprecate some deprecated const constants (#106230)
* Deprecate some deprecated const constants * Improve code * fix typing * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
55a5e9c4b5
commit
ebdf7b9c8c
6 changed files with 393 additions and 88 deletions
|
@ -18,36 +18,36 @@ from homeassistant.config_entries import ConfigEntry
|
||||||
|
|
||||||
# pylint: disable-next=hass-deprecated-import
|
# pylint: disable-next=hass-deprecated-import
|
||||||
from homeassistant.const import ( # noqa: F401
|
from homeassistant.const import ( # noqa: F401
|
||||||
|
_DEPRECATED_DEVICE_CLASS_AQI,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_BATTERY,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_CO,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_CO2,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_CURRENT,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_DATE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_ENERGY,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_FREQUENCY,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_GAS,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_HUMIDITY,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_MONETARY,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROGEN_DIOXIDE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROGEN_MONOXIDE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROUS_OXIDE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_OZONE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM1,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM10,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM25,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_POWER,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_POWER_FACTOR,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PRESSURE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_SULPHUR_DIOXIDE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_TEMPERATURE,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_TIMESTAMP,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
||||||
|
_DEPRECATED_DEVICE_CLASS_VOLTAGE,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
CONF_UNIT_OF_MEASUREMENT,
|
CONF_UNIT_OF_MEASUREMENT,
|
||||||
DEVICE_CLASS_AQI,
|
|
||||||
DEVICE_CLASS_BATTERY,
|
|
||||||
DEVICE_CLASS_CO,
|
|
||||||
DEVICE_CLASS_CO2,
|
|
||||||
DEVICE_CLASS_CURRENT,
|
|
||||||
DEVICE_CLASS_DATE,
|
|
||||||
DEVICE_CLASS_ENERGY,
|
|
||||||
DEVICE_CLASS_FREQUENCY,
|
|
||||||
DEVICE_CLASS_GAS,
|
|
||||||
DEVICE_CLASS_HUMIDITY,
|
|
||||||
DEVICE_CLASS_ILLUMINANCE,
|
|
||||||
DEVICE_CLASS_MONETARY,
|
|
||||||
DEVICE_CLASS_NITROGEN_DIOXIDE,
|
|
||||||
DEVICE_CLASS_NITROGEN_MONOXIDE,
|
|
||||||
DEVICE_CLASS_NITROUS_OXIDE,
|
|
||||||
DEVICE_CLASS_OZONE,
|
|
||||||
DEVICE_CLASS_PM1,
|
|
||||||
DEVICE_CLASS_PM10,
|
|
||||||
DEVICE_CLASS_PM25,
|
|
||||||
DEVICE_CLASS_POWER,
|
|
||||||
DEVICE_CLASS_POWER_FACTOR,
|
|
||||||
DEVICE_CLASS_PRESSURE,
|
|
||||||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
|
||||||
DEVICE_CLASS_SULPHUR_DIOXIDE,
|
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
|
||||||
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
|
|
||||||
DEVICE_CLASS_VOLTAGE,
|
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
UnitOfTemperature,
|
UnitOfTemperature,
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from typing import Final
|
from typing import Any, Final
|
||||||
|
|
||||||
APPLICATION_NAME: Final = "HomeAssistant"
|
APPLICATION_NAME: Final = "HomeAssistant"
|
||||||
MAJOR_VERSION: Final = 2024
|
MAJOR_VERSION: Final = 2024
|
||||||
|
@ -307,34 +307,147 @@ EVENT_SHOPPING_LIST_UPDATED: Final = "shopping_list_updated"
|
||||||
# #### DEVICE CLASSES ####
|
# #### DEVICE CLASSES ####
|
||||||
# DEVICE_CLASS_* below are deprecated as of 2021.12
|
# DEVICE_CLASS_* below are deprecated as of 2021.12
|
||||||
# use the SensorDeviceClass enum instead.
|
# use the SensorDeviceClass enum instead.
|
||||||
DEVICE_CLASS_AQI: Final = "aqi"
|
_DEPRECATED_DEVICE_CLASS_AQI: Final = ("aqi", "SensorDeviceClass.AQI", "2025.1")
|
||||||
DEVICE_CLASS_BATTERY: Final = "battery"
|
_DEPRECATED_DEVICE_CLASS_BATTERY: Final = (
|
||||||
DEVICE_CLASS_CO: Final = "carbon_monoxide"
|
"battery",
|
||||||
DEVICE_CLASS_CO2: Final = "carbon_dioxide"
|
"SensorDeviceClass.BATTERY",
|
||||||
DEVICE_CLASS_CURRENT: Final = "current"
|
"2025.1",
|
||||||
DEVICE_CLASS_DATE: Final = "date"
|
)
|
||||||
DEVICE_CLASS_ENERGY: Final = "energy"
|
_DEPRECATED_DEVICE_CLASS_CO: Final = (
|
||||||
DEVICE_CLASS_FREQUENCY: Final = "frequency"
|
"carbon_monoxide",
|
||||||
DEVICE_CLASS_GAS: Final = "gas"
|
"SensorDeviceClass.CO",
|
||||||
DEVICE_CLASS_HUMIDITY: Final = "humidity"
|
"2025.1",
|
||||||
DEVICE_CLASS_ILLUMINANCE: Final = "illuminance"
|
)
|
||||||
DEVICE_CLASS_MONETARY: Final = "monetary"
|
_DEPRECATED_DEVICE_CLASS_CO2: Final = (
|
||||||
DEVICE_CLASS_NITROGEN_DIOXIDE = "nitrogen_dioxide"
|
"carbon_dioxide",
|
||||||
DEVICE_CLASS_NITROGEN_MONOXIDE = "nitrogen_monoxide"
|
"SensorDeviceClass.CO2",
|
||||||
DEVICE_CLASS_NITROUS_OXIDE = "nitrous_oxide"
|
"2025.1",
|
||||||
DEVICE_CLASS_OZONE: Final = "ozone"
|
)
|
||||||
DEVICE_CLASS_PM1: Final = "pm1"
|
_DEPRECATED_DEVICE_CLASS_CURRENT: Final = (
|
||||||
DEVICE_CLASS_PM10: Final = "pm10"
|
"current",
|
||||||
DEVICE_CLASS_PM25: Final = "pm25"
|
"SensorDeviceClass.CURRENT",
|
||||||
DEVICE_CLASS_POWER_FACTOR: Final = "power_factor"
|
"2025.1",
|
||||||
DEVICE_CLASS_POWER: Final = "power"
|
)
|
||||||
DEVICE_CLASS_PRESSURE: Final = "pressure"
|
_DEPRECATED_DEVICE_CLASS_DATE: Final = ("date", "SensorDeviceClass.DATE", "2025.1")
|
||||||
DEVICE_CLASS_SIGNAL_STRENGTH: Final = "signal_strength"
|
_DEPRECATED_DEVICE_CLASS_ENERGY: Final = (
|
||||||
DEVICE_CLASS_SULPHUR_DIOXIDE = "sulphur_dioxide"
|
"energy",
|
||||||
DEVICE_CLASS_TEMPERATURE: Final = "temperature"
|
"SensorDeviceClass.ENERGY",
|
||||||
DEVICE_CLASS_TIMESTAMP: Final = "timestamp"
|
"2025.1",
|
||||||
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds"
|
)
|
||||||
DEVICE_CLASS_VOLTAGE: Final = "voltage"
|
_DEPRECATED_DEVICE_CLASS_FREQUENCY: Final = (
|
||||||
|
"frequency",
|
||||||
|
"SensorDeviceClass.FREQUENCY",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_GAS: Final = ("gas", "SensorDeviceClass.GAS", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_HUMIDITY: Final = (
|
||||||
|
"humidity",
|
||||||
|
"SensorDeviceClass.HUMIDITY",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_ILLUMINANCE: Final = (
|
||||||
|
"illuminance",
|
||||||
|
"SensorDeviceClass.ILLUMINANCE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_MONETARY: Final = (
|
||||||
|
"monetary",
|
||||||
|
"SensorDeviceClass.MONETARY",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROGEN_DIOXIDE = (
|
||||||
|
"nitrogen_dioxide",
|
||||||
|
"SensorDeviceClass.NITROGEN_DIOXIDE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROGEN_MONOXIDE = (
|
||||||
|
"nitrogen_monoxide",
|
||||||
|
"SensorDeviceClass.NITROGEN_MONOXIDE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_NITROUS_OXIDE = (
|
||||||
|
"nitrous_oxide",
|
||||||
|
"SensorDeviceClass.NITROUS_OXIDE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_OZONE: Final = ("ozone", "SensorDeviceClass.OZONE", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM1: Final = ("pm1", "SensorDeviceClass.PM1", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM10: Final = ("pm10", "SensorDeviceClass.PM10", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PM25: Final = ("pm25", "SensorDeviceClass.PM25", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_POWER_FACTOR: Final = (
|
||||||
|
"power_factor",
|
||||||
|
"SensorDeviceClass.POWER_FACTOR",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_POWER: Final = ("power", "SensorDeviceClass.POWER", "2025.1")
|
||||||
|
_DEPRECATED_DEVICE_CLASS_PRESSURE: Final = (
|
||||||
|
"pressure",
|
||||||
|
"SensorDeviceClass.PRESSURE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_SIGNAL_STRENGTH: Final = (
|
||||||
|
"signal_strength",
|
||||||
|
"SensorDeviceClass.SIGNAL_STRENGTH",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_SULPHUR_DIOXIDE = (
|
||||||
|
"sulphur_dioxide",
|
||||||
|
"SensorDeviceClass.SULPHUR_DIOXIDE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_TEMPERATURE: Final = (
|
||||||
|
"temperature",
|
||||||
|
"SensorDeviceClass.TEMPERATURE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_TIMESTAMP: Final = (
|
||||||
|
"timestamp",
|
||||||
|
"SensorDeviceClass.TIMESTAMP",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS = (
|
||||||
|
"volatile_organic_compounds",
|
||||||
|
"SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
_DEPRECATED_DEVICE_CLASS_VOLTAGE: Final = (
|
||||||
|
"voltage",
|
||||||
|
"SensorDeviceClass.VOLTAGE",
|
||||||
|
"2025.1",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Can be removed if no deprecated constant are in this module anymore
|
||||||
|
def __getattr__(name: str) -> Any:
|
||||||
|
"""Check if the not found name is a deprecated constant.
|
||||||
|
|
||||||
|
If it is, print a deprecation warning and return the value of the constant.
|
||||||
|
Otherwise raise AttributeError.
|
||||||
|
"""
|
||||||
|
module_globals = globals()
|
||||||
|
if f"_DEPRECATED_{name}" not in module_globals:
|
||||||
|
raise AttributeError(f"Module {__name__} has no attribute {name!r}")
|
||||||
|
|
||||||
|
# Avoid circular import
|
||||||
|
from .helpers.deprecation import ( # pylint: disable=import-outside-toplevel
|
||||||
|
check_if_deprecated_constant,
|
||||||
|
)
|
||||||
|
|
||||||
|
return check_if_deprecated_constant(name, module_globals)
|
||||||
|
|
||||||
|
|
||||||
|
# Can be removed if no deprecated constant are in this module anymore
|
||||||
|
def __dir__() -> list[str]:
|
||||||
|
"""Return dir() with deprecated constants."""
|
||||||
|
# Copied method from homeassistant.helpers.deprecattion#dir_with_deprecated_constants to avoid import cycle
|
||||||
|
module_globals = globals()
|
||||||
|
|
||||||
|
return list(module_globals) + [
|
||||||
|
name.removeprefix("_DEPRECATED_")
|
||||||
|
for name in module_globals
|
||||||
|
if name.startswith("_DEPRECATED_")
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
# #### STATES ####
|
# #### STATES ####
|
||||||
STATE_ON: Final = "on"
|
STATE_ON: Final = "on"
|
||||||
|
@ -1168,30 +1281,6 @@ PRECISION_TENTHS: Final = 0.1
|
||||||
# cloud, alexa, or google_home components
|
# cloud, alexa, or google_home components
|
||||||
CLOUD_NEVER_EXPOSED_ENTITIES: Final[list[str]] = ["group.all_locks"]
|
CLOUD_NEVER_EXPOSED_ENTITIES: Final[list[str]] = ["group.all_locks"]
|
||||||
|
|
||||||
# ENTITY_CATEGOR* below are deprecated as of 2021.12
|
|
||||||
# use the EntityCategory enum instead.
|
|
||||||
ENTITY_CATEGORY_CONFIG: Final = "config"
|
|
||||||
ENTITY_CATEGORY_DIAGNOSTIC: Final = "diagnostic"
|
|
||||||
ENTITY_CATEGORIES: Final[list[str]] = [
|
|
||||||
ENTITY_CATEGORY_CONFIG,
|
|
||||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
|
||||||
]
|
|
||||||
|
|
||||||
# The ID of the Home Assistant Media Player Cast App
|
|
||||||
CAST_APP_ID_HOMEASSISTANT_MEDIA: Final = "B45F4572"
|
|
||||||
# The ID of the Home Assistant Lovelace Cast App
|
|
||||||
CAST_APP_ID_HOMEASSISTANT_LOVELACE: Final = "A078F6B0"
|
|
||||||
|
|
||||||
# User used by Supervisor
|
|
||||||
HASSIO_USER_NAME = "Supervisor"
|
|
||||||
|
|
||||||
SIGNAL_BOOTSTRAP_INTEGRATIONS = "bootstrap_integrations"
|
|
||||||
|
|
||||||
# Date/Time formats
|
|
||||||
FORMAT_DATE: Final = "%Y-%m-%d"
|
|
||||||
FORMAT_TIME: Final = "%H:%M:%S"
|
|
||||||
FORMAT_DATETIME: Final = f"{FORMAT_DATE} {FORMAT_TIME}"
|
|
||||||
|
|
||||||
|
|
||||||
class EntityCategory(StrEnum):
|
class EntityCategory(StrEnum):
|
||||||
"""Category of an entity.
|
"""Category of an entity.
|
||||||
|
@ -1207,3 +1296,25 @@ class EntityCategory(StrEnum):
|
||||||
# Diagnostic: An entity exposing some configuration parameter,
|
# Diagnostic: An entity exposing some configuration parameter,
|
||||||
# or diagnostics of a device.
|
# or diagnostics of a device.
|
||||||
DIAGNOSTIC = "diagnostic"
|
DIAGNOSTIC = "diagnostic"
|
||||||
|
|
||||||
|
|
||||||
|
# ENTITY_CATEGOR* below are deprecated as of 2021.12
|
||||||
|
# use the EntityCategory enum instead.
|
||||||
|
_DEPRECATED_ENTITY_CATEGORY_CONFIG: Final = (EntityCategory.CONFIG, "2025.1")
|
||||||
|
_DEPRECATED_ENTITY_CATEGORY_DIAGNOSTIC: Final = (EntityCategory.DIAGNOSTIC, "2025.1")
|
||||||
|
ENTITY_CATEGORIES: Final[list[str]] = [cls.value for cls in EntityCategory]
|
||||||
|
|
||||||
|
# The ID of the Home Assistant Media Player Cast App
|
||||||
|
CAST_APP_ID_HOMEASSISTANT_MEDIA: Final = "B45F4572"
|
||||||
|
# The ID of the Home Assistant Lovelace Cast App
|
||||||
|
CAST_APP_ID_HOMEASSISTANT_LOVELACE: Final = "A078F6B0"
|
||||||
|
|
||||||
|
# User used by Supervisor
|
||||||
|
HASSIO_USER_NAME = "Supervisor"
|
||||||
|
|
||||||
|
SIGNAL_BOOTSTRAP_INTEGRATIONS = "bootstrap_integrations"
|
||||||
|
|
||||||
|
# Date/Time formats
|
||||||
|
FORMAT_DATE: Final = "%Y-%m-%d"
|
||||||
|
FORMAT_TIME: Final = "%H:%M:%S"
|
||||||
|
FORMAT_DATETIME: Final = f"{FORMAT_DATE} {FORMAT_TIME}"
|
||||||
|
|
|
@ -252,6 +252,7 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
|
||||||
"""
|
"""
|
||||||
module_name = module_globals.get("__name__")
|
module_name = module_globals.get("__name__")
|
||||||
logger = logging.getLogger(module_name)
|
logger = logging.getLogger(module_name)
|
||||||
|
value = replacement = None
|
||||||
if (deprecated_const := module_globals.get(_PREFIX_DEPRECATED + name)) is None:
|
if (deprecated_const := module_globals.get(_PREFIX_DEPRECATED + name)) is None:
|
||||||
raise AttributeError(f"Module {module_name!r} has no attribute {name!r}")
|
raise AttributeError(f"Module {module_name!r} has no attribute {name!r}")
|
||||||
if isinstance(deprecated_const, DeprecatedConstant):
|
if isinstance(deprecated_const, DeprecatedConstant):
|
||||||
|
@ -264,9 +265,22 @@ def check_if_deprecated_constant(name: str, module_globals: dict[str, Any]) -> A
|
||||||
f"{deprecated_const.enum.__class__.__name__}.{deprecated_const.enum.name}"
|
f"{deprecated_const.enum.__class__.__name__}.{deprecated_const.enum.name}"
|
||||||
)
|
)
|
||||||
breaks_in_ha_version = deprecated_const.breaks_in_ha_version
|
breaks_in_ha_version = deprecated_const.breaks_in_ha_version
|
||||||
else:
|
elif isinstance(deprecated_const, tuple):
|
||||||
|
# Use DeprecatedConstant and DeprecatedConstant instead, where possible
|
||||||
|
# Used to avoid import cycles.
|
||||||
|
if len(deprecated_const) == 3:
|
||||||
|
value = deprecated_const[0]
|
||||||
|
replacement = deprecated_const[1]
|
||||||
|
breaks_in_ha_version = deprecated_const[2]
|
||||||
|
elif len(deprecated_const) == 2 and isinstance(deprecated_const[0], Enum):
|
||||||
|
enum = deprecated_const[0]
|
||||||
|
value = enum.value
|
||||||
|
replacement = f"{enum.__class__.__name__}.{enum.name}"
|
||||||
|
breaks_in_ha_version = deprecated_const[1]
|
||||||
|
|
||||||
|
if value is None or replacement is None:
|
||||||
msg = (
|
msg = (
|
||||||
f"Value of {_PREFIX_DEPRECATED}{name!r} is an instance of {type(deprecated_const)} "
|
f"Value of {_PREFIX_DEPRECATED}{name} is an instance of {type(deprecated_const)} "
|
||||||
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
|
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -2535,3 +2535,46 @@ def test_deprecated_constants(
|
||||||
import_and_test_deprecated_constant_enum(
|
import_and_test_deprecated_constant_enum(
|
||||||
caplog, module, enum, "STATE_CLASS_", "2025.1"
|
caplog, module, enum, "STATE_CLASS_", "2025.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("enum"),
|
||||||
|
[
|
||||||
|
sensor.SensorDeviceClass.AQI,
|
||||||
|
sensor.SensorDeviceClass.BATTERY,
|
||||||
|
sensor.SensorDeviceClass.CO,
|
||||||
|
sensor.SensorDeviceClass.CO2,
|
||||||
|
sensor.SensorDeviceClass.CURRENT,
|
||||||
|
sensor.SensorDeviceClass.DATE,
|
||||||
|
sensor.SensorDeviceClass.ENERGY,
|
||||||
|
sensor.SensorDeviceClass.FREQUENCY,
|
||||||
|
sensor.SensorDeviceClass.GAS,
|
||||||
|
sensor.SensorDeviceClass.HUMIDITY,
|
||||||
|
sensor.SensorDeviceClass.ILLUMINANCE,
|
||||||
|
sensor.SensorDeviceClass.MONETARY,
|
||||||
|
sensor.SensorDeviceClass.NITROGEN_DIOXIDE,
|
||||||
|
sensor.SensorDeviceClass.NITROGEN_MONOXIDE,
|
||||||
|
sensor.SensorDeviceClass.NITROUS_OXIDE,
|
||||||
|
sensor.SensorDeviceClass.OZONE,
|
||||||
|
sensor.SensorDeviceClass.PM1,
|
||||||
|
sensor.SensorDeviceClass.PM10,
|
||||||
|
sensor.SensorDeviceClass.PM25,
|
||||||
|
sensor.SensorDeviceClass.POWER_FACTOR,
|
||||||
|
sensor.SensorDeviceClass.POWER,
|
||||||
|
sensor.SensorDeviceClass.PRESSURE,
|
||||||
|
sensor.SensorDeviceClass.SIGNAL_STRENGTH,
|
||||||
|
sensor.SensorDeviceClass.SULPHUR_DIOXIDE,
|
||||||
|
sensor.SensorDeviceClass.TEMPERATURE,
|
||||||
|
sensor.SensorDeviceClass.TIMESTAMP,
|
||||||
|
sensor.SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
|
||||||
|
sensor.SensorDeviceClass.VOLTAGE,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_deprecated_constants_sensor_device_class(
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
enum: sensor.SensorStateClass,
|
||||||
|
) -> None:
|
||||||
|
"""Test deprecated constants."""
|
||||||
|
import_and_test_deprecated_constant_enum(
|
||||||
|
caplog, sensor, enum, "DEVICE_CLASS_", "2025.1"
|
||||||
|
)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
"""Test deprecation helpers."""
|
"""Test deprecation helpers."""
|
||||||
|
from enum import StrEnum
|
||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
@ -257,6 +258,26 @@ def test_deprecated_function_called_from_custom_integration(
|
||||||
) in caplog.text
|
) in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeprecatedConstantEnum(StrEnum):
|
||||||
|
"""Test deprecated constant enum."""
|
||||||
|
|
||||||
|
TEST = "value"
|
||||||
|
|
||||||
|
|
||||||
|
def _get_value(obj: DeprecatedConstant | DeprecatedConstantEnum | tuple) -> Any:
|
||||||
|
if isinstance(obj, tuple):
|
||||||
|
if len(obj) == 2:
|
||||||
|
return obj[0].value
|
||||||
|
|
||||||
|
return obj[0]
|
||||||
|
|
||||||
|
if isinstance(obj, DeprecatedConstant):
|
||||||
|
return obj.value
|
||||||
|
|
||||||
|
if isinstance(obj, DeprecatedConstantEnum):
|
||||||
|
return obj.enum.value
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("deprecated_constant", "extra_msg"),
|
("deprecated_constant", "extra_msg"),
|
||||||
[
|
[
|
||||||
|
@ -268,6 +289,30 @@ def test_deprecated_function_called_from_custom_integration(
|
||||||
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
|
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
|
||||||
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
DeprecatedConstantEnum(TestDeprecatedConstantEnum.TEST, None),
|
||||||
|
". Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
DeprecatedConstantEnum(TestDeprecatedConstantEnum.TEST, "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
("value", "NEW_CONSTANT", None),
|
||||||
|
". Use NEW_CONSTANT instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(1, "NEW_CONSTANT", "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(TestDeprecatedConstantEnum.TEST, None),
|
||||||
|
". Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(TestDeprecatedConstantEnum.TEST, "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -282,7 +327,7 @@ def test_deprecated_function_called_from_custom_integration(
|
||||||
)
|
)
|
||||||
def test_check_if_deprecated_constant(
|
def test_check_if_deprecated_constant(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum,
|
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum | tuple,
|
||||||
extra_msg: str,
|
extra_msg: str,
|
||||||
module_name: str,
|
module_name: str,
|
||||||
extra_extra_msg: str,
|
extra_extra_msg: str,
|
||||||
|
@ -316,7 +361,7 @@ def test_check_if_deprecated_constant(
|
||||||
],
|
],
|
||||||
):
|
):
|
||||||
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
||||||
assert value == deprecated_constant.value
|
assert value == _get_value(deprecated_constant)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
module_name,
|
module_name,
|
||||||
|
@ -336,6 +381,30 @@ def test_check_if_deprecated_constant(
|
||||||
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
|
DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"),
|
||||||
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
DeprecatedConstantEnum(TestDeprecatedConstantEnum.TEST, None),
|
||||||
|
". Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
DeprecatedConstantEnum(TestDeprecatedConstantEnum.TEST, "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
("value", "NEW_CONSTANT", None),
|
||||||
|
". Use NEW_CONSTANT instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(1, "NEW_CONSTANT", "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(TestDeprecatedConstantEnum.TEST, None),
|
||||||
|
". Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
|
(
|
||||||
|
(TestDeprecatedConstantEnum.TEST, "2099.1"),
|
||||||
|
" which will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
@ -347,7 +416,7 @@ def test_check_if_deprecated_constant(
|
||||||
)
|
)
|
||||||
def test_check_if_deprecated_constant_integration_not_found(
|
def test_check_if_deprecated_constant_integration_not_found(
|
||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum,
|
deprecated_constant: DeprecatedConstant | DeprecatedConstantEnum | tuple,
|
||||||
extra_msg: str,
|
extra_msg: str,
|
||||||
module_name: str,
|
module_name: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
@ -361,7 +430,7 @@ def test_check_if_deprecated_constant_integration_not_found(
|
||||||
"homeassistant.helpers.frame.extract_stack", side_effect=MissingIntegrationFrame
|
"homeassistant.helpers.frame.extract_stack", side_effect=MissingIntegrationFrame
|
||||||
):
|
):
|
||||||
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
value = check_if_deprecated_constant("TEST_CONSTANT", module_globals)
|
||||||
assert value == deprecated_constant.value
|
assert value == _get_value(deprecated_constant)
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
module_name,
|
module_name,
|
||||||
|
@ -379,7 +448,7 @@ def test_test_check_if_deprecated_constant_invalid(
|
||||||
name = "TEST_CONSTANT"
|
name = "TEST_CONSTANT"
|
||||||
|
|
||||||
excepted_msg = (
|
excepted_msg = (
|
||||||
f"Value of _DEPRECATED_{name!r} is an instance of <class 'int'> "
|
f"Value of _DEPRECATED_{name} is an instance of <class 'int'> "
|
||||||
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
|
"but an instance of DeprecatedConstant or DeprecatedConstantEnum is required"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
68
tests/test_const.py
Normal file
68
tests/test_const.py
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
"""Test const module."""
|
||||||
|
|
||||||
|
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant import const
|
||||||
|
from homeassistant.components import sensor
|
||||||
|
|
||||||
|
from tests.common import import_and_test_deprecated_constant_enum
|
||||||
|
|
||||||
|
|
||||||
|
def _create_tuples(
|
||||||
|
value: Enum | list[Enum], constant_prefix: str
|
||||||
|
) -> list[tuple[Enum, str]]:
|
||||||
|
result = []
|
||||||
|
for enum in value:
|
||||||
|
result.append((enum, constant_prefix))
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("enum", "constant_prefix"),
|
||||||
|
_create_tuples(const.EntityCategory, "ENTITY_CATEGORY_")
|
||||||
|
+ _create_tuples(
|
||||||
|
[
|
||||||
|
sensor.SensorDeviceClass.AQI,
|
||||||
|
sensor.SensorDeviceClass.BATTERY,
|
||||||
|
sensor.SensorDeviceClass.CO,
|
||||||
|
sensor.SensorDeviceClass.CO2,
|
||||||
|
sensor.SensorDeviceClass.CURRENT,
|
||||||
|
sensor.SensorDeviceClass.DATE,
|
||||||
|
sensor.SensorDeviceClass.ENERGY,
|
||||||
|
sensor.SensorDeviceClass.FREQUENCY,
|
||||||
|
sensor.SensorDeviceClass.GAS,
|
||||||
|
sensor.SensorDeviceClass.HUMIDITY,
|
||||||
|
sensor.SensorDeviceClass.ILLUMINANCE,
|
||||||
|
sensor.SensorDeviceClass.MONETARY,
|
||||||
|
sensor.SensorDeviceClass.NITROGEN_DIOXIDE,
|
||||||
|
sensor.SensorDeviceClass.NITROGEN_MONOXIDE,
|
||||||
|
sensor.SensorDeviceClass.NITROUS_OXIDE,
|
||||||
|
sensor.SensorDeviceClass.OZONE,
|
||||||
|
sensor.SensorDeviceClass.PM1,
|
||||||
|
sensor.SensorDeviceClass.PM10,
|
||||||
|
sensor.SensorDeviceClass.PM25,
|
||||||
|
sensor.SensorDeviceClass.POWER_FACTOR,
|
||||||
|
sensor.SensorDeviceClass.POWER,
|
||||||
|
sensor.SensorDeviceClass.PRESSURE,
|
||||||
|
sensor.SensorDeviceClass.SIGNAL_STRENGTH,
|
||||||
|
sensor.SensorDeviceClass.SULPHUR_DIOXIDE,
|
||||||
|
sensor.SensorDeviceClass.TEMPERATURE,
|
||||||
|
sensor.SensorDeviceClass.TIMESTAMP,
|
||||||
|
sensor.SensorDeviceClass.VOLATILE_ORGANIC_COMPOUNDS,
|
||||||
|
sensor.SensorDeviceClass.VOLTAGE,
|
||||||
|
],
|
||||||
|
"DEVICE_CLASS_",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_deprecated_constants(
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
|
enum: Enum,
|
||||||
|
constant_prefix: str,
|
||||||
|
) -> None:
|
||||||
|
"""Test deprecated constants."""
|
||||||
|
import_and_test_deprecated_constant_enum(
|
||||||
|
caplog, const, enum, constant_prefix, "2025.1"
|
||||||
|
)
|
Loading…
Add table
Add a link
Reference in a new issue