clean up mqtt file
This commit is contained in:
parent
fca38b5819
commit
dd4cc98bd0
53
lib/mqtt.py
53
lib/mqtt.py
@ -1,6 +1,5 @@
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
|
|
||||||
def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
@ -14,64 +13,12 @@ def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
|||||||
Returns:
|
Returns:
|
||||||
mqtt: _description_
|
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
|
# Create a new MQTT client instance
|
||||||
client = mqtt.Client()
|
client = mqtt.Client()
|
||||||
|
|
||||||
# Set username and password
|
# Set username and password
|
||||||
client.username_pw_set(username, password)
|
client.username_pw_set(username, password)
|
||||||
|
|
||||||
# # Assign the on_connect function
|
|
||||||
# client.on_connect = on_connect
|
|
||||||
|
|
||||||
# Connect to the MQTT broker
|
# Connect to the MQTT broker
|
||||||
client.connect(broker, port, 60)
|
client.connect(broker, port, 60)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user