|
@@ -1,6 +1,5 @@
|
|
|
import paho.mqtt.client as mqtt
|
|
|
import json
|
|
|
-import time
|
|
|
|
|
|
def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
|
|
"""_summary_
|
|
@@ -14,64 +13,12 @@ def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
|
|
Returns:
|
|
|
mqtt: _description_
|
|
|
"""
|
|
|
- # Device-specific information for multiple sensors
|
|
|
- node_id = "floppy_player" # Unique device ID
|
|
|
-
|
|
|
- # Define discovery and state topics for each sensor
|
|
|
- discovery_topic_disc = f"homeassistant/sensor/floppy_player/current_disc/config"
|
|
|
- state_topic_disc = f"homeassistant/sensor/floppy_player/current_disc/state"
|
|
|
-
|
|
|
- discovery_topic_status = f"homeassistant/sensor/floppy_player/status/config"
|
|
|
- state_topic_status = f"homeassistant/sensor/floppy_player/status/state"
|
|
|
-
|
|
|
-
|
|
|
- # Sensor 1: current_disc (a text-based sensor)
|
|
|
- disc_config = {
|
|
|
- "name": "Current Disc",
|
|
|
- "state_topic": state_topic_disc,
|
|
|
- "value_template": "{{ value }}", # Textual value
|
|
|
- "unique_id": f"{node_id}_current_disc",
|
|
|
- "device": {
|
|
|
- "identifiers": [node_id],
|
|
|
- "name": node_id,
|
|
|
- "model": "v1",
|
|
|
- "manufacturer": "Karl"
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # Sensor 2: status (another text-based sensor)
|
|
|
- status_config = {
|
|
|
- "name": "Device Status",
|
|
|
- "state_topic": state_topic_status,
|
|
|
- "value_template": "{{ value }}", # Textual value
|
|
|
- "unique_id": f"{node_id}_status",
|
|
|
- "device": {
|
|
|
- "identifiers": [node_id],
|
|
|
- "name": node_id,
|
|
|
- "model": "v1",
|
|
|
- "manufacturer": "Karl"
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # # Create a function to handle connection
|
|
|
- # def on_connect(client, userdata, flags, rc):
|
|
|
- # if rc == 0:
|
|
|
- # print("Connected to broker")
|
|
|
- # # Publish device configuration for both sensors
|
|
|
- # client.publish(discovery_topic_disc, json.dumps(disc_config), retain=True)
|
|
|
- # client.publish(discovery_topic_status, json.dumps(status_config), retain=True)
|
|
|
- # else:
|
|
|
- # print(f"Connection failed with code {rc}")
|
|
|
-
|
|
|
# Create a new MQTT client instance
|
|
|
client = mqtt.Client()
|
|
|
|
|
|
# Set username and password
|
|
|
client.username_pw_set(username, password)
|
|
|
|
|
|
- # # Assign the on_connect function
|
|
|
- # client.on_connect = on_connect
|
|
|
-
|
|
|
# Connect to the MQTT broker
|
|
|
client.connect(broker, port, 60)
|
|
|
|