Add MQTT controls for channel and mute
This commit is contained in:
parent
62bdbe71f5
commit
896af8c12f
1 changed files with 49 additions and 10 deletions
59
main.py
59
main.py
|
@ -19,16 +19,16 @@ VOLUME_MAX = const(128)
|
||||||
MQTT_KEEPALIVE = const(60)
|
MQTT_KEEPALIVE = const(60)
|
||||||
MQTT_UPDATE_INTERVAL = const(60)
|
MQTT_UPDATE_INTERVAL = const(60)
|
||||||
|
|
||||||
|
channels = ["LINE 1", "LINE 2", "LINE 3", "PHONO"]
|
||||||
state = StateTree(
|
state = StateTree(
|
||||||
{
|
{
|
||||||
"network": "OFF",
|
"network": "OFF",
|
||||||
"volume": {
|
"volume": {
|
||||||
"left": 0,
|
"left": 0,
|
||||||
"right": 0,
|
"right": 0,
|
||||||
"muted": False,
|
"muted": "OFF",
|
||||||
},
|
},
|
||||||
"channel": 0,
|
"channel": channels[0],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
last_update = 0
|
last_update = 0
|
||||||
|
@ -53,7 +53,7 @@ except Exception as e:
|
||||||
oled = None
|
oled = None
|
||||||
|
|
||||||
switch = cd4052.CD4052(18, 19, 23)
|
switch = cd4052.CD4052(18, 19, 23)
|
||||||
switch.select(state["channel"])
|
switch.select(0)
|
||||||
|
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
@ -81,10 +81,13 @@ def on_message(topic, msg):
|
||||||
pot.write(0, volume["left"])
|
pot.write(0, volume["left"])
|
||||||
if isinstance(volume.get("right"), int):
|
if isinstance(volume.get("right"), int):
|
||||||
pot.write(1, volume["right"])
|
pot.write(1, volume["right"])
|
||||||
if isinstance(volume.get("muted"), bool):
|
if isinstance(volume.get("muted"), str):
|
||||||
switch.mute(volume["muted"])
|
switch.mute(volume["muted"] == "ON")
|
||||||
if isinstance(msg.get("channel"), int):
|
if isinstance(msg.get("channel"), str):
|
||||||
switch.select(msg["channel"])
|
try:
|
||||||
|
switch.select(channels.index(msg["channel"]))
|
||||||
|
except ValueError:
|
||||||
|
print("WARNING: Attempted to select invalid channel", msg["channel"])
|
||||||
|
|
||||||
|
|
||||||
def loop():
|
def loop():
|
||||||
|
@ -92,8 +95,8 @@ def loop():
|
||||||
|
|
||||||
state["volume"]["left"] = pot.read(0)
|
state["volume"]["left"] = pot.read(0)
|
||||||
state["volume"]["right"] = pot.read(1)
|
state["volume"]["right"] = pot.read(1)
|
||||||
state["volume"]["muted"] = switch.muted()
|
state["volume"]["muted"] = "ON" if switch.muted() else "OFF"
|
||||||
state["channel"] = switch.channel()
|
state["channel"] = channels[switch.channel()]
|
||||||
|
|
||||||
if state.changed:
|
if state.changed:
|
||||||
# Volume changed externally
|
# Volume changed externally
|
||||||
|
@ -200,6 +203,42 @@ def loop():
|
||||||
).encode(),
|
).encode(),
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
|
mqtt.publish(
|
||||||
|
f"homeassistant/switch/digital-audio-switch/mute/config".encode(),
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"name": "Digital Audio Switch Mute",
|
||||||
|
"command_topic": f"{mqtt_prefix}/set",
|
||||||
|
"payload_on": '{"volume": {"muted": "ON"}}',
|
||||||
|
"payload_off": '{"volume": {"muted": "OFF"}}',
|
||||||
|
"state_on": "ON",
|
||||||
|
"state_off": "OFF",
|
||||||
|
"state_topic": f"{mqtt_prefix}/state",
|
||||||
|
"value_template": "{{ value_json.volume.muted }}",
|
||||||
|
"availability_topic": f"{mqtt_prefix}/status",
|
||||||
|
"unique_id": "digital-audio-switch-volume-mute",
|
||||||
|
"device": mqtt_device,
|
||||||
|
}
|
||||||
|
).encode(),
|
||||||
|
retain=True,
|
||||||
|
)
|
||||||
|
mqtt.publish(
|
||||||
|
f"homeassistant/select/digital-audio-switch/channel/config".encode(),
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"name": "Digital Audio Switch Channel",
|
||||||
|
"command_topic": f"{mqtt_prefix}/set",
|
||||||
|
"command_template": '{"channel": "{{value}}"}',
|
||||||
|
"state_topic": f"{mqtt_prefix}/state",
|
||||||
|
"value_template": "{{ value_json.channel }}",
|
||||||
|
"availability_topic": f"{mqtt_prefix}/status",
|
||||||
|
"options": channels,
|
||||||
|
"unique_id": "digital-audio-switch-channel",
|
||||||
|
"device": mqtt_device,
|
||||||
|
}
|
||||||
|
).encode(),
|
||||||
|
retain=True,
|
||||||
|
)
|
||||||
|
|
||||||
if state.changed or utime.time() - last_update >= MQTT_UPDATE_INTERVAL:
|
if state.changed or utime.time() - last_update >= MQTT_UPDATE_INTERVAL:
|
||||||
topic = f"{mqtt_prefix}/state".encode()
|
topic = f"{mqtt_prefix}/state".encode()
|
||||||
|
|
Loading…
Reference in a new issue