-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
sensor.py
139 lines (118 loc) · 4.38 KB
/
sensor.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Support for iBeacon device sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from ibeacon_ble import iBeaconAdvertisement
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfLength
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import IBeaconConfigEntry
from .const import SIGNAL_IBEACON_DEVICE_NEW
from .coordinator import IBeaconCoordinator
from .entity import IBeaconEntity
@dataclass(frozen=True, kw_only=True)
class IBeaconSensorEntityDescription(SensorEntityDescription):
"""Describes iBeacon sensor entity."""
value_fn: Callable[[iBeaconAdvertisement], str | int | None]
SENSOR_DESCRIPTIONS = (
IBeaconSensorEntityDescription(
key="rssi",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
entity_registry_enabled_default=False,
value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.rssi,
state_class=SensorStateClass.MEASUREMENT,
),
IBeaconSensorEntityDescription(
key="power",
translation_key="power",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
entity_registry_enabled_default=False,
value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.power,
state_class=SensorStateClass.MEASUREMENT,
),
IBeaconSensorEntityDescription(
key="estimated_distance",
translation_key="estimated_distance",
native_unit_of_measurement=UnitOfLength.METERS,
value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.distance,
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
),
IBeaconSensorEntityDescription(
key="vendor",
translation_key="vendor",
entity_registry_enabled_default=False,
value_fn=lambda ibeacon_advertisement: ibeacon_advertisement.vendor,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: IBeaconConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensors for iBeacon Tracker component."""
coordinator = entry.runtime_data
@callback
def _async_device_new(
unique_id: str,
identifier: str,
ibeacon_advertisement: iBeaconAdvertisement,
) -> None:
"""Signal a new device."""
async_add_entities(
IBeaconSensorEntity(
coordinator,
description,
identifier,
unique_id,
ibeacon_advertisement,
)
for description in SENSOR_DESCRIPTIONS
)
entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_IBEACON_DEVICE_NEW, _async_device_new)
)
class IBeaconSensorEntity(IBeaconEntity, SensorEntity):
"""An iBeacon sensor entity."""
entity_description: IBeaconSensorEntityDescription
def __init__(
self,
coordinator: IBeaconCoordinator,
description: IBeaconSensorEntityDescription,
identifier: str,
device_unique_id: str,
ibeacon_advertisement: iBeaconAdvertisement,
) -> None:
"""Initialize an iBeacon sensor entity."""
super().__init__(
coordinator, identifier, device_unique_id, ibeacon_advertisement
)
self._attr_unique_id = f"{device_unique_id}_{description.key}"
self.entity_description = description
@callback
def _async_seen(
self,
ibeacon_advertisement: iBeaconAdvertisement,
) -> None:
"""Update state."""
self._attr_available = True
self._ibeacon_advertisement = ibeacon_advertisement
self.async_write_ha_state()
@callback
def _async_unavailable(self) -> None:
"""Update state."""
self._attr_available = False
self.async_write_ha_state()
@property
def native_value(self) -> str | int | None:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self._ibeacon_advertisement)