Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/device_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SOURCE_TYPE_BLUETOOTH_LE,
SOURCE_TYPE_GPS,
SOURCE_TYPE_ROUTER,
SourceType,
)
from .legacy import ( # noqa: F401
PLATFORM_SCHEMA,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/device_tracker/config_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONNECTED_DEVICE_REGISTERED,
DOMAIN,
LOGGER,
SourceType,
)


Expand Down Expand Up @@ -187,7 +188,7 @@ def battery_level(self) -> int | None:
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

Expand Down
16 changes: 16 additions & 0 deletions homeassistant/components/device_tracker/const.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -11,11 +15,23 @@
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)

Expand Down
29 changes: 13 additions & 16 deletions homeassistant/components/device_tracker/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,16 @@
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(
Expand Down Expand Up @@ -137,7 +134,7 @@ def __call__(
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,
Expand All @@ -158,7 +155,7 @@ async def __call__(
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,
Expand Down Expand Up @@ -412,7 +409,7 @@ async def async_device_tracker_scan(now: datetime | None) -> None:
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,
Expand Down Expand Up @@ -490,7 +487,7 @@ def see(
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,
Expand Down Expand Up @@ -523,7 +520,7 @@ async def async_see(
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,
Expand Down Expand Up @@ -709,7 +706,7 @@ def __init__(

self._icon = icon

self.source_type: str | None = None
self.source_type: SourceType | str | None = None

self._attributes: dict[str, Any] = {}

Expand Down Expand Up @@ -762,7 +759,7 @@ async def async_seen(
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."""
Expand Down Expand Up @@ -815,7 +812,7 @@ async def async_update(self) -> None:
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
)
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/mobile_app/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
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,
Expand Down Expand Up @@ -103,9 +103,9 @@ def name(self):
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):
Expand Down