Bläddra i källkod

move config to mqtt client

Karl Hudgell 6 månader sedan
förälder
incheckning
7ee01c0e4a
3 ändrade filer med 16 tillägg och 40 borttagningar
  1. 2 16
      buttons.py
  2. 13 8
      lib/mqtt.py
  3. 1 16
      main.py

+ 2 - 16
buttons.py

@@ -1,22 +1,8 @@
-import os
 import RPi.GPIO as GPIO
 import time
-from dotenv import load_dotenv
-from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
+from lib.mqtt import create_client, control_player, create_config
 
-# Load the .env file
-load_dotenv()
-
-# Define the MQTT server details
-broker = os.environ.get("broker")
-port = int(os.environ.get("port"))
-# MQTT username and password
-username = os.environ.get("username")
-password = os.environ.get("password")
-
-#print(f"Broker: {broker}, Port: {port}, Username: {username}")
-
-client = create_client(broker, port, username, password)
+client = create_client()
 
 create_config(client)
 

+ 13 - 8
lib/mqtt.py

@@ -1,16 +1,21 @@
+import os
 import paho.mqtt.client as mqtt
-import paho.mqtt.subscribe as subscribe
 import json
+from dotenv import load_dotenv
 
+# Load the .env file
+load_dotenv()
 
-def create_client(broker: str, port: int, username: str, password: str) -> mqtt.Client:
-    """Create an MQTT client and connect it to the broker
+# Define the MQTT server details
+broker = os.environ.get("broker")
+port = int(os.environ.get("port"))
+# MQTT username and password
+username = os.environ.get("username")
+password = os.environ.get("password")
 
-    Args:
-        broker (str): MQTT broker address
-        port (int): MQTT broker port
-        username (str): Username for MQTT broker
-        password (str): Password for MQTT broker
+
+def create_client() -> mqtt.Client:
+    """Create an MQTT client and connect it to the broker
 
     Returns:
         mqtt.Client: Connected MQTT client instance

+ 1 - 16
main.py

@@ -1,22 +1,7 @@
-import os
 import sys
-import time
-from dotenv import load_dotenv
 from lib.mqtt import create_client, update_disc, control_player, create_config, check_current_disc
 
-# Load the .env file
-load_dotenv()
-
-# Define the MQTT server details
-broker = os.environ.get("broker")
-port = int(os.environ.get("port"))
-# MQTT username and password
-username = os.environ.get("username")
-password = os.environ.get("password")
-
-#print(f"Broker: {broker}, Port: {port}, Username: {username}")
-
-client = create_client(broker, port, username, password)
+client = create_client()
 
 create_config(client)