Created
September 29, 2022 13:15
-
-
Save lbt/d665886890e32f88254735d007a7f3bb to your computer and use it in GitHub Desktop.
BathroomPump.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import logging | |
import datetime | |
from PySide2.QtCore import QObject, Slot | |
from PySide2.QtCore import QTimer | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
class BathroomPump(QObject): | |
def __init__(self, sensors, relay, pir, parent=None): | |
QObject.__init__(self, parent) | |
self.sensors = sensors | |
self.timer = QTimer(self) | |
self.pumpRelay = sensors.QtSensor(relay) | |
self.pumpPIR = sensors.QtSensor(pir) | |
self.lastPumpedAt = datetime.datetime.min | |
self.pumpPIR.triggered.connect(self.checkPump) | |
self.pumpOccasionally = datetime.timedelta(minutes=30) | |
logger.debug("Bathroom Pump Created") | |
async def init(self): | |
# There should be no retained MQTT so just make sure we're off | |
self.pumpRelay.off() | |
logger.debug("Bathroom Pump Initialised") | |
@Slot() | |
def checkPump(self): | |
logger.debug("Bathroom Pump Checking") | |
now = datetime.datetime.now() | |
if (now - self.lastPumpedAt) < self.pumpOccasionally: | |
return | |
self.lastPumpedAt = now | |
logger.debug("Bathroom Pump On") | |
self.pumpRelay.on() | |
QTimer.singleShot(30000, self.pumpOff) | |
@Slot() | |
def pumpOff(self): | |
logger.debug("Bathroom Pump Off") | |
self.pumpRelay.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment