This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeswitch.py
executable file
·310 lines (218 loc) · 8.35 KB
/
timeswitch.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python
import time
from datetime import date
from pprint import pprint
import signal
import sys
import yaml
import os
import logging
import schedule
import json
import ephem
import math
import paho.mqtt.publish as publish
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', # noqa
datefmt='%m-%d %H:%M')
logger = logging.getLogger(os.path.basename(__file__))
logger.debug('Starting...')
def shSigInt(signal, frame):
logger.info("CTRL+C detected, exiting.")
sys.exit(0)
class Socket(object):
"""Class to represent a socket, storing the on/off times and the name"""
def __init__(self, **socket):
self.name = socket.get('name')
self.onTimes = socket.get('on')
self.offTimes = socket.get('off')
logger.debug("new socket object for name=%s" % self.name)
class Config(object):
"""Class to represent the configuration file"""
def __init__(self):
self.sockets = []
self.filePath = None
self.mtime = None
self.file = None
self.yaml = None
logger.debug("Creating a new config object")
def load(self, path):
"""Load and parse the YAML config file"""
logger.debug("Config file path=%s" % path)
self.mtime = os.path.getmtime(path)
logger.debug("Last modification time of %s was %s" %
(path, time.ctime(self.mtime)))
self.filePath = path
self._parseYaml()
# Clear the existing list of sockets
self.sockets = []
logger.info("Found %d sockets in config" % len(self.yaml))
for socket in self.yaml:
self.sockets.append(Socket(**socket))
def updated(self):
new_mtime = os.path.getmtime(self.filePath)
if new_mtime > self.mtime:
logger.debug(
"Config file (%s) has been updated, reloading" % self.filePath)
self.mtime = new_mtime
self.load(self.filePath)
return True
else:
return False
def _loadConfig(self):
"""Private method to actually handle the loading of the file"""
logger.info("Loading config from %s" % self.filePath)
self.file = open(self.filePath, 'r')
def _parseYaml(self):
"""Private metod to parse the yaml"""
self._loadConfig()
logger.debug("Parsing YAML")
try:
self.yaml = yaml.load(self.file, Loader=yaml.BaseLoader)
except yaml.YAMLError as e:
logger.error("Invalid YAML")
def get_sockets(self):
return self.sockets
class MQTT(object):
""" Simple class to represent our connection to MQTT"""
def __init__(self):
"""Intentionally simple constructor"""
self.topic = None
self.options = {}
def config(self, topic, host, username=None, password=None):
"""Sets the configuration options for the MQTT connection,
authetication is optional"""
self.topic = topic
self.options = {'hostname': host}
if username is not None and password is not None:
logging.debug("connected to MQTT with authentication")
self.options['auth'] = {'username': username, 'password': password}
else:
logging.debug("connected to MQTT without authentication")
def send_message(self, payload):
"""Actually send the message defined in payload to MQTT"""
kwargs = self.options
kwargs['payload'] = payload
logger.debug("mqtt args=%s" % kwargs)
logger.debug("mqtt topic=%s" % self.topic)
publish.single(self.topic, **kwargs)
class sun(object):
"""Class to represent things like 'sunrise'"""
TIME_FORMAT = '%H:%M:%S'
def __init__(self):
self.radians = lambda d: d * math.pi / 180
self.local = lambda d: ephem.localtime(d)
self.sun = ephem.Sun()
self.obs = ephem.Observer()
logger.debug("Creating sun object")
pass
def config(self, coords):
self.obs.lat, self.obs.lon = map(self.radians, map(
float, coords.split(',')))
logger.debug("Setting coords to:%s" % coords)
def configured(self):
"""Returns true if this object has been configured"""
if self.coords is None:
return False
else:
return True
def _calculate(self, state, date, horizon='0'):
obs = self.obs
obs.date = date
obs.horizon = horizon
if state == 'rising':
value = self.local(obs.next_rising(self.sun)).strftime(
self.TIME_FORMAT)
elif state == 'setting':
value = self.local(obs.next_setting(self.sun)).strftime(
self.TIME_FORMAT)
else:
return False
return value
def sunrise(self, date=date.today()):
return self._calculate('rising', date)
def sunset(self, date=date.today()):
return self._calculate('setting', date)
def dusk(self, date=date.today()):
return self._calculate('setting', date, '-6')
def dawn(self, date=date.today()):
return self._calculate('rising', date, '-6')
def sendCommand(socket, action):
"""function to actually send a message off to MQTT"""
logger.info("turning socket=%s %s" % (socket, action))
payload = json.dumps({"switch": socket, "action": action, "source": "timeswitch"})
mqtt.send_message(payload)
def setSchedules(name, action, times):
"""For all the times defined in 'times' set a schedule"""
today = date.today()
for time in times:
if time == "dawn":
actual_time = sun_times.dawn(today)
elif time == "sunrise":
actual_time = sun_times.sunrise(today)
elif time == "sunset":
actual_time = sun_times.sunset(today)
elif time == "dusk":
actual_time = sun_times.dusk(today)
else:
actual_time = time
logger.info("Setting job %s %s at %s" % (name, action, actual_time))
# Slice the actual time here because schedule doesn't like seconds
schedule.every().day.at(actual_time[:5]).do(sendCommand, name, action)
def createSchedules(sockets):
"""Create all of the on and off schedules for all the sockets"""
# Clear the existing schedule, because in cases where a switch on or
# off time is defined as sunset or similar, the time will change daily
logger.debug("Clearing existing schedule")
schedule.clear()
logger.info("Creating schedule")
for socket in sockets:
setSchedules(socket.name, 'on', socket.onTimes)
setSchedules(socket.name, 'off', socket.offTimes)
def newDay(day):
"""Check to see if we are in a new day"""
newDay = date.today().day
if day != newDay:
logger.debug(
"Detected date change, old date=%d new date=%d" % (day, newDay))
day = newDay
logger.debug("Dawn: %s" % sun_times.dawn())
logger.debug("Sunrise: %s" % sun_times.sunrise())
logger.debug("Sunset: %s" % sun_times.sunset())
logger.debug("Dusk: %s" % sun_times.dusk())
logger.info("New day, clearing schedule")
createSchedules(config.sockets)
return day
if __name__ == '__main__':
# Nicer handling of CTRL-C
signal.signal(signal.SIGINT, shSigInt)
ts_config = os.getenv('CONFIG')
mqtt_host = os.getenv('MQTT_HOST')
mqtt_topic = os.getenv('MQTT_TOPIC')
""" Optional username and password for MQTT """
mqtt_user = os.getenv('MQTT_USERNAME', None)
mqtt_password = os.getenv('MQTT_PASSWORD', None)
# Optional coodinates, if you want to support 'sunrise' and the like
# in time schedules.
coords = os.getenv('COORDS', None)
sun_times = sun()
sun_times.config(coords)
# Create a MQTT object to represet out connection to the broker
mqtt = MQTT()
mqtt.config(mqtt_topic,
mqtt_host,
mqtt_user,
mqtt_password)
today = date.today()
lastDay = date.today().day
logger.debug("Detected todays date as %d" % lastDay)
config = Config()
config.load(ts_config)
createSchedules(config.get_sockets())
# Main loop
while True:
lastDay = newDay(lastDay)
if config.updated() is True:
createSchedules(config.get_sockets())
schedule.run_pending()
time.sleep(1)