forked from mike0xt/home-assistant_inkbird
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.py
413 lines (352 loc) · 13.9 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import asyncio
from collections import OrderedDict
from datetime import timedelta, datetime
import logging
from random import randint
import re
from struct import unpack
import signal
import time
# added os to kill bluepy-helper
import os
pid=os.getpid()
# added list for MAC addresses
devicemacs = []
##
from bluepy import btle
from bluepy.btle import BTLEException, Scanner, DefaultDelegate
from homeassistant.components.sensor import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC,
DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_BATTERY
)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import voluptuous as vol
DOMAIN = 'Inkbird'
DEFAULT_NAME = 'Inkbird'
_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=60)
# Sensor types are defined like: Name, units
SENSOR_TYPES = {
'updater': [None, 'Updater', None],
'temperature': [DEVICE_CLASS_TEMPERATURE, 'Temperature', '°C'],
'humidity': [DEVICE_CLASS_HUMIDITY, 'Humidity', '%'],
'battery': [DEVICE_CLASS_BATTERY, 'Battery', '%'],
}
STATE_ATTR_TEMPERATURE = "temperature"
STATE_ATTR_HUMIDITY = "humidity"
STATE_ATTR_BATTERY = "battery"
ENTITY_ITEM = {
vol.Required(CONF_MAC): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required('devices'):
vol.All(cv.ensure_list, [OrderedDict(ENTITY_ITEM)])
})
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Inkbird thermostat."""
_LOGGER.info(">>> The inkbird component is ready!")
inkbird_devices = []
for device in config['devices']:
for parameter in device['monitored_conditions']:
name = SENSOR_TYPES[parameter][1]
uom = SENSOR_TYPES[parameter][2]
prefix = device['name']
if prefix:
name = "{} {}".format(prefix, name)
entity_name = re.sub(' ', '_', name.lower())
if parameter == "temperature":
inkbird_devices.append( InkbirdThermalSensor(device['mac'].lower(), uom, name, entity_name) )
elif parameter == "humidity":
inkbird_devices.append( InkbirdHumiditySensor(device['mac'].lower(), uom, name, entity_name) )
else:
inkbird_devices.append( InkbirdBatterySensor(device['mac'].lower(), uom, name, entity_name) )
# create list for MAC addresses
devicemacs.append( device['mac'].lower() )
_LOGGER.debug(f"Device MAC list is {devicemacs}")
inkbird_devices.append( InkbirdUpdater(hass, inkbird_devices) )
add_entities(inkbird_devices, True)
class InkbirdUpdater(Entity):
entity_id = "inkbird.updater"
def __init__(self, hass, inkbird_devices):
"""Initialize the thermometer."""
Entity.__init__(self)
self._name = 'Inkbird Updater'
self._state = None
self._mac = None
self.hass = hass
self.scanner = Scanner()
self.scanner.clear()
self.scanner.start()
self.inkbird_devices = inkbird_devices
@property
def mac(self):
"""Return the mac of the sensor."""
return self._mac
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def should_poll(self):
"""Return the name of the sensor."""
_LOGGER.debug("Should_Poll called")
return True
def update(self):
# added
global pid
##
"""Get the latest data and use it to update our sensor state."""
_LOGGER.debug("UPDATE called")
_LOGGER.debug(f"scanner here is {self.scanner}")
try:
self.scanner.process(timeout=8.0)
except:
e = sys.exc_info()[0]
_LOGGER.error(f" Exception occoured during scanning: {e}")
results = self.scanner.getDevices()
_LOGGER.debug(f"got results {results}")
# The btle on my raspberry pi 4 seems to go MIA
# if we have no results at all, the scanner may have gone MIA.
# it happens apparently. So, let's count upto 5 and then, if it
# still happens, restart/refresh the btle stack.
# Seems to go MIA more frequently on RPi3B: removed counter and restart immediately if no results obtained
# May generate 10 s update timeout errors
if not any(results):
_LOGGER.error("Btle went away .. restarting entire btle stack")
## Kill the bluepy-helper process
# There is a memory leak: new instance of bluepy-helper is created with Scanner(), without terminating running instance
# https://github.com/IanHarvey/bluepy/issues/267#issuecomment-657183840
# adapted from: https://github.com/JsBergbau/MiTemperature2/blob/master/LYWSD03MMC.py
del self.scanner #probably not needed?
bluepypid=0
pstree=os.popen("pstree -p " + str(pid)).read() #we want to kill only bluepy from our own process tree, because other python scripts have there own bluepy-helper process
_LOGGER.debug("PSTree: " + pstree)
try:
bluepypid=re.findall(r'bluepy-helper\((.*)\)',pstree)[0] #Store the bluepypid, to kill it later
except IndexError: #Should not happen since we're now connected
_LOGGER.debug("Couldn't find pid of bluepy-helper")
if bluepypid is not 0:
os.system("kill " + bluepypid)
_LOGGER.debug("Killed bluepy with pid: " + str(bluepypid))
# Kill bluepy-helper systemwide anyways...not good if there are other scripts using bluepy
# else:
# os.system('pkill bluepy-helper')
# _LOGGER.debug("Killed bluepy-helper")
##
self.scanner = Scanner()
self.scanner.clear()
self.scanner.start()
try:
self.scanner.process(timeout=8.0)
except:
e = sys.exc_info()[0]
_LOGGER.error(f" Exception occoured during scanning: {e}")
results = self.scanner.getDevices()
_LOGGER.debug(f"Got new results {results}")
for dev in results:
if dev.addr in devicemacs:
self.handleDiscovery(dev)
self.scanner.clear()
self._state = []
return True
def handleDiscovery(self, dev):
# _LOGGER.debug(f"Discovered device {dev.addr}")
_LOGGER.debug("Discovered device {} ({}), RSSI={} dB".format(dev.addr, dev.addrType, dev.rssi))
for (adtype, desc, value) in dev.getScanData():
_LOGGER.debug("[%s] %s = %s" % (adtype, desc, value))
if adtype == 255:
_LOGGER.debug(f"{dev.addr} is in devicemacs list and now gets parameters!")
humidity = "%2.2f" % (int(value[6:8]+value[4:6], 16)/100)
#temperature = "%2.2f" % (int(value[2:4]+value[:2], 16)/100)
temperature = int(value[2:4]+value[:2], 16)
temperature_bits = 16
if temperature & (1 << (temperature_bits-1)):
temperature -= 1 << temperature_bits
temperature = "%2.2f" % (temperature / 100)
battery = int(value[14:16], 16)
_LOGGER.debug(self.inkbird_devices)
for device in self.inkbird_devices:
_LOGGER.debug(f" dev addr is {dev.addr} and mac is {device.mac}")
# _LOGGER.debug(f" --> {temperature} - {humidity} - {battery} ")
if dev.addr == device.mac:
_LOGGER.debug(f" dev addr is {dev.addr} and mac is {device.mac} with parameter of {device.parameter}")
# What does this do? Removed
# old_state = self.hass.states.get(f"sensor.{device.entity_name}")
# if old_state:
# attrs = old_state.attributes
# else:
# attrs = None
if device.parameter == "temperature":
_LOGGER.debug(f" >>>> updating device {device.mac} with {temperature}")
device.temperature = temperature
device._state = temperature
#self.hass.states.set(f"sensor.{device.entity_name}", temperature, attrs)
elif device.parameter == "humidity":
_LOGGER.debug(f" >>>> updating device {device.mac} with {humidity}")
device.humidity = humidity
device._state = humidity
#self.hass.states.set(f"sensor.{device.entity_name}", humidity, attrs)
else:
_LOGGER.debug(f" >>>> updating device {device.mac} with {battery}")
device.battery = battery
device._state = battery
#self.hass.states.set(f"sensor.{device.entity_name}", battery, attrs)
_LOGGER.debug(f" Done with handleDiscovery")
class InkbirdThermalSensor(Entity):
"""Representation of a Inkbird Sensor."""
def __init__(self, mac, uom, name, entity_name):
"""Initialize the sensor."""
Entity.__init__(self)
self._device_class = DEVICE_CLASS_TEMPERATURE
self._mac = mac
self._unit_of_measurement = uom
self._name = name
self._entity_name = entity_name
self._unique_id = 't_' + self._mac
self.parameter = "temperature"
self._state = None
self.temperature = None
@property
def mac(self):
"""Return the mac of the sensor."""
return self._mac
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def entity_name(self):
"""Return the entity name of the sensor."""
return self._entity_name
@property
def unique_id(self):
"""Return the unique_id of the sensor."""
return self._unique_id
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return self._unit_of_measurement
@property
def temperature_unit(self):
"""Return the unit of measurement which this thermostat uses."""
return TEMP_CELSIUS
@property
def state_attributes(self):
"""Return the state attributes of the sun."""
return {
STATE_ATTR_TEMPERATURE: self.temperature
}
class InkbirdHumiditySensor(Entity):
"""Representation of a Inkbird Sensor."""
def __init__(self, mac, uom, name, entity_name):
"""Initialize the sensor."""
Entity.__init__(self)
self._device_class = DEVICE_CLASS_HUMIDITY
self._mac = mac
self._unit_of_measurement = uom
self._name = name
self._entity_name = entity_name
self._unique_id = 'h_' + self._mac
self.parameter = "humidity"
self._state = None
self.humidity = None
@property
def mac(self):
"""Return the mac of the sensor."""
return self._mac
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def entity_name(self):
"""Return the entity name of the sensor."""
return self._entity_name
@property
def unique_id(self):
"""Return the unique_id of the sensor."""
return self._unique_id
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return self._unit_of_measurement
@property
def state_attributes(self):
"""Return the state attributes of the sun."""
return {
STATE_ATTR_HUMIDITY: self.humidity
}
class InkbirdBatterySensor(Entity):
"""Representation of a Inkbird Sensor."""
def __init__(self, mac, uom, name, entity_name):
"""Initialize the sensor."""
Entity.__init__(self)
self._device_class = DEVICE_CLASS_BATTERY
self._mac = mac
self._unit_of_measurement = uom
self._name = name
self._entity_name = entity_name
self._unique_id = 'b_' + self._mac
self.parameter = "battery"
self._state = None
self.battery = None
@property
def mac(self):
"""Return the mac of the sensor."""
return self._mac
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def entity_name(self):
"""Return the entity name of the sensor."""
return self._entity_name
@property
def unique_id(self):
"""Return the unique_id of the sensor."""
return self._unique_id
@property
def device_class(self):
"""Return the device class."""
return self._device_class
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return self._unit_of_measurement
@property
def state_attributes(self):
"""Return the state attributes of the sun."""
return {
STATE_ATTR_BATTERY: self.battery
}