./controllers/onoff.py

./controllers/onoff.py#

from dataclasses import dataclass

@dataclass
class OnOffThermostat:
    setpoint: float
    deadband: float
    safety_high: float = 26.0
    state: int = 0  # 0=OFF, 1=ON

    def update(self, measured_temp: float) -> int:
        ''' Update the thermostat state based on the measured temperature.
            If the measured temperature is below the lower threshold (setpoint - deadband/2),
            the thermostat turns ON (state=1).
            If the measured temperature is above the upper threshold (setpoint + deadband/2),
            the thermostat turns OFF (state=0).
            If the measured temperature exceeds the safety_high limit, the thermostat turns OFF.
            Returns the current state (0=OFF, 1=ON).
        '''
        lower = self.setpoint - self.deadband / 2.0
        upper = self.setpoint + self.deadband / 2.0
        if measured_temp >= self.safety_high:
            self.state = 0
            return self.state
        if measured_temp < lower:
            self.state = 1
        elif measured_temp > upper:
            self.state = 0
        return self.state