-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.py
85 lines (68 loc) · 2.42 KB
/
Light.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
import milight
import re
PRIORITY = 10
WORDS = ["LIGHT", "LIGHTS", "ON", "OFF", "DIM", "WHITE", "FIRST", "SECOND", "THIRD", "FOURTH", "ALL"]
template = re.compile(r'.*\b(turn|all|first|second|third|fourth)\b.*\blights\b.*\b(on|off|white|dim)\b.*', re.IGNORECASE)
words_to_numbers = {
'turn': 0,
'all': 0,
'first': 1,
'second': 2,
'third': 3,
'forth': 4
}
def isValid(text):
return bool(template.search(text))
def message(text, command, group):
message = ''
if command == 'dim':
message += 'Dimming '
elif command == 'white':
message += 'Setting '
else:
message += 'Turning '
if group == 'all' or group == 'turn':
message += 'all lights '
else:
message += group + ' group '
if command != 'dim':
if command == 'white':
message += 'to white'
else:
message += command
return message
def handle(text, mic, profile):
ip = profile['milight']['ip']
port = profile['milight']['port'] if hasattr(profile['milight'], 'port') else 8899
wait_duration = profile['milight']['wait_duration'] if hasattr(profile['milight'], 'wait_duration') else 0.200
bulb_type = profile['milight']['bulb_type'] if hasattr(profile['milight'], 'bulb_type') else ['rgbw']
controller = milight.MiLight({'host': ip, 'port': port}, wait_duration=wait_duration)
light = milight.LightBulb(bulb_type)
m = template.search(text)
light_group_string = m.group(1)
command = m.group(2).lower()
if not light_group_string:
light_group_string = 'all'
light_group_string = light_group_string.lower()
light_group_int = words_to_numbers[light_group_string]
try:
if command == 'dim' or command == 'white':
if (command == 'white'):
controller.send(light.white(light_group_int))
controller.send(light.brightness(50 if command == 'dim' else 100, light_group_int))
else:
print('sending to controller ' + command + '(' + str(light_group_int) + ')')
lightCommand = getattr(light, command)
controller.send(lightCommand(light_group_int))
mic.say(message(text, command, light_group_string))
except Exception as e:
print(e)
mic.say('Fail to send command to lights')
# tests
#
# class Mic:
# def say(a, b):
# print(b)
#
#
# handle("all lights on", Mic(), {"milight": {"ip": "192.168.1.109"}})