Poll the MQTT server for updates

This commit is contained in:
polyfloyd 2016-10-14 20:08:17 +02:00
parent 5ddda7c331
commit 99dfc40c91

View file

@ -48,18 +48,15 @@ def get_state():
} }
def await_state_change(prev_state): def await_state_change(prev_state):
# FIXME: Changing the state using a switch while the space is cycling will be ignored. state = None
# FIXME: If the alarm is closed while the space is being opened, the state will stay open.
# Receive 3 messages from MQTT. The first two messages are retained.
# The last one will be acted upon.
# This is not the cleanest solution...
while True: while True:
cmd = ['mqtt-simple', '--count', '3', '-h', MQTT_HOST, '-s', 'bitlair/switch/state', '-s', 'bitlair/alarm'] try:
subprocess.check_output(cmd)[:-1].decode('utf8')
state = get_state() state = get_state()
if state != prev_state: if state != prev_state:
return state return state
except Exception as err:
print(err)
time.sleep(2)
if __name__ == '__main__': if __name__ == '__main__':
prev_state = get_state() prev_state = get_state()