diff --git a/espmusicmouse/host_driver/mqtt_json.py b/espmusicmouse/host_driver/mqtt_json.py index 0ed5fe2..0040867 100644 --- a/espmusicmouse/host_driver/mqtt_json.py +++ b/espmusicmouse/host_driver/mqtt_json.py @@ -145,13 +145,20 @@ class ShelveLightMqtt: async def start_mqtt(music_mouse_protocol, server, username, password): - async with asyncio_mqtt.Client(hostname=server, username=username, password=password) as client: - shelve_light = ShelveLightMqtt(music_mouse_protocol, client) - await shelve_light.init() - async with client.filtered_messages("musicmouse_json/#") as messages: - await client.subscribe("musicmouse_json/#") - async for message in messages: - await shelve_light.handle_light_message(message) + reconnect_interval = 10 # [seconds] + while True: + try: + async with asyncio_mqtt.Client(hostname=server, username=username, password=password) as client: + shelve_light = ShelveLightMqtt(music_mouse_protocol, client) + await shelve_light.init() + async with client.filtered_messages("musicmouse_json/#") as messages: + await client.subscribe("musicmouse_json/#") + async for message in messages: + await shelve_light.handle_light_message(message) + except MqttError as error: + print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds') + finally: + await asyncio.sleep(reconnect_interval) if __name__ == "__main__":