4 import paho.mqtt.client as mqtt
19 self.now = time.time()
20 tm = time.localtime(self.now)
21 self.year = tm.tm_year
22 self.month = tm.tm_mon
24 self.hour = tm.tm_hour
26 self.wday = tm.tm_wday
28 def get_sensor(self, key):
29 topic = "burrow/" + key
30 if topic in self.attrs:
31 s = self.attrs[topic].split(" ")
32 if len(s) >= 2 and s[1] < self.now - 120:
38 def set(self, key, val):
40 topic = "burrow/" + key
41 debug("Setting {} to {}".format(topic, val))
42 mq.publish(topic, val, qos=1, retain=True)
46 def on_connect(mq, userdata, flags, rc):
47 mq.subscribe("burrow/#")
49 def on_message(mq, userdata, msg):
51 debug("Message {}: {}".format(msg.topic, msg.payload))
52 st.attrs[msg.topic] = msg.payload
56 lt = st.get_sensor("temp/loft")
57 if lt is not None and lt >= 30:
59 elif st.hour in range(10, 20):
63 st.set("loft/fan", fs)
67 if st.hour in range(19, 23):
71 st.set("loft/circulation", c)
73 opts, args = getopt.gnu_getopt(sys.argv[1:], "", ["debug"])
80 mq.on_connect = on_connect
81 mq.on_message = on_message
82 mq.will_set("status/auto", "failed", retain=True)
83 mq.connect("127.0.0.1")
84 mq.publish("status/auto", "ok", retain=True)