-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy pathdevice_tracker.py
72 lines (57 loc) · 2.22 KB
/
device_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Tracking for bluetooth low energy devices."""
from __future__ import annotations
from collections.abc import Mapping
import logging
from homeassistant.components import bluetooth
from homeassistant.components.device_tracker import SourceType
from homeassistant.components.device_tracker.config_entry import BaseTrackerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_HOME, STATE_NOT_HOME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import BasePrivateDeviceEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Load Device Tracker entities for a config entry."""
async_add_entities([BasePrivateDeviceTracker(config_entry)])
class BasePrivateDeviceTracker(BasePrivateDeviceEntity, BaseTrackerEntity):
"""A trackable Private Bluetooth Device."""
_attr_should_poll = False
_attr_has_entity_name = True
_attr_translation_key = "device_tracker"
_attr_name = None
@property
def extra_state_attributes(self) -> Mapping[str, str]:
"""Return extra state attributes for this device."""
if last_info := self._last_info:
return {
"current_address": last_info.address,
"source": last_info.source,
}
return {}
@callback
def _async_track_unavailable(
self, service_info: bluetooth.BluetoothServiceInfoBleak
) -> None:
self._last_info = None
self.async_write_ha_state()
@callback
def _async_track_service_info(
self,
service_info: bluetooth.BluetoothServiceInfoBleak,
change: bluetooth.BluetoothChange,
) -> None:
self._last_info = service_info
self.async_write_ha_state()
@property
def state(self) -> str:
"""Return the state of the device."""
return STATE_HOME if self._last_info else STATE_NOT_HOME
@property
def source_type(self) -> SourceType:
"""Return the source type, eg gps or router, of the device."""
return SourceType.BLUETOOTH_LE