Add control over MQTT

This commit is contained in:
arofarn 2020-04-22 23:20:10 +02:00
parent 38b9ed20cd
commit 43ba1bc18a

View File

@ -101,7 +101,7 @@ def now(clock):
return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*clock.datetime()) return "{0:04d}/{1:02d}/{2:02d}_{4:02d}:{5:02d}:{6:02d}".format(*clock.datetime())
def MqttPublish(topic, message, retain=False, qos=0, sleep=0): def mqtt_publish(topic, message, retain=False, qos=0, sleep=0):
"""MQTT publish helper.""" """MQTT publish helper."""
global CLIENT global CLIENT
@ -114,10 +114,35 @@ def MqttPublish(topic, message, retain=False, qos=0, sleep=0):
time.sleep_ms(sleep) time.sleep_ms(sleep)
def MqttMultiPublish(payload, sleep=0): def mqtt_multipub(payload, sleep=0):
"""Send multiple data""" """Send multiple data"""
for pl in payload: for pl in payload:
MqttPublish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep) mqtt_publish(pl[0], pl[1], retain=pl[2], qos=pl[3], sleep=sleep)
def mqtt_callback(topic, msg):
"""Callback en cas de changement de mode"""
global CURRENT_MODE
global BRIGHTN
global PWR
# print(topic, msg)
t = topic.split(b'/')
l = len(t)
if t[l-1] == b'value' and msg.isdigit():
msg = int(msg)
# print(t[l-2], msg)
if t[l-2] == b'mode' and msg in range(0, len(light_modes.MODES_LST)):
# print("Nouveau mode: ", int(msg))
CURRENT_MODE = msg
if t[l-2] == b'brightness' and msg in range(0, 101):
# print("Modif. luminosité :", int(msg))
BRIGHTN = msg
ENCODER.reset(BRIGHTN)
if t[l-2] == b'power' and msg in (0, 1):
PWR = bool(msg)
else:
print("Pas une valeur. Ignore...")
def power_cycle(): def power_cycle():
@ -143,8 +168,8 @@ def power_cycle():
time.sleep_ms(10) time.sleep_ms(10)
# Envoi de la nouvelle info au MQTT # Envoi de la nouvelle info au MQTT
MqttMultiPublish([['power/date', now(rtc), True, 1], mqtt_multipub([['power/date', now(rtc), True, 1],
['power/value', "{}".format(PWR), True, 0]]) ['power/value', "{:d}".format(PWR), True, 0]])
def update_button_c(button, button_state, pwr, mode): def update_button_c(button, button_state, pwr, mode):
@ -174,8 +199,8 @@ def update_button_c(button, button_state, pwr, mode):
if mode >= len(light_modes.MODES_LST): if mode >= len(light_modes.MODES_LST):
mode = 0 mode = 0
MqttMultiPublish([['mode/date', now(rtc), True, 1], mqtt_multipub([['mode/date', now(rtc), True, 1],
['mode/value', "{}".format(mode), True, 1]]) ['mode/value', "{}".format(mode), True, 1]])
return mode return mode
@ -202,10 +227,18 @@ data = [["last_boot", now(rtc), True, 1],
['brightness/date', now(rtc), True, 1], ['brightness/date', now(rtc), True, 1],
["brightness/value", "{}".format(BRIGHTN), True, 1], ["brightness/value", "{}".format(BRIGHTN), True, 1],
['power/date', now(rtc), True, 1], ['power/date', now(rtc), True, 1],
["power/value", "{}".format(PWR), True, 1], ["power/value", "{:d}".format(PWR), True, 1],
] ]
MqttMultiPublish(data) mqtt_multipub(data)
# MQTT subscription
print("Set callback...")
CLIENT.set_callback(mqtt_callback)
print("OK")
print("Subcribe...")
CLIENT.subscribe(b"{location}/{device}/#".format(location=config.LOCATION,
device=CLIENT.client_id))
print("OK")
time.sleep(1) time.sleep(1)
BRIGHTN_DATA = None BRIGHTN_DATA = None
@ -236,9 +269,11 @@ while True:
update_conn_led() update_conn_led()
CLIENT.check_msg()
# On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY) # On n'envoie les données de luminosité qu'une fois stabilisée (MQTT_DELAY)
if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), MQTT_TIMER) >= 0: if BRIGHTN_DATA is not None and time.ticks_diff(time.ticks_ms(), MQTT_TIMER) >= 0:
MqttMultiPublish(BRIGHTN_DATA) mqtt_multipub(BRIGHTN_DATA)
BRIGHTN_DATA = None BRIGHTN_DATA = None
time_to_test = rtc.datetime() time_to_test = rtc.datetime()