-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
45 lines (32 loc) · 1.09 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import os
from dotenv import load_dotenv
load_dotenv()
import rxv
from mqttwrapper import run_script
RECEIVER_ADDR = os.environ.get("RECEIVER_ADDR")
RECEIVER_VOLUME = float(os.environ.get("RECEIVER_VOLUME", -60.0))
def message_callback(topic: str, payload: bytes, rxv_client):
if payload == b"off":
# Don't switch off if another source is selected.
if rxv_client.input == "NET RADIO":
print("Switching off")
rxv_client.on = False
else:
station = payload.decode("utf-8")
volume = min(RECEIVER_VOLUME, -50.0) # no loud surprises
print(f"Playing {station} at {volume}dB")
rxv_client.on = True
rxv_client.net_radio(station)
rxv_client.volume = volume
def setup_rxv():
if RECEIVER_ADDR:
ctrl_url = f"http://{RECEIVER_ADDR}:80/YamahaRemoteControl/ctrl"
return rxv.RXV(ctrl_url)
else:
return rxv.find()[0]
def main():
rxv_client = setup_rxv()
run_script(message_callback, rxv_client=rxv_client, ignore_retained=True)
if __name__ == "__main__":
main()