#!/usr/bin/python3
+from collections import deque
import getopt
import paho.mqtt.client as mqtt
import sys
return True
def hysteresis(self, key, value, low, high):
- if key in self.hyst_state:
- old_state = self.hyst_state[key]
- else:
- old_state = 0
+ old_state = self.hyst_state.get(key, 0)
if value is None:
new_state = 0
elif old_state <= 0:
def update_average(self, key, window_seconds):
if key not in self.running_averages:
- self.running_averages[key] = ([], 0, 0, None)
+ self.running_averages[key] = (deque(), 0, 0, None)
(history, sum, count, avg) = self.running_averages[key]
while len(history) > 0 and history[0][0] <= self.now - window_seconds:
if history[0][1] is not None:
sum -= history[0][1]
count -= 1
- history.pop(0)
+ history.popleft()
curr = self.get_sensor(key)
history.append((self.now, curr))