diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index a6a8e9d2d8c..b5ad4660cde 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -1,8 +1,14 @@ """Provide functionality to keep track of devices.""" from __future__ import annotations +from functools import partial + from homeassistant.const import ATTR_GPS_ACCURACY, STATE_HOME # noqa: F401 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.loader import bind_hass @@ -13,6 +19,10 @@ from .config_entry import ( # noqa: F401 async_unload_entry, ) 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_BATTERY, ATTR_DEV_ID, @@ -32,10 +42,6 @@ from .const import ( # noqa: F401 DOMAIN, ENTITY_ID_FORMAT, SCAN_INTERVAL, - SOURCE_TYPE_BLUETOOTH, - SOURCE_TYPE_BLUETOOTH_LE, - SOURCE_TYPE_GPS, - SOURCE_TYPE_ROUTER, SourceType, ) from .legacy import ( # noqa: F401 @@ -51,6 +57,12 @@ from .legacy import ( # noqa: F401 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 def is_on(hass: HomeAssistant, entity_id: str) -> bool: diff --git a/homeassistant/components/device_tracker/const.py b/homeassistant/components/device_tracker/const.py index 3a0b0afd7c9..10c16e09107 100644 --- a/homeassistant/components/device_tracker/const.py +++ b/homeassistant/components/device_tracker/const.py @@ -3,9 +3,16 @@ from __future__ import annotations from datetime import timedelta from enum import StrEnum +from functools import partial import logging from typing import Final +from homeassistant.helpers.deprecation import ( + DeprecatedConstantEnum, + check_if_deprecated_constant, + dir_with_deprecated_constants, +) + LOGGER: Final = logging.getLogger(__package__) DOMAIN: Final = "device_tracker" @@ -14,13 +21,6 @@ ENTITY_ID_FORMAT: Final = DOMAIN + ".{}" PLATFORM_TYPE_LEGACY: Final = "legacy" 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): """Source type for device trackers.""" @@ -31,6 +31,23 @@ class SourceType(StrEnum): 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" SCAN_INTERVAL: Final = timedelta(seconds=12) diff --git a/tests/components/device_tracker/test_init.py b/tests/components/device_tracker/test_init.py index 2960789c646..024187a33f6 100644 --- a/tests/components/device_tracker/test_init.py +++ b/tests/components/device_tracker/test_init.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta import json import logging import os +from types import ModuleType from unittest.mock import Mock, call, patch import pytest @@ -33,6 +34,7 @@ from . import common from tests.common import ( assert_setup_component, async_fire_time_changed, + import_and_test_deprecated_constant_enum, mock_registry, mock_restore_cache, patch_yaml_files, @@ -681,3 +683,19 @@ def test_see_schema_allowing_ios_calls() -> None: "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" + ) diff --git a/tests/testing_config/custom_components/test/device_tracker.py b/tests/testing_config/custom_components/test/device_tracker.py index 31294a48e3d..11eb366f2fc 100644 --- a/tests/testing_config/custom_components/test/device_tracker.py +++ b/tests/testing_config/custom_components/test/device_tracker.py @@ -2,7 +2,7 @@ from homeassistant.components.device_tracker import DeviceScanner 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): @@ -23,7 +23,7 @@ class MockScannerEntity(ScannerEntity): @property def source_type(self): """Return the source type, eg gps or router, of the device.""" - return SOURCE_TYPE_ROUTER + return SourceType.ROUTER @property def battery_level(self):