Add StrEnum for device_tracker SourceType (#75892)

Add StrEnum for device_tracker SourceType
This commit is contained in:
Marc Mueller 2022-07-29 13:09:03 +02:00 committed by GitHub
parent ab5dfb3c42
commit 2b1e1365fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 20 deletions

View File

@ -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,

View File

@ -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

View File

@ -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)

View File

@ -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
)

View File

@ -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):