123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import paho.mqtt.client as mqtt
- import paho.mqtt.subscribe as subscribe
- import json
- def create_client(broker: str, port: int, username: str, password: str) -> mqtt.Client:
- """Create an MQTT client and connect it to the broker
- Args:
- broker (str): MQTT broker address
- port (int): MQTT broker port
- username (str): Username for MQTT broker
- password (str): Password for MQTT broker
- Returns:
- mqtt.Client: Connected MQTT client instance
- """
-
- client = mqtt.Client()
-
- client.username_pw_set(username, password)
-
- client.connect(broker, port, 60)
-
- return client
- def create_config(client: mqtt.Client) -> None:
- """Create Home Assistant discovery topics
- Args:
- client (mqtt.Client): MQTT Client
- """
-
- node_id = "floppy_player"
-
- 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"
-
- disc_config = {
- "name": "Current Disc",
- "state_topic": state_topic_disc,
- "value_template": "{{ value }}",
- "unique_id": f"{node_id}_current_disc",
- "device": {
- "identifiers": [node_id],
- "name": "Floppy Player",
- "model": "v1",
- "manufacturer": "Karl"
- }
- }
-
- status_config = {
- "name": "Device Status",
- "state_topic": state_topic_status,
- "value_template": "{{ value }}",
- "unique_id": f"{node_id}_status",
- "device": {
- "identifiers": [node_id],
- "name": "Floppy Player",
- "model": "v1",
- "manufacturer": "Karl"
- }
- }
- client.publish(discovery_topic_disc, json.dumps(disc_config), retain=True)
- client.publish(discovery_topic_status, json.dumps(status_config), retain=True)
- def check_current_disc(client: mqtt.Client) -> str:
- """Check the current loaded disc by subscribing to the retained message on the state topic.
- Args:
- client (mqtt.Client): MQTT Client
- Returns:
- str: Current object at current_disc/state
- """
- def on_message(client, userdata, message):
- """Callback function to handle received MQTT messages."""
- userdata['message'] = message.payload.decode()
-
-
- userdata = {'message': None}
-
- client.user_data_set(userdata)
- client.on_message = on_message
-
- client.subscribe("homeassistant/sensor/floppy_player/current_disc/state")
-
- timeout = 5
- while userdata['message'] is None and timeout > 0:
- client.loop(timeout=0.1)
- timeout -= 0.1
-
- if userdata['message'] is None:
- raise TimeoutError("No retained message was found.")
- return userdata['message']
- def update_disc(client: mqtt.Client, disc_message: dict) -> None:
- """Update current disc.
- Args:
- client (mqtt.Client): MQTT Client
- disc_message (dict): Current disc information
- """
- client.publish("homeassistant/sensor/floppy_player/current_disc/state", json.dumps(disc_message), retain=True)
- print(f"Published current disc: {disc_message}")
- def control_player(client: mqtt.Client, state: str) -> None:
- """Control the player
- Args:
- client (mqtt.Client): MQTT Client
- state (str): Player State
- """
- client.publish("homeassistant/sensor/floppy_player/status/state", state, retain=True)
- print(f"Published status: {state}")
|