-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiPowPlayBulbAPI.py
246 lines (206 loc) · 9.1 KB
/
MiPowPlayBulbAPI.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
"""
Python module to interface with MiPow PlayBulbs lamps
it requires "BluePy" to be installed. See https://github.com/IanHarvey/bluepy - install it from source
and depending on your python version and system you might need to make a symlink such as for example:
sudo ln -s /usr/local/lib/python3.5/dist-packages/bluepy /usr/lib/python3.5/
written by Logread based on work by Arttu Mahlakaarto and Matthew Garret,
with improved bluetooth characteristics detection
Also credits to Heckie75 (https://github.com/Heckie75/Mipow-Playbulb-BTL201)
This code is released under the terms of the MIT license. See the LICENSE
file for more details.
Version: 2018.11.24 (beta)
2018.12.04 fix the reconnect process if the device is not connected
"""
from bluepy import btle
from datetime import datetime, timedelta
import time
# if we are running as a plugin embedded in Domoticz then we need to overwrite python's logging module with Domoticz's
# logging methods
class Logger:
def __init__(self):
self.error = Domoticz.Error
self.debug = Domoticz.Debug
self.info = Domoticz.Status
self.debugging = Domoticz.Debugging
try:
import Domoticz
except:
import logging
domoticz = False
else:
logging = Logger()
domoticz = True
_MANUFACTURER = "Mipow Limited"
# below are types of PlayBulb that "should" work with this code... requires user feedback to confirm
# source of info is https://github.com/Heckie75/Mipow-Playbulb-BTL201
_SERIAL = (
"BTL300", # PlayBulb Candle - tested
"STRING MESH", # Playbulb String Mesh - tested by Github user DJFliX
"BTL200", # PlayBulb Rainbow - untested
"BTL201", # Playbulb Smart - untested
"BTL203", # Playbulb Spot Mesh - untested
"BTL301W", # Playbulb Sphere - untested
"BTL400", # Playbulb Garden - untested
"BTL501A", # Playbulb Comet - untested
"BTL505-GN", # Playbulb String - untested
"BTL601") # Playbulb Solar - untested
class Delegate(btle.DefaultDelegate):
"""Delegate Class."""
def __init__(self, bulb):
self.bulb = bulb
btle.DefaultDelegate.__init__(self)
class MiPowLamp:
def __init__(self, interface, mac, debug):
global domoticz
if domoticz:
logging.debugging = debug
logging.debug("this is a test of the debugging...")
self.timeout = 2 # seconds timeout for bluetooth connection
self.interface = interface
self.mac = mac
self.device = None
self.manufacturer = None
self.serial = None
self.name = None
self.battery = 255
self.power = False
self.handlebattery = None
self.handleWRGB = None
self.handleWRGBES = None
self.connected = False
self.white = 0
self.red = 0
self.green = 0
self.blue = 0
self.effect = 0
self.speed = 0
self.errmsg = ""
self.strict_check = True
def connect(self):
self.errmsg = ""
timeout_time = datetime.now() + timedelta(seconds=self.timeout)
while datetime.now() < timeout_time:
logging.debug("I am in the connect loop")
try:
self.device = btle.Peripheral(self.mac, addrType=btle.ADDR_TYPE_PUBLIC, iface=self.interface)
self.connected = True
characteristics = self.device.getCharacteristics()
for characteristic in characteristics:
hook = characteristic.uuid.getCommonName()
handle = characteristic.getHandle()
if hook == "Manufacturer Name String":
self.manufacturer = self.device.readCharacteristic(handle).decode('utf-8')
if hook == "Serial Number String":
self.serial = self.device.readCharacteristic(handle).decode('utf-8')
if hook == "Device Name":
self.name = self.device.readCharacteristic(handle).decode('utf-8')
elif hook == "Battery Level":
self.handlebattery = handle
elif hook == "fffc":
self.handleWRGB = handle
elif hook == "fffb":
self.handleWRGBES = handle
if self.manufacturer != _MANUFACTURER or (self.strict_check and not self.serial in _SERIAL):
logging.error("Device found is not supported: Manufacturer = '{}', Serial = '{}' !".format(
self.name, self.manufacturer, self.serial))
else:
logging.info("Connected to device: Name = '{}', Manufacturer = '{}', Serial = '{}'".format(
self.name, self.manufacturer, self.serial))
logging.info("Bluetooth Color Handle = {}".format(hex(self.handleWRGB)))
logging.info("Bluetooth Effects Handle = {}".format(hex(self.handleWRGBES)))
self.get_state()
return True
except btle.BTLEException as error:
self.connected = False
self.errmsg = "MiPowPlayBulbAPI connection error: {}".format(error)
logging.error(self.errmsg)
time.sleep(0.1) # sleep a little bit before trying to reconnect
return False
def disconnect(self):
logging.debug("Disconnecting device '{}'".format(self.name))
self.connected = False
self.device.disconnect()
def _send_packet(self, handleId, data):
if not self.connected:
self.connected = self.connect()
if self.connected:
self.errmsg = ""
try:
self.device.writeCharacteristic(handleId, data)
return True
except btle.BTLEException as error:
self.connected = False
self.errmsg = "MiPowPlayBulbAPI packet send error: {}".format(error)
logging.error(self.errmsg)
else:
self.errmsg = "MiPowPlayBulbAPI : device not connected, could not send packet"
return False
def off(self):
self.power = False
self.white = 0
self.red = 0
self.green = 0
self.blue = 0
packet = bytearray([0x00, 0x00, 0x00, 0x00])
return self._send_packet(self.handleWRGB, packet)
def set_white(self, level):
self.power = True
self.white = level
self.red = 0
self.green = 0
self.blue = 0
packet = bytearray([self.white, self.red, self.green, self.blue])
return self._send_packet(self.handleWRGB, packet)
def set_rgb(self, red, green, blue):
self.white = 0
self.red = red
self.green = green
self.blue = blue
packet = bytearray([self.white, self.red, self.green, self.blue])
return self._send_packet(self.handleWRGB, packet)
def set_rgbw(self, red, green, blue, white):
self.white = white
self.red = red
self.green = green
self.blue = blue
logging.info("function set_rgb: red={}, green={}, blue={}, white={}".format(red, green, blue, white))
packet = bytearray([self.white, self.red, self.green, self.blue])
return self._send_packet(self.handleWRGB, packet)
def set_effect(self, effect):
self.effect = effect
packet = bytearray([self.white, self.red, self.green, self.blue, self.effect, 0x00, self.speed, self.speed])
return self._send_packet(self.handleWRGBES, packet)
def set_speed(self, speed):
self.speed = speed
packet = bytearray([self.white, self.red, self.green, self.blue, self.effect, 0x00, self.speed, self.speed])
return self._send_packet(self.handleWRGBES, packet)
def get_state(self):
if not self.connected:
self.connected = self.connect()
if self.connected:
self.errmsg = ""
try:
status = self.device.readCharacteristic(self.handleWRGB)
if bytearray(status) != bytearray([0, 0, 0, 0]):
self.power = True
else:
self.power = False
self.white = status[0]
self.red = status[1]
self.green = status[2]
self.blue = status[3]
# get effect and speed
# note that handleWRGBES also provides actual real-time color data based on current effect and speed
status = self.device.readCharacteristic(self.handleWRGBES)
self.effect = status[4]
self.speed = status[6]
# let's update the battery level
self.battery = int.from_bytes(self.device.readCharacteristic(self.handlebattery), byteorder='big')
return True
except btle.BTLEException as error:
self.connected = False
self.errmsg = "MiPowPlayBulbAPI status read error: {}".format(error)
logging.error(self.errmsg)
else:
self.errmsg = "MiPowPlayBulbAPI : device not connected, could not read status"
return False