Catch socket.gairerror

This commit is contained in:
Scott Wallace 2021-06-17 08:46:26 +01:00
parent b8859717c5
commit a921c3b88f
Signed by: scott
GPG key ID: AA742FDC5AFE2A72

View file

@ -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__':