-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path_P513_BLEMijiaClock.py
184 lines (176 loc) · 6.73 KB
/
_P513_BLEMijiaClock.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
#!/usr/bin/env python3
#############################################################################
##################### BLE LYWSD02 plugin for RPIEasy ########################
#############################################################################
#
# Xiaomi Mijia LYWSD02 Bluetooth Clock+Temperature/Humidity Sensor plugin.
# Can be used when BLE compatible Bluetooth dongle, and BluePy is installed.
#
# Based on:
# https://github.com/h4/lywsd02
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import time
from lywsd02 import Lywsd02Client
from datetime import datetime
import lib.lib_blehelper as BLEHelper
class Plugin(plugin.PluginProto):
PLUGIN_ID = 513
PLUGIN_NAME = "Environment - BLE Xiaomi LYWSD02 Clock&Hygrometer (EXPERIMENTAL)"
PLUGIN_VALUENAME1 = "Temperature"
PLUGIN_VALUENAME2 = "Humidity"
PLUGIN_VALUENAME3 = "Battery"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_BLE
self.vtype = rpieGlobals.SENSOR_TYPE_TEMP_HUM
self.valuecount = 2
self.senddataoption = True
self.recdataoption = False
self.timeroption = True
self.timeroptional = True
self.connected = False
self.formulaoption = True
self.BLEPeripheral = False
# self.conninprogress = False
self.readinprogress = False
self.battery = 0
self.lastbatteryreq = 0
self._lastdataservetime = 0
self._nextdataservetime = 0
self.blestatus = None
self._lastclockupdate = 0
def webform_load(self): # create html page for settings
webserver.addFormTextBox("Device Address","plugin_513_addr",str(self.taskdevicepluginconfig[0]),20)
webserver.addFormNote("Enable blueetooth then <a href='blescanner'>scan LYWSD02 address</a> first.")
webserver.addFormNote("This plugin may not work well with ble scanner plugin.")
webserver.addFormCheckBox("Add Battery value for non-Domoticz system","plugin_513_bat",self.taskdevicepluginconfig[1])
webserver.addFormCheckBox("Set LYWSD time at startup","plugin_513_t",self.taskdevicepluginconfig[2])
webserver.addFormNumericBox("Force to release BLE queue if blocked for x seconds","plugin_513_free",self.taskdevicepluginconfig[3],5,240)
webserver.addUnit('s')
return True
def webform_save(self,params): # process settings post reply
self.taskdevicepluginconfig[0] = str(webserver.arg("plugin_513_addr",params)).strip().lower()
self.taskdevicepluginconfig[1] = (webserver.arg("plugin_513_bat",params)=="on")
self.taskdevicepluginconfig[2] = (webserver.arg("plugin_513_t",params)=="on")
try:
self.taskdevicepluginconfig[3] = int(webserver.arg("plugin_513_free",params))
except:
self.taskdevicepluginconfig[3] = 0
if self.taskdevicepluginconfig[1]:
self.valuecount = 3
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
else:
self.valuecount = 2
self.vtype = rpieGlobals.SENSOR_TYPE_TEMP_HUM
try:
if self._lastclockupdate:
pass
except:
self._lastclockupdate = 0
self.plugin_init()
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.timer1s = False
self.readinprogress = 0
# self.uservar[0] = 0
# self.uservar[1] = 0
self._lastclockupdate = 0
if self.taskdevicepluginconfig[3]<5:
self.taskdevicepluginconfig[3]=240
self.connected = False
try:
self.blestatus = BLEHelper.BLEStatus[0] # 0 is hardwired in LYWSD02 library
except:
pass
if self.enabled:
self.conninprogress=False
self.timer1s = True
else:
self.ports = ""
self.initialized = False
self.timer1s = False
self.conninprogress=False
def timer_once_per_second(self):
if self.enabled and self.connected==False and self.conninprogress==False:
self.conninprogress=True
self.connect()
if self.connected:
self.initialized = True
self.ports = str(self.taskdevicepluginconfig[0])
self._lastdataservetime = rpieTime.millis() - ((self.interval-1)*1000)
self.timer1s = False
self.conninprogress=False
if self.enabled and self.connected:
if self.taskdevicepluginconfig[2]:
try:
if (time.time() - self._lastclockupdate)>604800:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"Sync LWSD02 time")
try:
from pytz import reference
localtime = reference.LocalTimezone()
today = datetime.now(localtime)
self.BLEPeripheral.tz_offset = localtime.utcoffset(today).seconds//3600 #set timezone
except Exception as e:
today = datetime.now()
self.BLEPeripheral.time = today
self._lastclockupdate = time.time()
except Exception as e:
self._lastclockupdate = 0
def connect(self):
try:
if self.blestatus.isscaninprogress():
self.blestatus.requeststopscan(self.taskindex)
return False
except Exception as e:
return False
cstart = time.time()
while self.blestatus.norequesters()==False or self.blestatus.nodataflows()==False:
time.sleep(0.5)
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG_MORE,"BLE line not free for P513! "+str(self.blestatus.dataflow))
try:
if time.time()-cstart>int(self.taskdevicepluginconfig[3]):
self.blestatus.dataflow = [] #reset line by force
except:
pass
self.blestatus.registerdataprogress(self.taskindex)
try:
self.BLEPeripheral = Lywsd02Client(str(self.taskdevicepluginconfig[0]),int(self.interval))
self.connected = True
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE connected "+str(self.taskdevicepluginconfig[0]))
time.sleep(1)
except Exception as e:
self.connected = False
self.initialized = False
self.blestatus.unregisterdataprogress(self.taskindex)
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BLE connection failed "+str(self.taskdevicepluginconfig[0]))
def plugin_read(self):
result = False
if self.enabled and self.initialized and self.readinprogress==0:
self.readinprogress = 1
try:
self.battery = int(self.BLEPeripheral.battery)
self.set_value(1,float(self.BLEPeripheral.temperature),False)
if self.taskdevicepluginconfig[1]:
self.set_value(2,float(self.BLEPeripheral.humidity),False)
self.set_value(3,self.battery,False,susebattery=self.battery)
else:
self.set_value(2,float(self.BLEPeripheral.humidity),False,susebattery=self.battery)
self.blestatus.unregisterdataprogress(self.taskindex)
self.plugin_senddata(pusebattery=self.battery)
self._lastdataservetime = rpieTime.millis()
result = True
except Exception as e:
self.blestatus.unregisterdataprogress(self.taskindex)
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE read error: "+str(e))
time.sleep(3)
self.connect()
self.readinprogress = 0
return result