initial commit
This commit is contained in:
commit
2f90562b9c
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.vscode/launch.json
|
||||||
|
**/*.pyc
|
146
lib/mqtt.py
Normal file
146
lib/mqtt.py
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
|
def create_client(broker:str, port:int, username:str, password:str)-> mqtt:
|
||||||
|
"""_summary_
|
||||||
|
|
||||||
|
Args:
|
||||||
|
broker (str): _description_
|
||||||
|
port (int): _description_
|
||||||
|
username (str): _description_
|
||||||
|
password (str): _description_
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
return client
|
||||||
|
|
||||||
|
def create_config(client:mqtt)->None:
|
||||||
|
"""Create Home Assistant discovery topics
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client (mqtt): MQTT Client
|
||||||
|
"""
|
||||||
|
# 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": "Floppy Player",
|
||||||
|
"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": "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 update_disc(client:mqtt, disc_message:dict)->None:
|
||||||
|
"""Update current disc.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client (mqtt): MQTT Client
|
||||||
|
disc_message (dict): Current disc information
|
||||||
|
"""
|
||||||
|
client.publish("homeassistant/sensor/floppy_player/current_disc/state", json.dumps(disc_message))
|
||||||
|
print(f"Published current disc: {disc_message}")
|
||||||
|
|
||||||
|
|
||||||
|
def control_player(client:mqtt, state:str)->None:
|
||||||
|
"""Control the player
|
||||||
|
|
||||||
|
Args:
|
||||||
|
client (mqtt): MQTT Client
|
||||||
|
state (str): Player State
|
||||||
|
"""
|
||||||
|
client.publish("homeassistant/sensor/floppy_player/status/state", state)
|
||||||
|
print(f"Published status: {state}")
|
||||||
|
|
21
main.py
Normal file
21
main.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import os
|
||||||
|
from lib.mqtt import create_client, update_disc, control_player, create_config
|
||||||
|
|
||||||
|
# Define the MQTT server details
|
||||||
|
broker = os.environ.get("broker")
|
||||||
|
port = int(os.environ.get("port"))
|
||||||
|
# MQTT username and password
|
||||||
|
username = os.environ.get('username')
|
||||||
|
password = os.environ.get('password')
|
||||||
|
|
||||||
|
client = create_client(broker, port, username, password)
|
||||||
|
|
||||||
|
create_config(client)
|
||||||
|
|
||||||
|
# update_disc(client, {"type":"album", "id":"MPREb_2UBRpewr9ad"})
|
||||||
|
# update_disc(client, {"type":"playlist", "id":"LM"})
|
||||||
|
|
||||||
|
#
|
||||||
|
control_player(client, "EJECT")
|
||||||
|
|
||||||
|
#
|
20
poetry.lock
generated
Normal file
20
poetry.lock
generated
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "paho-mqtt"
|
||||||
|
version = "2.1.0"
|
||||||
|
description = "MQTT version 5.0/3.1.1 client class"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
files = [
|
||||||
|
{file = "paho_mqtt-2.1.0-py3-none-any.whl", hash = "sha256:6db9ba9b34ed5bc6b6e3812718c7e06e2fd7444540df2455d2c51bd58808feee"},
|
||||||
|
{file = "paho_mqtt-2.1.0.tar.gz", hash = "sha256:12d6e7511d4137555a3f6ea167ae846af2c7357b10bc6fa4f7c3968fc1723834"},
|
||||||
|
]
|
||||||
|
|
||||||
|
[package.extras]
|
||||||
|
proxy = ["pysocks"]
|
||||||
|
|
||||||
|
[metadata]
|
||||||
|
lock-version = "2.0"
|
||||||
|
python-versions = "^3.8"
|
||||||
|
content-hash = "6c033a93847eceeaa0c39046b8cf5ddd3b31e00b768adae8c60d059d3003b2f5"
|
3
poetry.toml
Normal file
3
poetry.toml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[virtualenvs]
|
||||||
|
in-project = true
|
||||||
|
prefer-active-python = true
|
15
pyproject.toml
Normal file
15
pyproject.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "FloppyPlayer"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = ["Karl Hudgell"]
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
python = "^3.8"
|
||||||
|
paho-mqtt = "^2.1.0"
|
||||||
|
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
Loading…
x
Reference in New Issue
Block a user