-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlights.py
More file actions
180 lines (145 loc) · 6.8 KB
/
lights.py
File metadata and controls
180 lines (145 loc) · 6.8 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import appdaemon.appapi as appapi
import datetime
class MotionLights(appapi.AppDaemon):
"""
This class keeps track of different lights managed by a motion sensor.
the luminosity in the room must be lower of the luminosity_min value passed in the
configuration that is reported by the luminosity sensor list.
A disabler binary sensor can be used for disabling the usage of the module.
For example if kodi starts playing I don't want that some movement in the room turns on the lights.
"""
def initialize(self):
print("MotionLights initialize")
self._lights = self.args.get("lights", [])
self._motion = self.args.get("motion", None)
self._luminosity = self.args.get("luminosity", [])
self._disabler = self.args.get("disabler", None)
self._timeout = None
self._luma_val = []
self._luminosity_min = self.args.get("luminosity_min", 20)
self.log("Got controlled lights {}".format(self._lights))
self.log("Got controller motion sensor {}".format(self._motion))
self.log("Got luminosity sensor {}".format(self._luminosity))
self.log("Got disabler sensor {}".format(self._disabler))
self.listen_state(self.motion, entity=self._motion, new="on")
self.listen_state(self.demotion, entity=self._motion, new="off")
i = 0
for item in self._luminosity:
self.listen_state(self.luminosity, item, index=i)
i += 1
self._luma_val.append(0)
time = datetime.time(6, 0, 0)
self.run_daily(self.reset_status, time)
def motion(self, entity, attribute, old, new, kwargs):
self.log("Motion detected")
luma = 0
for val in self._luma_val:
if int(val) > int(luma):
luma = val
if self.get_state(self._disabler) == 'on':
self.log("Controlling lights is disabled")
return
if luma > self._luminosity_min:
self.log("room is too bright for lights. luma %d" % (luma))
# if lights was already on... don't let the timer expire but reload it instead.
self.retrigger_timer()
return
self.log("luma %d is ok for controlling lights." % (luma))
self.turn_on_lights()
def delete_timer(self):
if self._timeout is not None:
self.cancel_timer(self._timeout)
def retrigger_timer(self):
self.delete_timer()
self._timeout = self.run_in(self.light_off, 300)
def turn_on_lights(self):
for light in self._lights:
self.turn_on(light)
self.delete_timer()
def demotion(self, entity, attribute, old, new, kwargs):
if self.get_state(self._disabler) == 'on':
self.log("Controlling lights is disabled")
return
self.log("Motion switched off, starting timer")
self._timeout = self.run_in(self.light_off, 300)
def light_off(self, kwargs):
if self.get_state(self._disabler) == 'on':
self.log("Controlling lights is disabled")
return
if self.get_state(self._motion) == 'on':
return self.retrigger_timer()
self.log("Timer ended.")
self.cancel_timer(self._timeout)
self._timeout = None
for light in self._lights:
self.turn_off(light)
def luminosity(self, entity, attribute, old, new, kwargs):
self.log("Got luminosity for " + entity + " " + new)
self._luma_val[int(kwargs["index"])] = float(new)
def reset_status(self, kwargs):
self.log("Resetting the disabler")
self.turn_off(self._disabler)
class NightLight(MotionLights):
def initialize(self):
print("NightLight initialize")
super(NightLight, self).initialize()
self._brightlight_start = datetime.datetime.strptime(self.args.get("brightlight_start", "0:0:0"),
"%H:%M:%S").time()
self._brightlight_end = datetime.datetime.strptime(self.args.get("brightlight_end", "0:0:0"), "%H:%M:%S").time()
self._bright_value = self.args.get("bright_value", 0)
self._lowbright_value = self.args.get("lowbright_value", 0)
self._rgbcolor_value = self.args.get("rgbcolor_value", 0)
self._lowrgbcolor_value = self.args.get("lowrgbcolor_value", 0)
self.log("Got starting time {}".format(self._brightlight_start))
self.log("Got ending time {}".format(self._brightlight_end))
self.log("Got bright_value {}".format(self._bright_value))
self.log("Got lowbright_value {}".format(self._lowbright_value))
self.log("Got rgbcolor_value {}".format(self._rgbcolor_value))
self.log("Got lowrgbcolor_value {}".format(self._lowrgbcolor_value))
def turn_on_lights(self, brightness=None):
self.log("turn on lights")
now = self.time()
if self._brightlight_end > now > self._brightlight_start:
brightness = self._bright_value
color = self._rgbcolor_value
else:
brightness = self._lowbright_value
color = self._lowrgbcolor_value
for light in self._lights:
self.log("turning on light {} with {} {}".format(light, brightness, color))
self.turn_on(light, color_name=color, brightness=brightness)
if self._timeout is not None:
self.cancel_timer(self._timeout)
class FluxLight(MotionLights):
""" Flux and Motion lights
This class uses the MotionLights base class including also the fluxer service
defined in home assistant. The fluxer service call after the turn_on_lights is used
for updating the color of the lights.
"""
def initialize(self):
print("FluxLight initialize")
super(FluxLight, self).initialize()
self._fluxer_service = self.args.get("fluxer", [])
self.log("Got fluxer {}".format(self._fluxer_service))
def turn_on_lights(self):
self.log("FluxLight turn on lights")
super(FluxLight, self).turn_on_lights()
if self._fluxer_service is not None:
for item in self._fluxer_service:
self.call_service(item)
class BedroomLight(FluxLight):
pass
class KodiFluxedLight(FluxLight):
def initialize(self):
super(KodiFluxedLight, self).initialize()
self._kodi = self.args.get("kodi", None)
self.log("Got kodi {}".format(self._kodi))
self.listen_state(self.kodi_playing, entity=self._kodi, new="playing")
self.listen_state(self.kodi_idling, entity=self._kodi, new="paused")
def kodi_playing(self, entity, attribute, old, new, kwargs):
for light in self._lights:
self.turn_off(light)
self.turn_on(self._disabler)
def kodi_idling(self, entity, attribute, old, new, kwargs):
self.turn_off(self._disabler)
self.turn_on_lights()