From 26878f172e5d8495d1a4f7a23ab9f5cc88e8fe62 Mon Sep 17 00:00:00 2001 From: Norm Rasmussen Date: Mon, 14 Aug 2023 16:48:05 -0400 Subject: [PATCH] Created python script to send MQTT message with Todo items. Need to add an automation for running it when it saves. Also need to add some error handling. Next up is to get the ESP side stood up. --- Scripts/Todo_reorg/Todos.md | 7 +++ Scripts/Todo_reorg/mqtt_test_.py | 44 +++++++++++++++ Scripts/Todo_reorg/todo_mqtt.py | 91 ++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 Scripts/Todo_reorg/mqtt_test_.py create mode 100644 Scripts/Todo_reorg/todo_mqtt.py diff --git a/Scripts/Todo_reorg/Todos.md b/Scripts/Todo_reorg/Todos.md index e69de29b..0d8a5fc8 100644 --- a/Scripts/Todo_reorg/Todos.md +++ b/Scripts/Todo_reorg/Todos.md @@ -0,0 +1,7 @@ +# This is a Heading + + +## Do this and do that + +TODO: King of de castle, king of de castle +TODO: Do this thing now! diff --git a/Scripts/Todo_reorg/mqtt_test_.py b/Scripts/Todo_reorg/mqtt_test_.py new file mode 100644 index 00000000..d30b144a --- /dev/null +++ b/Scripts/Todo_reorg/mqtt_test_.py @@ -0,0 +1,44 @@ +''' +This was the code provided by this tutorial: + https://techtutorialsx.com/2017/04/14/python-publishing-messages-to-mqtt-topic/ +''' +import paho.mqtt.client as mqttClient +import time + + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected to broker") + + global Connected + Connected = True + + else: + print("Connection failed") + + +Connected = False + +broker_address = "192.168.200.55" +port = 1883 +user = "hass" +password = "Over9+look*" + +client = mqttClient.Client("Python") +client.username_pw_set(user, password=password) +client.on_connect = on_connect +client.connect(broker_address, port=port) + +client.loop_start() + +while Connected != True: + time.sleep(0.1) + +try: + while True: + value = input("Enter the message:") + client.publish("python/test", value) + +except KeyboardInterrupt: + client.disconnect() + client.loop_stop() diff --git a/Scripts/Todo_reorg/todo_mqtt.py b/Scripts/Todo_reorg/todo_mqtt.py new file mode 100644 index 00000000..297bfbf3 --- /dev/null +++ b/Scripts/Todo_reorg/todo_mqtt.py @@ -0,0 +1,91 @@ +import sys +import os +import paho.mqtt.client as mqttClient +import time +from datetime import date +import json + +# Paho MQTT Setup +Connected = False +broker_address = "192.168.200.55" +port = 1883 +user = "hass" +password = "Over9+look*" +client = mqttClient.Client("Python") + +# Working Dir setup +# rootdir = "/Users/normrasmussen/Documents/Work/CustomerNotes/" +# todoDir = "/Users/normrasmussen/Documents/Work/" +rootdir = "/Users/normrasmussen/Documents/Work/Scripts/Todo_reorg/" +input = sys.argv[1] +company = input.split("/")[6] +print(company) + + +def findCompany(rootdir, company): + files = os.listdir(rootdir) + for fileName in files: + if fileName.startswith(".") or fileName.startswith("Todos"): + pass + else: + company = fileName[:-3] + findTodos(rootdir, company) + + +def findTodos(rootdir, company): + todos = [] + with open(rootdir + company, "r") as currentfile: + file = currentfile.readlines() + for fullTasks in file: + if "TODO:" in fullTasks: + tasks = fullTasks + create_payload(tasks) + # deleteTasks(rootdir, company, tasks) + todos.append(tasks) + # writeTasks(company, todos) + # create_payload(todos) + + +def create_payload(tasks): + payload = {} + client.username_pw_set(user, password=password) + client.on_connect = on_connect + client.connect(broker_address, port=port) + client.loop_start() + # for todo in todos: + time.sleep(5) + status = tasks.split()[0] + status = status[:-1] + + todo = tasks[6:-1] + payload = {"status": status, "task": todo} + payload = str(payload) + print(payload) + client.publish("home/todos", payload) + + # Wait for a connection - This was used by the tutorial +# Tutorial: +# https://techtutorialsx.com/2017/04/14/python-publishing-messages-to-mqtt-topic/ + # while Connected != True: + # time.sleep(0.1) + # try: + # while True: + # client.publish("home/todos", payload) + # except KeyboardInterrupt: + # client.disconnect() + # client.loop_stop() + + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print("Connected to broker") + + global Connected + Connected = True + + else: + print("Connection failed") + + +if __name__ == "__main__": + findTodos(rootdir, company)