Skip to content

Commit

Permalink
switch to pywemo instead of ouimeaux (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic authored Jan 11, 2021
1 parent 6dbe56e commit c8e155d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 318 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"python.pythonPath": "C:\\Python39\\python.exe",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.linting.enabled": true
}
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# See https://docs.docker.com/develop/develop-images/multistage-build/

# Creating a python base with shared environment variables
# FROM python:slim@sha256:4d92968b26bb6b7b62d957244de86fc1054f03793577d49e85c00864eb03ca07 as python-base
FROM python:3.6-slim as python-base
FROM python:slim@sha256:4d92968b26bb6b7b62d957244de86fc1054f03793577d49e85c00864eb03ca07 as python-base
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
Expand Down
109 changes: 61 additions & 48 deletions light_check.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,90 @@
#!/usr/bin/python
import datetime
from ouimeaux.environment import Environment
env = Environment()
env.start()
env.discover(seconds=10)
import os
import time
import pywemo

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

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

def Refresh(wemo_object):
_ = wemo_object.get_state(True)
return wemo_object

def DrawingPower(wemo_switch):
return (wemo_switch.current_power > 6000)
return (Refresh(wemo_switch).current_power > 6000)

def EnsureOn(wemo_switch):
if not IsOn(wemo_switch):
wemo_switch.on()
if not IsOn(wemo_switch):
wemo_switch.on()

def GetOldState():
try:
return open(state_file, 'r').read(1) != '0'
except:
return False
try:
return open(state_file, 'r').read(1) != '0'
except Exception as e:
Log("Getting state failed: ", e)
return False

def WriteState(On):
try:
char = '0'
if On:
char = '1'
f = open(state_file, 'w')
f.write(char)
f.close()
except:
return
try:
char = '0'
if On:
char = '1'
f = open(state_file, 'w')
f.write(char)
f.close()
except Exception as e:
Log("Writting state failed: ", e)
return

def Log(*message):
if debug:
for m in message:
print(m)
if debug:
print(*message)


devices = { x.name : x for x in pywemo.discover_devices()}
heater = devices["Heater"]
lightDetector = devices["Light detector"]

Log(devices)

debug = True
light_was_drawing_power = GetOldState()
twentyMins = datetime.timedelta(minutes=20)
#twentyMins = datetime.timedelta(seconds=20)

#loop for an hour
second_sleep =5
for x in range(0, 3600, second_sleep):
EnsureOn(env.get_switch('Light detector'))

env.wait(second_sleep)
light_outlet = env.get_switch('Light detector')
heater = env.get_switch('Heater')
twentyMinsFuture = datetime.datetime.now() + twentyMins
EnsureOn(lightDetector)

time.sleep(second_sleep)
twentyMinsFuture = datetime.datetime.now() + twentyMins

drawing_power = DrawingPower(light_outlet)
heater_on = IsOn(heater)

while (not light_was_drawing_power) and heater_on and (not drawing_power) and datetime.datetime.now() < twentyMinsFuture:
env.wait(second_sleep)
drawing_power = DrawingPower(light_outlet)
drawing_power = DrawingPower(lightDetector)
heater_on = IsOn(heater)

if drawing_power:
if not heater_on:
heater.on()
else:
if heater_on:
heater.off()

if light_was_drawing_power != drawing_power:
light_was_drawing_power = drawing_power
WriteState(drawing_power)

Log("light_was_drawing_power:", light_was_drawing_power, "drawing_power:", drawing_power, "heater_on:", heater_on)

while (not light_was_drawing_power) and heater_on and (not drawing_power) and datetime.datetime.now() < twentyMinsFuture:
time.sleep(second_sleep)
drawing_power = DrawingPower(lightDetector)
heater_on = IsOn(heater)
Log("Inner - ", "light_was_drawing_power:", light_was_drawing_power, "drawing_power:", drawing_power, "heater_on:", heater_on)

if drawing_power:
if not heater_on:
heater.on()
else:
if heater_on:
heater.off()

if light_was_drawing_power != drawing_power:
light_was_drawing_power = drawing_power
WriteState(drawing_power)
Log("Writting state:", drawing_power)

#state = IsOn(switch)
#greater than 6 watts
Expand Down
Loading

0 comments on commit c8e155d

Please sign in to comment.