gardencontrol/heat/_Heat.py

59 lines
1.5 KiB
Python

import datetime
import logging
import threading
import time
import RPi.GPIO as GPIO
class Heat(threading.Thread):
def __init__(self, pin):
threading.Thread.__init__(self)
self.__pin = pin
self.__state = False
self.__lock = threading.Lock()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
if not GPIO.input(pin):
self.__state = True
self.__run_condition = True
self.__next_update = datetime.datetime.now()
def on(self):
"""Switch the heat on"""
with self.__lock:
self.__state = True
GPIO.output(self.__pin, 0)
def off(self):
"""Switch the heat off"""
with self.__lock:
self.__state = False
GPIO.output(self.__pin, 1)
def run(self):
self.__next_update = datetime.datetime.now()
while self.__run_condition:
now = datetime.datetime.now()
if now >= self.__next_update:
if self.__state:
# Do a power cycle to prevent auto-poweroff
GPIO.output(self.__pin, 1)
time.sleep(5)
GPIO.output(self.__pin, 0)
self.__next_update = now + datetime.timedelta(minutes=5)
time.sleep(1)
self.off()
def stop(self):
"""Stop the controlling thead"""
self.__run_condition = False
self.join()
def state(self):
"""Return the state of the heat"""
return self.__state