Skip to content

Commit

Permalink
Always on (#4)
Browse files Browse the repository at this point in the history
* switch to pywemo instead of ouimeaux

* add always on times
  • Loading branch information
KnicKnic authored Jan 19, 2021
1 parent c8e155d commit 0115694
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ I have a belkin Wemo insight smart plug, that I monitor the power usage of. Scen
* When the power usage goes higher than a specific amount I know that the light is turned on, I turn on another adapter on the other side of the room.
* When the light turns off, I turn off the other side.
* I let the other side of the room be on for up to 15 minutes, to facilitate prewarming the kotatsu.
* added ability to specify multiple always on times
* `--on-time=18:45-21:00 --on-time=23:40-2:00`

## Files explained
files | explanation
Expand Down
38 changes: 36 additions & 2 deletions light_check.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
#!/usr/bin/python
#!/usr/bin/env python3

import datetime
import os
import sys
import time
import pywemo

debug = os.getenv('debug') is not None
state_file = '/state/state.txt'

options = [arg.lstrip('-').split('=', 1) for arg in sys.argv[1:]]

def makeHourMinRange(x):
start,end = x.split('-')
start_hour,start_min = (int(x) for x in start.split(':'))
end_hour,end_min = (int(x) for x in end.split(':'))
return [[start_hour, start_min], [end_hour,end_min]]

on_times = [makeHourMinRange(opt[1]) for opt in options if opt[0] == 'on-time']

# fix up spanning across midnight
for i in range(len(on_times)):
if on_times[i][0] >= on_times[i][1]:
on_times.append([[0,0], on_times[i][1]])
on_times[i][1] = [24,0]

print("on times", on_times)

def isOnTime():
for x in on_times:
currentTimeFull = time.localtime()
currentTime = [currentTimeFull.tm_hour, currentTimeFull.tm_min]
if x[0] <= currentTime and x[1] >= currentTime:
return True
return False

def IsOn(wemo_switch):
return (wemo_switch.get_state(True) != 0)

Expand Down Expand Up @@ -56,11 +84,17 @@ def Log(*message):
#twentyMins = datetime.timedelta(seconds=20)

#loop for an hour
second_sleep =5
second_sleep = 5
for x in range(0, 3600, second_sleep):
EnsureOn(lightDetector)

time.sleep(second_sleep)

if isOnTime():
Log("ensuring on due to ontime")
EnsureOn(heater)
continue

twentyMinsFuture = datetime.datetime.now() + twentyMins

drawing_power = DrawingPower(lightDetector)
Expand Down

0 comments on commit 0115694

Please sign in to comment.