|
@@ -52,6 +52,16 @@ mqtt_port = config['mqtt'].getint('port')
|
|
|
mqtt_user = config['mqtt'].get('user')
|
|
|
mqtt_password = config['mqtt'].get('password')
|
|
|
|
|
|
+
|
|
|
+# MQTT Control
|
|
|
+enableMQTTControl = config['main'].getboolean('enableMQTTControl')
|
|
|
+MQTT_Control_Topic_turnOnActions = config['main'].get('MQTT_Control_Topic_turnOnActions')
|
|
|
+MQTT_Control_StateTopic = config['main'].get('MQTT_Control_StateTopic')
|
|
|
+
|
|
|
+turnOnActions = True
|
|
|
+if enableMQTTControl:
|
|
|
+ turnOnActions = False
|
|
|
+
|
|
|
TX_interface_prefer = config['cul'].get('TX_interface_prefer') # UART or MQTT
|
|
|
if TX_interface_prefer is None: # compatibility with old ini file structure
|
|
|
TX_interface_prefer = config['main'].get('TX_interface_prefer') # UART or MQTT
|
|
@@ -334,6 +344,50 @@ lastSentDevCmd = ""
|
|
|
def touch(fname, times=None):
|
|
|
with open(fname, 'a'):
|
|
|
os.utime(fname, times)
|
|
|
+
|
|
|
+def turnOnActions_enable():
|
|
|
+ global turnOnActions
|
|
|
+ turnOnActions = True
|
|
|
+ log_write("")
|
|
|
+ log_write("MQTT control: switched turnOnActions to ON")
|
|
|
+ mqttc.publish(MQTT_Control_StateTopic + "/turnOnActions", "ON", qos=0, retain=True)
|
|
|
+ try:
|
|
|
+ turnOnActions_file = open("turnOnActions", "w")
|
|
|
+ turnOnActions_file.write("ON")
|
|
|
+ turnOnActions_file.close()
|
|
|
+ except:
|
|
|
+ log_write("ERROR: could not write turnOnActions state to file.")
|
|
|
+
|
|
|
+def turnOnActions_disable():
|
|
|
+ global turnOnActions
|
|
|
+ turnOnActions = False
|
|
|
+ log_write("")
|
|
|
+ log_write("MQTT control: switched turnOnActions to OFF")
|
|
|
+ mqttc.publish(MQTT_Control_StateTopic + "/turnOnActions", "OFF", qos=0, retain=True)
|
|
|
+ try:
|
|
|
+ turnOnActions_file = open("turnOnActions", "w")
|
|
|
+ turnOnActions_file.write("OFF")
|
|
|
+ turnOnActions_file.close()
|
|
|
+ except:
|
|
|
+ log_write("ERROR: could not write turnOnActions state to file.")
|
|
|
+
|
|
|
+def turnOnActions_get():
|
|
|
+ global turnOnActions
|
|
|
+ try:
|
|
|
+ turnOnActions_file = open("turnOnActions", "r")
|
|
|
+ _content = turnOnActions_file.read()
|
|
|
+ turnOnActions_file.close()
|
|
|
+
|
|
|
+ log_write("MQTT control: restored last state of turnOnActions from file")
|
|
|
+ if _content == "ON":
|
|
|
+ turnOnActions = True
|
|
|
+ log_write("MQTT control: switched turnOnActions to ON")
|
|
|
+ elif _content == "OFF":
|
|
|
+ turnOnActions = True
|
|
|
+ log_write("MQTT control: switched turnOnActions to OFF")
|
|
|
+ except:
|
|
|
+ log_write("ERROR: could not read turnOnActions state from file.")
|
|
|
+
|
|
|
|
|
|
def on_connect(client, userdata, flags, rc):
|
|
|
if verbose:
|
|
@@ -351,12 +405,20 @@ def on_connect(client, userdata, flags, rc):
|
|
|
client.subscribe(in_topic)
|
|
|
if verbose:
|
|
|
log_write("MQTT subscribed: " + in_topic)
|
|
|
+
|
|
|
+ # MQTT control
|
|
|
+ if enableMQTTControl:
|
|
|
+ if MQTT_Control_Topic_turnOnActions != "":
|
|
|
+ client.subscribe(MQTT_Control_Topic_turnOnActions)
|
|
|
+ if verbose:
|
|
|
+ log_write("MQTT subscribed: " + MQTT_Control_Topic_turnOnActions)
|
|
|
|
|
|
def on_disconnect(client, userdata, rc):
|
|
|
if rc != 0:
|
|
|
log_write("Unexpected MQTT disconnection. Will auto-reconnect")
|
|
|
|
|
|
def on_message(client, userdata, msg):
|
|
|
+ global turnOnActions
|
|
|
#print(msg.topic + ": " + str(msg.payload))
|
|
|
payload = msg.payload.decode("utf-8")
|
|
|
|
|
@@ -364,8 +426,15 @@ def on_message(client, userdata, msg):
|
|
|
log_write("")
|
|
|
log_write("MQTT received: " + msg.topic + " -> " + str(payload))
|
|
|
|
|
|
+ # MQTT message is for control
|
|
|
+ if enableMQTTControl and msg.topic == MQTT_Control_Topic_turnOnActions:
|
|
|
+ if payload == "ON" or payload == "on" or payload == "true" or payload == "1":
|
|
|
+ turnOnActions_enable()
|
|
|
+ elif payload == "OFF" or payload == "off" or payload == "false" or payload == "0":
|
|
|
+ turnOnActions_disable()
|
|
|
+
|
|
|
# MQTT message is output from CUL
|
|
|
- if receive_from_mqtt_cul and msg.topic == mqtt_cul_topic_received:
|
|
|
+ elif receive_from_mqtt_cul and msg.topic == mqtt_cul_topic_received:
|
|
|
payload = payload.rstrip()
|
|
|
|
|
|
# support output from Tasmota SerialBridge on tele/[DEVICE]/RESULT topic
|
|
@@ -415,7 +484,8 @@ def on_message(client, userdata, msg):
|
|
|
|
|
|
|
|
|
def publish_device_statusupdate(device, cmd, value):
|
|
|
- if device in devdata.keys():
|
|
|
+ global turnOnActions
|
|
|
+ if turnOnActions and device in devdata.keys():
|
|
|
if 'statTopic' in devdata[device].keys():
|
|
|
statTopic = devdata[device].get('statTopic')
|
|
|
|
|
@@ -997,6 +1067,14 @@ mqttc.on_connect = on_connect
|
|
|
mqttc.on_disconnect = on_disconnect
|
|
|
mqttc.on_message = on_message
|
|
|
|
|
|
+if enableMQTTControl:
|
|
|
+ turnOnActions_get()
|
|
|
+ log_write("")
|
|
|
+ log_write("MQTT-control is enabled.")
|
|
|
+ log_write("to enable any actions you must send 'ON' to MQTT-topic: '" + MQTT_Control_Topic_turnOnActions + "'")
|
|
|
+ log_write("")
|
|
|
+ log_write("")
|
|
|
+
|
|
|
if mqtt_user is not None and mqtt_password is not None:
|
|
|
if len(mqtt_user) > 0 and len(mqtt_password) > 0:
|
|
|
mqttc.username_pw_set(mqtt_user, mqtt_password)
|