Rather stuck with this one, I'd like to automatically (re)set the propery "relaystate" when one of the others (Tm, Tset, Th) is set, regardless of wether their value has changed.


My code so far:

from collections import namedtuple
import logging

logger = logging.getLogger()
stream_handler = logging.StreamHandler()
formatter = logging.Formatter(
    '[%(levelname)-8s] %(asctime)s (%(name)-8s) - %(message)s',
    '%Y-%m-%d %H:%M:%S',

class Thermostat():
    def __init__(self, Tm=20, Tset=20, Th=0.3):
        self.Tm = Tm
        self.Tset = Tset
        self.Th = Th
        self.relaystate = None

    def Tm(self):
        logger.debug("GETTER Tm")
        return self._Tm
    def Tm(self, value):
        logger.debug("SETTER Tm")
        self._Tm = value

    def Tset(self):
        logger.debug("GETTER Tset")
        return self._Tset
    def Tset(self, value):
        logger.debug("SETTER Tset")
        self._Tset = value

    def Th(self):
        logger.debug("GETTER Th")
        return self._Th
    def Th(self, value):
        logger.debug("SETTER Th")
        self._Th = value

    def relaystate(self):
        logger.debug("GETTER relaystate")
        return self._relaystate
    def relaystate(self, value):
        logger.debug("SETTER relaystate")
        #on init while setting Tm, Tset and Th are not known
        #so relaystate can not be calculated
            lower = self.Tset-self.Th
            upper = self.Tset+self.Th
        except AttributeError as e:
            logger.debug("SETTER relaystate : %s", e)
            self._relaystate = None
        rlstate = namedtuple('relaystate', ['heat', 'cool'])
        if self.Tm < lower:
            value = rlstate(1,0)
        elif self.Tm > upper:
            value = rlstate(0,1)
        elif self.Tm > lower and self.Tm < upper:
            value = rlstate(0,0)
        self._relaystate = value

if __name__ == "__main__":

    TS1 = Thermostat()
    TS1.Tset = 44
    print("heat : ",TS1.relaystate.heat,"cool : ",TS1.relaystate.cool)

Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

Reply via email to