-
Notifications
You must be signed in to change notification settings - Fork 1
/
lights.py
83 lines (72 loc) · 2.23 KB
/
lights.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
import asyncio
import logging
import datetime
import csv
from phue import Bridge
import pychromecast
import settings
logger = logging.getLogger(__name__)
VIDEO_FILE = 'https://osf.io/zqnyu/?action=download&direct&mode=render'
HUE_COLORS = dict(
goldenrod=[0.5136, 0.4444],
green=[0.214, 0.709],
lightblue=[0.2621, 0.3157],
darkorchid=[0.296, 0.1409],
silver=[0.3227, 0.329],
mediumpurple=[0.263, 0.1773],
forestgreen=[0.2097, 0.6732],
slategray=[0.2762, 0.3009],
mediumslateblue=[0.2179, 0.1424],
mediumvioletred=[0.504, 0.2201],
palevioletred=[0.4658, 0.2773],
deepskyblue=[0.1576, 0.2368],
cornflowerblue=[0.1905, 0.1945],
rosybrown=[0.4026, 0.3227],
darksalmon=[0.4837, 0.3479],
yellowgreen=[0.3517, 0.5618],
steelblue=[0.183, 0.2325],
orangered=[0.6726, 0.3217],
mediumorchid=[0.3365, 0.1735],
slateblue=[0.2218, 0.1444],
beige=[0.3402, 0.356]
)
# CSV containing timing for color changes
CUE_FILE = 'timecolor.csv'
def _create_hue_cues():
with open(CUE_FILE) as f:
reader = csv.reader(f)
next(reader) # Skip header
data = [(int(time), color.strip()) for time, color in reader]
return data
# Maps timestamps (seconds) => color names (strings)
HUE_CUES = _create_hue_cues()
# Delay in seconds to correct for Chromecast connection time
DELAY = 3
def start_video():
logger.debug('Starting video')
cast = pychromecast.get_chromecast(friendly_name=settings.CHROMECAST_NAME)
cast.wait()
mc = cast.media_controller
mc.play_media(VIDEO_FILE, 'video/mp4')
def setup_hue():
hue = settings.BRIDGE_IP
return Bridge(hue)
def change_light(bridge, color):
lights = bridge.get_light_objects(mode='id').values()
logger.info('Changing color: {}'.format(color))
for light in lights:
light.xy = HUE_COLORS[color]
def main():
loop = asyncio.get_event_loop()
start_video()
bridge = setup_hue()
for time, color in HUE_CUES:
loop.call_later(time + DELAY, change_light, bridge, color)
loop.run_forever()
loop.close()
if __name__ == "__main__":
logging.basicConfig(
format='[%(name)s] %(levelname)s: %(message)s',
level=getattr(logging, settings.LOG_LEVEL)
)
main()