From 2b1e1365fdb3bfe72feb515fcf2e02331caa4088 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 29 Jul 2022 13:09:03 +0200 Subject: [PATCH] Add StrEnum for device_tracker `SourceType` (#75892) Add StrEnum for device_tracker SourceType --- .../components/device_tracker/__init__.py | 1 + .../components/device_tracker/config_entry.py | 3 +- .../components/device_tracker/const.py | 16 ++++++++++ .../components/device_tracker/legacy.py | 29 +++++++++---------- .../components/mobile_app/device_tracker.py | 6 ++-- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 5617d56ac3f..9e58c5bbc92 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -26,6 +26,7 @@ from .const import ( # noqa: F401 SOURCE_TYPE_BLUETOOTH_LE, SOURCE_TYPE_GPS, SOURCE_TYPE_ROUTER, + SourceType, ) from .legacy import ( # noqa: F401 PLATFORM_SCHEMA, diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index c9b8534c2bc..b587f17d58e 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -30,6 +30,7 @@ from .const import ( CONNECTED_DEVICE_REGISTERED, DOMAIN, LOGGER, + SourceType, ) @@ -187,7 +188,7 @@ class BaseTrackerEntity(Entity): return None @property - def source_type(self) -> str: + def source_type(self) -> SourceType | str: """Return the source type, eg gps or router, of the device.""" raise NotImplementedError diff --git a/homeassistant/components/device_tracker/const.py b/homeassistant/components/device_tracker/const.py index c52241ae51f..ad68472d9b0 100644 --- a/homeassistant/components/device_tracker/const.py +++ b/homeassistant/components/device_tracker/const.py @@ -1,8 +1,12 @@ """Device tracker constants.""" +from __future__ import annotations + from datetime import timedelta import logging from typing import Final +from homeassistant.backports.enum import StrEnum + LOGGER: Final = logging.getLogger(__package__) DOMAIN: Final = "device_tracker" @@ -11,11 +15,23 @@ 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.""" + + GPS = "gps" + ROUTER = "router" + BLUETOOTH = "bluetooth" + BLUETOOTH_LE = "bluetooth_le" + + CONF_SCAN_INTERVAL: Final = "interval_seconds" SCAN_INTERVAL: Final = timedelta(seconds=12) diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index d8097a68ad5..8216c5fba27 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -66,19 +66,16 @@ from .const import ( LOGGER, PLATFORM_TYPE_LEGACY, SCAN_INTERVAL, - SOURCE_TYPE_BLUETOOTH, - SOURCE_TYPE_BLUETOOTH_LE, - SOURCE_TYPE_GPS, - SOURCE_TYPE_ROUTER, + SourceType, ) SERVICE_SEE: Final = "see" SOURCE_TYPES: Final[tuple[str, ...]] = ( - SOURCE_TYPE_GPS, - SOURCE_TYPE_ROUTER, - SOURCE_TYPE_BLUETOOTH, - SOURCE_TYPE_BLUETOOTH_LE, + SourceType.GPS, + SourceType.ROUTER, + SourceType.BLUETOOTH, + SourceType.BLUETOOTH_LE, ) NEW_DEVICE_DEFAULTS_SCHEMA = vol.Any( @@ -137,7 +134,7 @@ class SeeCallback(Protocol): gps_accuracy: int | None = None, battery: int | None = None, attributes: dict[str, Any] | None = None, - source_type: str = SOURCE_TYPE_GPS, + source_type: SourceType | str = SourceType.GPS, picture: str | None = None, icon: str | None = None, consider_home: timedelta | None = None, @@ -158,7 +155,7 @@ class AsyncSeeCallback(Protocol): gps_accuracy: int | None = None, battery: int | None = None, attributes: dict[str, Any] | None = None, - source_type: str = SOURCE_TYPE_GPS, + source_type: SourceType | str = SourceType.GPS, picture: str | None = None, icon: str | None = None, consider_home: timedelta | None = None, @@ -412,7 +409,7 @@ def async_setup_scanner_platform( kwargs: dict[str, Any] = { "mac": mac, "host_name": host_name, - "source_type": SOURCE_TYPE_ROUTER, + "source_type": SourceType.ROUTER, "attributes": { "scanner": scanner.__class__.__name__, **extra_attributes, @@ -490,7 +487,7 @@ class DeviceTracker: gps_accuracy: int | None = None, battery: int | None = None, attributes: dict[str, Any] | None = None, - source_type: str = SOURCE_TYPE_GPS, + source_type: SourceType | str = SourceType.GPS, picture: str | None = None, icon: str | None = None, consider_home: timedelta | None = None, @@ -523,7 +520,7 @@ class DeviceTracker: gps_accuracy: int | None = None, battery: int | None = None, attributes: dict[str, Any] | None = None, - source_type: str = SOURCE_TYPE_GPS, + source_type: SourceType | str = SourceType.GPS, picture: str | None = None, icon: str | None = None, consider_home: timedelta | None = None, @@ -709,7 +706,7 @@ class Device(RestoreEntity): self._icon = icon - self.source_type: str | None = None + self.source_type: SourceType | str | None = None self._attributes: dict[str, Any] = {} @@ -762,7 +759,7 @@ class Device(RestoreEntity): gps_accuracy: int | None = None, battery: int | None = None, attributes: dict[str, Any] | None = None, - source_type: str = SOURCE_TYPE_GPS, + source_type: SourceType | str = SourceType.GPS, consider_home: timedelta | None = None, ) -> None: """Mark the device as seen.""" @@ -815,7 +812,7 @@ class Device(RestoreEntity): return if self.location_name: self._state = self.location_name - elif self.gps is not None and self.source_type == SOURCE_TYPE_GPS: + elif self.gps is not None and self.source_type == SourceType.GPS: zone_state = zone.async_active_zone( self.hass, self.gps[0], self.gps[1], self.gps_accuracy ) diff --git a/homeassistant/components/mobile_app/device_tracker.py b/homeassistant/components/mobile_app/device_tracker.py index 0f6f0835c3b..d0f1db6caff 100644 --- a/homeassistant/components/mobile_app/device_tracker.py +++ b/homeassistant/components/mobile_app/device_tracker.py @@ -6,7 +6,7 @@ from homeassistant.components.device_tracker import ( ATTR_LOCATION_NAME, ) from homeassistant.components.device_tracker.config_entry import TrackerEntity -from homeassistant.components.device_tracker.const import SOURCE_TYPE_GPS +from homeassistant.components.device_tracker.const import SourceType from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_BATTERY_LEVEL, @@ -103,9 +103,9 @@ class MobileAppEntity(TrackerEntity, RestoreEntity): return self._entry.data[ATTR_DEVICE_NAME] @property - def source_type(self): + def source_type(self) -> SourceType: """Return the source type, eg gps or router, of the device.""" - return SOURCE_TYPE_GPS + return SourceType.GPS @property def device_info(self):