Retry mqtt connection on unreachable network

This commit is contained in:
Odd Stråbø 2022-03-09 23:41:37 +01:00
parent 1e41bdd2dc
commit f030b460c8

View file

@ -130,11 +130,25 @@ class MqttConsumer(BaseConsumer):
self.client.will_set(
f"{self.topic_prefix}/available", payload="offline", retain=True
)
self.client.connect(
settings["client"]["host"],
settings["client"]["port"],
settings["client"]["keepalive"],
)
while True:
try:
self.client.connect(
settings["client"]["host"],
settings["client"]["port"],
settings["client"]["keepalive"],
)
break
except OSError as err:
# Network is unreachable
if err.errno == 101:
pass
# Temporary failure in name resolution
elif err.errno == -3:
pass
else:
raise
print(err)
sleep(0.1)
def config(self, settings: Dict[str, Any]):
super().config(settings)