From c440b10bbe126e9cbe4f674c2058ba218c0e260a Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 23 Jul 2022 21:52:18 +0200 Subject: [PATCH] MQTT auth and improvements implemented --- .gitignore | 16 +++++++++++++++- config_example/config.py | 9 +++++---- powerplug.py | 3 ++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 35b217f..dfdbddf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # ---> powerplug-energenie # config.py - +*.err # ---> Python # Byte-compiled / optimized / DLL files @@ -119,6 +119,20 @@ dmypy.json # Pyre type checker .pyre/ +# ---> VirtualEnv +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +.venv +pip-selfcheck.json + # ---> Linux *~ diff --git a/config_example/config.py b/config_example/config.py index b7cd8ef..aa3fc4a 100644 --- a/config_example/config.py +++ b/config_example/config.py @@ -3,8 +3,9 @@ import os import report - -MQTT_SERVER = "multimedia" +MQTT_USER = "user" +MQTT_PASS = "pass" +MQTT_SERVER = "localhost" MQTT_TOPIC = "hifi/powerplug" # @@ -12,10 +13,10 @@ MQTT_TOPIC = "hifi/powerplug" # __BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) APP_NAME = "powerplug" -LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout' +LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout' LOGLVL = 'DEBUG' -LOGHOST = 'localhost' +LOGHOST = 'cutelog' LOGPORT = 19996 formatter = report.LONG_FMT diff --git a/powerplug.py b/powerplug.py index 4c4c45c..c31b1f2 100644 --- a/powerplug.py +++ b/powerplug.py @@ -52,7 +52,8 @@ class mqtt_powerplug(object): def __init__(self): self.__client__ = mqtt.Client("mqtt_powerplug") # create client object self.__client__.on_message = self.__receive__ # attach function to callback - self.__client__.connect("192.168.0.131", 1883) # establish connection + self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials + self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection self.__client__.loop_start() # start the loop self.__topics__ = [] for subtopic in self.SUBTOPICS: