mqtt.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import paho.mqtt.client as mqtt
  2. import json
  3. def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
  4. """_summary_
  5. Args:
  6. broker (str): _description_
  7. port (int): _description_
  8. username (str): _description_
  9. password (str): _description_
  10. Returns:
  11. mqtt: _description_
  12. """
  13. # Create a new MQTT client instance
  14. client = mqtt.Client()
  15. # Set username and password
  16. client.username_pw_set(username, password)
  17. # Connect to the MQTT broker
  18. client.connect(broker, port, 60)
  19. return client
  20. def create_config(client:mqtt)->None:
  21. """Create Home Assistant discovery topics
  22. Args:
  23. client (mqtt): MQTT Client
  24. """
  25. # Device-specific information for multiple sensors
  26. node_id = "floppy_player" # Unique device ID
  27. # Define discovery and state topics for each sensor
  28. discovery_topic_disc = f"homeassistant/sensor/floppy_player/current_disc/config"
  29. state_topic_disc = f"homeassistant/sensor/floppy_player/current_disc/state"
  30. discovery_topic_status = f"homeassistant/sensor/floppy_player/status/config"
  31. state_topic_status = f"homeassistant/sensor/floppy_player/status/state"
  32. # Sensor 1: current_disc (a text-based sensor)
  33. disc_config = {
  34. "name": "Current Disc",
  35. "state_topic": state_topic_disc,
  36. "value_template": "{{ value }}", # Textual value
  37. "unique_id": f"{node_id}_current_disc",
  38. "device": {
  39. "identifiers": [node_id],
  40. "name": "Floppy Player",
  41. "model": "v1",
  42. "manufacturer": "Karl"
  43. }
  44. }
  45. # Sensor 2: status (another text-based sensor)
  46. status_config = {
  47. "name": "Device Status",
  48. "state_topic": state_topic_status,
  49. "value_template": "{{ value }}", # Textual value
  50. "unique_id": f"{node_id}_status",
  51. "device": {
  52. "identifiers": [node_id],
  53. "name": "Floppy Player",
  54. "model": "v1",
  55. "manufacturer": "Karl"
  56. }
  57. }
  58. client.publish(discovery_topic_disc, json.dumps(disc_config), retain=True)
  59. client.publish(discovery_topic_status, json.dumps(status_config), retain=True)
  60. def update_disc(client:mqtt, disc_message:dict)->None:
  61. """Update current disc.
  62. Args:
  63. client (mqtt): MQTT Client
  64. disc_message (dict): Current disc information
  65. """
  66. client.publish("homeassistant/sensor/floppy_player/current_disc/state", json.dumps(disc_message))
  67. print(f"Published current disc: {disc_message}")
  68. def control_player(client:mqtt, state:str)->None:
  69. """Control the player
  70. Args:
  71. client (mqtt): MQTT Client
  72. state (str): Player State
  73. """
  74. client.publish("homeassistant/sensor/floppy_player/status/state", state)
  75. print(f"Published status: {state}")