-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhueBG.py
125 lines (101 loc) · 4.31 KB
/
hueBG.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
#Written by Eric Margadonna
#January 21, 2021
import phue
#Thank you to https://github.com/studioimaginaire for the philips hue control library
#DOCS: https://github.com/studioimaginaire/phue
import requests as r
import time as t
from config import *
class hueBG:
def __init__(self,key,ip_addr,target_bg,min_bg,max_bg,target_hue,out_hue,rainbow_on_target,refresh_rate):
self.key = key
self.min_bg = min_bg
self.max_bg = max_bg
self.target_bg = target_bg
self.target_hue = target_hue
self.out_hue = out_hue
#self.temp_mode = temp_mode
self.rainbow_on_target=rainbow_on_target
self.refresh_rate = refresh_rate
self.low_range = self.target_bg-self.min_bg
self.high_range = self.max_bg-self.target_bg
#This is the difference between the hues for high and low sugars
#25500 is the default for a green to red hue shift
self.hue_diff = abs(self.target_hue-self.out_hue)
self.low_scale = self.hue_diff/self.low_range
self.high_scale = self.hue_diff/self.high_range
#Connects to the Philips Hue Bridge, fetches the
#lights as a list, ensures that lights are on,
#clears any effects, then sets the brightness and
#saturation to max
self.bridge = phue.Bridge(ip_addr)
self.lights = self.bridge.lights
for l in self.lights:
l.on = True
l.effect = 'none'
l.bri = 254
l.sat = 254
def getBG(self, key):
#Using the sugarmate API to fetch the most recent BG reading
try:
url = 'https://sugarmate.io/api/v1/{0}/latest.json'.format(key)
return r.get(url).json().get('value')
#If for some reason there's an issue, just tell the user and
#try again.
except Exception:
print('Error in retrieving BG reading')
print(Exception)
print('Retrying')
return getBG()
def updateLights(self, BG):
#Sets the lights to rainbow mode if you hit your target BG
#and the effect is enabled in config.py
if BG == self.target_bg:
if self.rainbow_on_target == True:
for l in self.lights:
l.effect = 'colorloop'
else:
for l in self.lights:
l.effect = 'none'
l.transitiontime = 100
l.hue = self.target_hue
#If BG is out of the range defined in config.py,
#we skip any calculation and just set the bulbs to
#the OUT_HUE
if BG >= self.max_bg or BG <= self.min_bg:
for l in self.lights:
l.effect = 'none'
l.transitiontime = 100
l.hue = self.out_hue
#The lines below consist of the following steps:
#Check if BG is above the target or below the maximum and
#Check to see which hue has a greater numeric value, then
#calculate the hue to set the lights to and set them
if BG > self.target_bg and BG < self.max_bg:
if self.target_hue > self.out_hue:
calc_hue = self.target_hue - ((BG-self.target_bg)*self.high_scale)
if self.out_hue > self.target_hue:
calc_hue = self.target_hue + ((BG-self.target_bg)*self.high_scale)
for l in self.lights:
l.effect = 'none'
l.transitiontime = 100
l.hue = calc_hue
if BG < self.target_bg and BG > self.min_bg:
if self.target_hue > self.out_hue:
calc_hue = self.target_hue - ((self.target_bg-BG)*self.low_scale)
if self.out_hue > self.target_hue:
calc_hue = self.target_hue + ((self.target_bg-BG)*self.low_scale)
for l in self.lights:
l.effect = 'none'
l.transitiontime = 100
l.hue = calc_hue
#Rest, you've worked hard
t.sleep(self.refresh_rate)
def run(self):
while True:
self.updateLights(self.getBG(self.key))
def main():
app = hueBG(SUGARMATE_API_CODE,HUE_BRIDGE_IP,TARGET_BG,MIN_BG,MAX_BG,TARGET_HUE,OUT_HUE,RNBW_ON_TARGET,REFRESH_RATE)
app.run()
if __name__ == '__main__':
main()