Pārlūkot izejas kodu

switch back from radio

Karl Hudgell 6 mēneši atpakaļ
vecāks
revīzija
cf3005cdc0
2 mainītis faili ar 18 papildinājumiem un 5 dzēšanām
  1. 1 0
      buttons.py
  2. 17 5
      lib/mqtt.py

+ 1 - 0
buttons.py

@@ -41,6 +41,7 @@ def button_callback_3(channel):
         if time.time() - start_time >= 3:  # Check if 3 seconds have passed
             print("Button 3 held for 3 seconds! Sending RADIO2.")
             update_disc(client, ["ytmusic",disc_data['name'],disc_data['type'],"RESET"])
+            control_player(client, "PLAY")
             update_disc(client, ["ytmusic",disc_data['name'],disc_data['type'],disc_data['id']])
             return  # Exit after sending RADIO2
     

+ 17 - 5
lib/mqtt.py

@@ -1,6 +1,7 @@
 import os
 import paho.mqtt.client as mqtt
 import json
+import time
 from dotenv import load_dotenv
 
 # Load the .env file
@@ -228,29 +229,40 @@ def check_current_status(client: mqtt.Client) -> str:
     return userdata['message']
 
 
-def update_disc(client: mqtt.Client, disc_message: dict) -> None:
+def update_disc(client: mqtt.Client, disc_message: list) -> None:
     """Update current disc.
 
     Args:
         client (mqtt.Client): MQTT Client
         disc_message (dict): Current disc information
     """
+    # Publish disc name with QoS 1
     client.publish(
         "homeassistant/sensor/floppy_player/current_disc/state",
         disc_message[1],
-        retain=True,
+        qos=1,
+        retain=True
     )
+    time.sleep(0.1)  # Small delay to ensure messages are sent in order
+
+    # Publish disc type with QoS 1
     client.publish(
         "homeassistant/sensor/floppy_player/current_disc_type/state",
         disc_message[2],
-        retain=True,
+        qos=1,
+        retain=True
     )
+    time.sleep(0.1)
+
+    # Publish disc ID with QoS 1
     client.publish(
         "homeassistant/sensor/floppy_player/current_disc_id/state",
         disc_message[3],
-        retain=True,
+        qos=1,
+        retain=True
     )
-    print(f"Published current disc: {disc_message}")
+
+    print(f"Published current disc with QoS 1: {disc_message}")
 
 
 def control_player(client: mqtt.Client, state: str) -> None: