xiaomi2mqtt/get_data.py

102 lines
2.5 KiB
Python
Raw Normal View History

2021-06-16 07:00:43 +01:00
"""
Read Xiaomi LYWSD03MMC advertised packets and send them to MQTT
"""
import os
from datetime import datetime
2021-06-17 08:46:26 +01:00
from socket import gaierror
2021-06-16 07:00:43 +01:00
import bluetooth._bluetooth as bluez
import paho.mqtt.publish as publish
2021-06-16 16:23:24 +01:00
from bluetooth_utils.bluetooth_utils import (
disable_le_scan,
enable_le_scan,
parse_le_advertising_events,
raw_packet_to_str,
toggle_device,
)
2021-06-16 07:00:43 +01:00
def send_to_mqtt(data: dict):
"""
Send data from LYWSD03MMC to MQTT
"""
msgs = [
{
'topic': f'sensors/home/{data["mac"]}/temperature',
'payload': data['temperature'],
},
{
'topic': f'sensors/home/{data["mac"]}/humidity',
'payload': data['humidity'],
},
{
'topic': f'sensors/home/{data["mac"]}/battery',
'payload': data['battery'],
},
]
publish.multiple(msgs, hostname=os.environ.get('MQTT_HOST', 'mqtt'))
2021-06-17 08:46:26 +01:00
def le_advertise_packet_handler(
mac: str,
adv_type: int, # pylint: disable=unused-argument
data: bytes,
rssi: int, # pylint: disable=unused-argument
):
2021-06-16 07:00:43 +01:00
"""
Handle new Xiaomi LYWSD03MMC BTLE advertise packet
"""
data_str = raw_packet_to_str(data)
# Check for ATC preamble
if data_str[0:2] == '11' and data_str[6:10] == '1a18':
temp = int(data_str[22:26], 16) / 10
hum = int(data_str[26:28], 16)
batt = int(data_str[28:30], 16)
2021-06-16 16:23:24 +01:00
mqtt_data = {
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'mac': mac,
'temperature': temp,
'humidity': hum,
'battery': batt,
}
2021-06-17 08:46:26 +01:00
try:
send_to_mqtt(mqtt_data)
except gaierror as error:
print(f'[ERROR] {error}')
2021-06-16 07:00:43 +01:00
if __name__ == '__main__':
def main():
"""
Main program
"""
# Use 0 for hci0
dev_id = 0
toggle_device(dev_id, True)
try:
sock = bluez.hci_open_dev(dev_id) # pylint: disable=c-extension-no-member
except:
print(f'Cannot open bluetooth device {dev_id}')
raise
# Set filter to "True" to see only one packet per device
enable_le_scan(sock, filter_duplicates=False)
try:
# Called on new LE packet
parse_le_advertising_events(
2021-06-16 19:55:50 +01:00
sock,
handler=le_advertise_packet_handler,
debug=False,
2021-06-16 07:00:43 +01:00
)
# Scan until Ctrl-C
except KeyboardInterrupt:
disable_le_scan(sock)
main()