gardencontrol/heat/_Heat.py

59 lines
1.5 KiB
Python
Raw Normal View History

2021-05-05 10:30:56 +00:00
import datetime
import logging
2021-05-05 10:30:56 +00:00
import threading
import time
import RPi.GPIO as GPIO
class Heat(threading.Thread):
def __init__(self, pin):
threading.Thread.__init__(self)
2021-05-05 10:30:56 +00:00
self.__pin = pin
self.__state = False
self.__lock = threading.Lock()
2021-05-05 10:30:56 +00:00
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
if not GPIO.input(pin):
2021-05-05 10:30:56 +00:00
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)
2021-05-05 10:30:56 +00:00
def off(self):
"""Switch the heat off"""
with self.__lock:
self.__state = False
GPIO.output(self.__pin, 1)
2021-05-05 10:30:56 +00:00
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"""
2021-05-05 10:30:56 +00:00
self.__run_condition = False
self.join()
def state(self):
"""Return the state of the heat"""
2021-05-05 10:30:56 +00:00
return self.__state