-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigbeeMqttLight.py
91 lines (75 loc) · 2.46 KB
/
zigbeeMqttLight.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
from zigbeeMqtt import zigbeeMqtt
class zigbeeMqttLight(zigbeeMqtt):
def __init__(self, config=..., name=""):
super().__init__(config, name)
self.name = name
if 'colorTempEnabled' in config:
self.colorTempEnabled = config['colorTempEnabled']
else:
self.colorTempEnabled = False
self.state = False
self.brightness = 254
self.colorTemp = 290
def set(self, key, value):
print("setting in light", key, value)
if key == 'toggle':
self.state = not self.state
if self.state:
self.turnOn()
else:
self.turnOff()
elif key == 'state':
self.state = not (value == 'ON') # not here because we're toggling anyway
self.toggle()
elif key == 'brightness':
if value == '0':
self.turnOff()
else:
self.brightness = int(value)
self.turnOn()
else:
super().set(key, value)
if (key == 'color_temp'): self.colorTemp = int(value)
def turnOn(self):
self.state = True
super().set('brightness', self.brightness)
if (self.colorTempEnabled): super().set('color_temp', self.colorTemp)
def turnOff(self):
self.state = False
super().set('state', 'OFF')
def increment(self, val):
val = int(val)
print(val)
if val > 0:
if not self.state:
self.set('brightness', val)
elif self.brightness <= 255 - val:
self.set('brightness', self.brightness+val)
else:
if self.brightness >= val*-1:
self.set('brightness', self.brightness+val)
else:
self.brightness = 200
self.turnOff()
def okay(self):
self.queue.put({ 'f':self.set, 'a':['effect', 'okay'] })
def toggle(self, brightness = None, colorTemp = None, reset = False, resetBrightnessOnToggle = False):
print('toggle', self.name)
if brightness is not None:
self.brightness = brightness
self.state = False # treat as if its off -> switch on in any case
if colorTemp is not None:
self.colorTemp = colorTemp
self.state = False # treat as if its off -> switch on in any case
if reset:
self.brightness = 254
self.colorTemp = 310
self.state = False
if resetBrightnessOnToggle:
self.brightness = 254 if self.state == False else 0
self.set('toggle', '')
def setHomebridge(self, input):
with self.queue.mutex:
self.queue.queue.clear()
self.queue.put({'f':self.set, 'a': ['brightness', int(int(input) * 2.55)]})
print(input)