From a921c3b88f1d4a08246ba393832598c5635385aa Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Thu, 17 Jun 2021 08:46:26 +0100 Subject: [PATCH] Catch socket.gairerror --- get_data.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/get_data.py b/get_data.py index 391bace..8307c2a 100644 --- a/get_data.py +++ b/get_data.py @@ -3,6 +3,7 @@ Read Xiaomi LYWSD03MMC advertised packets and send them to MQTT """ import os from datetime import datetime +from socket import gaierror import bluetooth._bluetooth as bluez import paho.mqtt.publish as publish @@ -37,7 +38,12 @@ def send_to_mqtt(data: dict): publish.multiple(msgs, hostname=os.environ.get('MQTT_HOST', 'mqtt')) -def le_advertise_packet_handler(mac: str, adv_type: int, data: bytes, rssi: int): +def le_advertise_packet_handler( + mac: str, + adv_type: int, # pylint: disable=unused-argument + data: bytes, + rssi: int, # pylint: disable=unused-argument +): """ Handle new Xiaomi LYWSD03MMC BTLE advertise packet """ @@ -54,7 +60,10 @@ def le_advertise_packet_handler(mac: str, adv_type: int, data: bytes, rssi: int) 'humidity': hum, 'battery': batt, } - send_to_mqtt(mqtt_data) + try: + send_to_mqtt(mqtt_data) + except gaierror as error: + print(f'[ERROR] {error}') if __name__ == '__main__':