Deprecate deprecated device tracker constants (#106099)

This commit is contained in:
Robert Resch 2023-12-20 18:06:38 +01:00 committed by GitHub
parent 28e4358c53
commit 9dd1b9e268
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 13 deletions

View File

@ -1,8 +1,14 @@
"""Provide functionality to keep track of devices.""" """Provide functionality to keep track of devices."""
from __future__ import annotations from __future__ import annotations
from functools import partial
from homeassistant.const import ATTR_GPS_ACCURACY, STATE_HOME # noqa: F401 from homeassistant.const import ATTR_GPS_ACCURACY, STATE_HOME # noqa: F401
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.deprecation import (
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
@ -13,6 +19,10 @@ from .config_entry import ( # noqa: F401
async_unload_entry, async_unload_entry,
) )
from .const import ( # noqa: F401 from .const import ( # noqa: F401
_DEPRECATED_SOURCE_TYPE_BLUETOOTH,
_DEPRECATED_SOURCE_TYPE_BLUETOOTH_LE,
_DEPRECATED_SOURCE_TYPE_GPS,
_DEPRECATED_SOURCE_TYPE_ROUTER,
ATTR_ATTRIBUTES, ATTR_ATTRIBUTES,
ATTR_BATTERY, ATTR_BATTERY,
ATTR_DEV_ID, ATTR_DEV_ID,
@ -32,10 +42,6 @@ from .const import ( # noqa: F401
DOMAIN, DOMAIN,
ENTITY_ID_FORMAT, ENTITY_ID_FORMAT,
SCAN_INTERVAL, SCAN_INTERVAL,
SOURCE_TYPE_BLUETOOTH,
SOURCE_TYPE_BLUETOOTH_LE,
SOURCE_TYPE_GPS,
SOURCE_TYPE_ROUTER,
SourceType, SourceType,
) )
from .legacy import ( # noqa: F401 from .legacy import ( # noqa: F401
@ -51,6 +57,12 @@ from .legacy import ( # noqa: F401
see, see,
) )
# 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())
@bind_hass @bind_hass
def is_on(hass: HomeAssistant, entity_id: str) -> bool: def is_on(hass: HomeAssistant, entity_id: str) -> bool:

View File

@ -3,9 +3,16 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from enum import StrEnum from enum import StrEnum
from functools import partial
import logging import logging
from typing import Final from typing import Final
from homeassistant.helpers.deprecation import (
DeprecatedConstantEnum,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
LOGGER: Final = logging.getLogger(__package__) LOGGER: Final = logging.getLogger(__package__)
DOMAIN: Final = "device_tracker" DOMAIN: Final = "device_tracker"
@ -14,13 +21,6 @@ ENTITY_ID_FORMAT: Final = DOMAIN + ".{}"
PLATFORM_TYPE_LEGACY: Final = "legacy" PLATFORM_TYPE_LEGACY: Final = "legacy"
PLATFORM_TYPE_ENTITY: Final = "entity_platform" PLATFORM_TYPE_ENTITY: Final = "entity_platform"
# SOURCE_TYPE_* below are deprecated as of 2022.9
# use the SourceType enum instead.
SOURCE_TYPE_GPS: Final = "gps"
SOURCE_TYPE_ROUTER: Final = "router"
SOURCE_TYPE_BLUETOOTH: Final = "bluetooth"
SOURCE_TYPE_BLUETOOTH_LE: Final = "bluetooth_le"
class SourceType(StrEnum): class SourceType(StrEnum):
"""Source type for device trackers.""" """Source type for device trackers."""
@ -31,6 +31,23 @@ class SourceType(StrEnum):
BLUETOOTH_LE = "bluetooth_le" BLUETOOTH_LE = "bluetooth_le"
# SOURCE_TYPE_* below are deprecated as of 2022.9
# use the SourceType enum instead.
_DEPRECATED_SOURCE_TYPE_GPS: Final = DeprecatedConstantEnum(SourceType.GPS, "2025.1")
_DEPRECATED_SOURCE_TYPE_ROUTER: Final = DeprecatedConstantEnum(
SourceType.ROUTER, "2025.1"
)
_DEPRECATED_SOURCE_TYPE_BLUETOOTH: Final = DeprecatedConstantEnum(
SourceType.BLUETOOTH, "2025.1"
)
_DEPRECATED_SOURCE_TYPE_BLUETOOTH_LE: Final = DeprecatedConstantEnum(
SourceType.BLUETOOTH_LE, "2025.1"
)
# 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())
CONF_SCAN_INTERVAL: Final = "interval_seconds" CONF_SCAN_INTERVAL: Final = "interval_seconds"
SCAN_INTERVAL: Final = timedelta(seconds=12) SCAN_INTERVAL: Final = timedelta(seconds=12)

View File

@ -3,6 +3,7 @@ from datetime import datetime, timedelta
import json import json
import logging import logging
import os import os
from types import ModuleType
from unittest.mock import Mock, call, patch from unittest.mock import Mock, call, patch
import pytest import pytest
@ -33,6 +34,7 @@ from . import common
from tests.common import ( from tests.common import (
assert_setup_component, assert_setup_component,
async_fire_time_changed, async_fire_time_changed,
import_and_test_deprecated_constant_enum,
mock_registry, mock_registry,
mock_restore_cache, mock_restore_cache,
patch_yaml_files, patch_yaml_files,
@ -681,3 +683,19 @@ def test_see_schema_allowing_ios_calls() -> None:
"hostname": "beer", "hostname": "beer",
} }
) )
@pytest.mark.parametrize(("enum"), list(SourceType))
@pytest.mark.parametrize(
"module",
[device_tracker, device_tracker.const],
)
def test_deprecated_constants(
caplog: pytest.LogCaptureFixture,
enum: SourceType,
module: ModuleType,
) -> None:
"""Test deprecated constants."""
import_and_test_deprecated_constant_enum(
caplog, module, enum, "SOURCE_TYPE_", "2025.1"
)

View File

@ -2,7 +2,7 @@
from homeassistant.components.device_tracker import DeviceScanner from homeassistant.components.device_tracker import DeviceScanner
from homeassistant.components.device_tracker.config_entry import ScannerEntity from homeassistant.components.device_tracker.config_entry import ScannerEntity
from homeassistant.components.device_tracker.const import SOURCE_TYPE_ROUTER from homeassistant.components.device_tracker.const import SourceType
async def async_get_scanner(hass, config): async def async_get_scanner(hass, config):
@ -23,7 +23,7 @@ class MockScannerEntity(ScannerEntity):
@property @property
def source_type(self): def source_type(self):
"""Return the source type, eg gps or router, of the device.""" """Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_ROUTER return SourceType.ROUTER
@property @property
def battery_level(self): def battery_level(self):