-
-
Notifications
You must be signed in to change notification settings - Fork 28.6k
/
sensor.py
194 lines (159 loc) · 5.83 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Support for Skybeacon temperature/humidity Bluetooth LE sensors."""
from __future__ import annotations
import logging
import threading
from uuid import UUID
from pygatt import BLEAddressType
from pygatt.backends import Characteristic, GATTToolBackend
from pygatt.exceptions import BLEError, NotConnectedError, NotificationTimeout
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
)
from homeassistant.const import (
CONF_MAC,
CONF_NAME,
EVENT_HOMEASSISTANT_STOP,
PERCENTAGE,
STATE_UNKNOWN,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_LOGGER = logging.getLogger(__name__)
ATTR_DEVICE = "device"
ATTR_MODEL = "model"
BLE_TEMP_HANDLE = 0x24
BLE_TEMP_UUID = "0000ff92-0000-1000-8000-00805f9b34fb"
CONNECT_LOCK = threading.Lock()
CONNECT_TIMEOUT = 30
DEFAULT_NAME = "Skybeacon"
SKIP_HANDLE_LOOKUP = True
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Skybeacon sensor."""
name = config.get(CONF_NAME)
mac = config.get(CONF_MAC)
_LOGGER.debug("Setting up")
mon = Monitor(hass, mac, name)
add_entities([SkybeaconTemp(name, mon)])
add_entities([SkybeaconHumid(name, mon)])
def monitor_stop(_service_or_event):
"""Stop the monitor thread."""
_LOGGER.info("Stopping monitor for %s", name)
mon.terminate()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, monitor_stop)
mon.start()
class SkybeaconHumid(SensorEntity):
"""Representation of a Skybeacon humidity sensor."""
_attr_native_unit_of_measurement = PERCENTAGE
def __init__(self, name, mon):
"""Initialize a sensor."""
self.mon = mon
self._name = name
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the device."""
return self.mon.data["humid"]
@property
def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
return {ATTR_DEVICE: "SKYBEACON", ATTR_MODEL: 1}
class SkybeaconTemp(SensorEntity):
"""Representation of a Skybeacon temperature sensor."""
_attr_device_class = SensorDeviceClass.TEMPERATURE
_attr_native_unit_of_measurement = UnitOfTemperature.CELSIUS
def __init__(self, name, mon):
"""Initialize a sensor."""
self.mon = mon
self._name = name
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the device."""
return self.mon.data["temp"]
@property
def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
return {ATTR_DEVICE: "SKYBEACON", ATTR_MODEL: 1}
class Monitor(threading.Thread, SensorEntity):
"""Connection handling."""
def __init__(self, hass, mac, name):
"""Construct interface object."""
threading.Thread.__init__(self)
self.daemon = False
self.hass = hass
self.mac = mac
self.name = name
self.data = {"temp": STATE_UNKNOWN, "humid": STATE_UNKNOWN}
self.keep_going = True
self.event = threading.Event()
def run(self):
"""Thread that keeps connection alive."""
cached_char = Characteristic(BLE_TEMP_UUID, BLE_TEMP_HANDLE)
adapter = GATTToolBackend()
while True:
try:
_LOGGER.debug("Connecting to %s", self.name)
# We need concurrent connect, so lets not reset the device
adapter.start(reset_on_start=False)
# Seems only one connection can be initiated at a time
with CONNECT_LOCK:
device = adapter.connect(
self.mac, CONNECT_TIMEOUT, BLEAddressType.random
)
if SKIP_HANDLE_LOOKUP:
# HACK: inject handle mapping collected offline
# pylint: disable-next=protected-access
device._characteristics[UUID(BLE_TEMP_UUID)] = cached_char
# Magic: writing this makes device happy
device.char_write_handle(0x1B, bytearray([255]), False)
device.subscribe(BLE_TEMP_UUID, self._update)
_LOGGER.info("Subscribed to %s", self.name)
while self.keep_going:
# protect against stale connections, just read temperature
device.char_read(BLE_TEMP_UUID, timeout=CONNECT_TIMEOUT)
self.event.wait(60)
break
except (BLEError, NotConnectedError, NotificationTimeout) as ex:
_LOGGER.error("Exception: %s ", str(ex))
finally:
adapter.stop()
def _update(self, handle, value):
"""Notification callback from pygatt."""
_LOGGER.debug(
"%s: %15s temperature = %-2d.%-2d, humidity = %3d",
handle,
self.name,
value[0],
value[2],
value[1],
)
self.data["temp"] = float("%d.%d" % (value[0], value[2]))
self.data["humid"] = value[1]
def terminate(self):
"""Signal runner to stop and join thread."""
self.keep_going = False
self.event.set()
self.join()