-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphilips.py
96 lines (80 loc) · 2.78 KB
/
philips.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
import mirobo
import paho.mqtt.client as mqtt
print("Skrypt sterowania Philips Xiaomi LED Bulb")
# Inicjacja zarowki z biblioteki mirobo
led = mirobo.Ceil()
# Set MQTT Topic
setOnMQTT = "/home/setOn"
statusOnMQTT = "/home/statusOn"
setBrightnessMQTT = "/home/setB"
statusBrightness = "/home/statusB"
setColorTemperatureMQTT = "/home/setColor"
statusColorTemperature = "/home/statusColorTemperature"
# Metody Paho-MQTT
# Gdy polaczony
def on_connect(client, userdata, flasgs, rc):
print("Connect with result code: " + str(rc))
client.subscribe(setOnMQTT)
client.subscribe(setBrightnessMQTT)
client.subscribe(setColorTemperatureMQTT)
# Gdy odbiera wiadomosc
def on_message(client, userdata, message):
if message.topic == setOnMQTT:
on = bool(int(message.payload))
if on == True:
setPowerOn(True)
else:
setPowerOn(False)
if message.topic == setBrightnessMQTT:
setBrightness(int(message.payload))
if message.topic == setColorTemperatureMQTT:
print(int(message.payload))
setColorTemperature(int(message.payload))
# -------------------------
# Metody sterowania zarowka
def setPowerOn(on):
if on == True:
print("<INFO> Philips Xiaomi LED Bulb ON: True")
try:
led.on()
except mirobo.device.DeviceException:
print("<ERROR> Nie odnaleziono zarowki")
client.publish(statusOnMQTT, 0)
else:
client.publish(statusOnMQTT, 1)
else:
print("<INFO> Philips Xiaomi LED Bulb ON: False")
try:
led.off()
except mirobo.device.DeviceException:
print("<ERROR> Nie odnaleziono zarowki")
client.publish(statusOnMQTT, 1)
else:
client.publish(statusOnMQTT, 0)
def setBrightness(value):
print("<INFO> Philips Xiaomi LED Bulb Brightness: " + str(value))
try:
led.set_bright(value)
except mirobo.device.DeviceException:
print("<ERROR> Nie odnaleziono zarowki")
client.publish(statusOnMQTT, 0)
else:
client.publish(statusBrightness, value)
def setColorTemperature(value):
print("<INFO> Philips Xiaomi LED CCT: " + str(value))
try:
led.set_cct(calculateColorTemperature(value, 140, 500, 100, 1))
except mirobo.device.DeviceException:
print("<ERROR> Nie odnaleziono zarowki")
client.publish(statusOnMQTT, 0)
else:
client.publish(statusColorTemperature, value)
# Metoda obliczania map
def calculateColorTemperature(x, in_min, in_max, out_min, out_max):
result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
return result
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("homekit.local", 1883, 60)
client.loop_forever()