From 6128d8706d0f96bbc83ccd9a17bd5e3b366f17c2 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Mon, 9 Oct 2023 10:54:33 +0200 Subject: [PATCH] MQTT persist discovery msg --- .gitignore | 1 + espmusicmouse/host_driver/mqtt_json.py | 7 +++---- espmusicmouse/musicmouse.service | 13 +++++++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 espmusicmouse/musicmouse.service diff --git a/.gitignore b/.gitignore index 995e6e9..80332a4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ venv build *.FCStd1 *.blend1 +__pycache__ diff --git a/espmusicmouse/host_driver/mqtt_json.py b/espmusicmouse/host_driver/mqtt_json.py index 0040867..8b16d6f 100644 --- a/espmusicmouse/host_driver/mqtt_json.py +++ b/espmusicmouse/host_driver/mqtt_json.py @@ -6,7 +6,7 @@ from copy import deepcopy class ShelveLightMqtt: - def __init__(self, protocol, client): + def __init__(self, protocol, client: asyncio_mqtt.Client): self._protocol = protocol self._mqtt_client = client @@ -121,7 +121,7 @@ class ShelveLightMqtt: async def _send_autodiscovery_msg(self): topic = f"homeassistant/light/{self._discovery_spec['unique_id']}/config" - await self._mqtt_client.publish(topic, json.dumps(self._discovery_spec).encode()) + await self._mqtt_client.publish(topic, json.dumps(self._discovery_spec).encode(), retain=True) async def _notify_mqtt_state(self, state): state_payload = json.dumps(self._state) @@ -155,14 +155,13 @@ async def start_mqtt(music_mouse_protocol, server, username, password): await client.subscribe("musicmouse_json/#") async for message in messages: await shelve_light.handle_light_message(message) - except MqttError as error: + except asyncio_mqtt.MqttError as error: print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds') finally: await asyncio.sleep(reconnect_interval) if __name__ == "__main__": - class DummyProtocol: def shelve_led_effect(self, effect): print("EFF ", repr(effect)) diff --git a/espmusicmouse/musicmouse.service b/espmusicmouse/musicmouse.service new file mode 100644 index 0000000..f3721de --- /dev/null +++ b/espmusicmouse/musicmouse.service @@ -0,0 +1,13 @@ +# Put this into /etc/systemd/system/musicmouse.service +[Unit] +Description=Music Mouse RFID Music Player +After=multi-user.target + + +[Service] +Type=simple +Restart=always +ExecStart=/opt/musicmouse/venv/bin/python /opt/musicmouse/espmusicmouse/host_driver/main.py /media/musicmouse/ + +[Install] +WantedBy=multi-user.target \ No newline at end of file