Update bt_exporter.py

This commit is contained in:
House of Vanity
2023-10-30 22:51:07 +02:00
committed by GitHub
parent 7b176cb4a9
commit 33edeeb361

View File

@ -1,29 +1,25 @@
"""Xiaomi Mi Temperature and Humidity Monitor 2 (LYWSD03MMC) prom exporter"""
import os
import time
import requests
import binascii
import argparse
from time import sleep
from prometheus_client import start_http_server, Gauge, Enum
from bluepy import btle
class XiaoMiTemp(btle.DefaultDelegate):
def __init__(self,mac,location, temp, humid, batt):
def __init__(self, mac, location, temp, humid, batt):
btle.DefaultDelegate.__init__(self)
self.mac=mac
self.loc=location
self.mac = mac
self.loc = location
self.temp = temp
self.humid = humid
self.batt = batt
def handleNotification(self, cHandle, data):
databytes=bytearray(data)
self.temp.labels(self.mac, self.loc).set(int.from_bytes(databytes[0:2],"little")/100)
self.humid.labels(self.mac, self.loc).set(int.from_bytes(databytes[2:3],"little"))
self.batt.labels(self.mac, self.loc).set(int.from_bytes(databytes[3:5],"little")/1000)
databytes = bytearray(data)
self.temp.labels(self.mac, self.loc).set(int.from_bytes(databytes[0:2], "little") / 100)
self.humid.labels(self.mac, self.loc).set(int.from_bytes(databytes[2:3], "little"))
self.batt.labels(self.mac, self.loc).set(int.from_bytes(databytes[3:5], "little") / 1000)
class AppMetrics:
@ -44,8 +40,8 @@ class AppMetrics:
def fetch(self):
for mac, location in self.devices:
p = btle.Peripheral( )
p.setDelegate( XiaoMiTemp(mac, location, self.temperature, self.humidity, self.battery) )
p = btle.Peripheral()
p.setDelegate(XiaoMiTemp(mac, location, self.temperature, self.humidity, self.battery))
# BLE performs very poorly, constant errors are not uncommon
for attempt in range(10):
@ -58,6 +54,7 @@ class AppMetrics:
finally:
p.disconnect()
def main():
parser = argparse.ArgumentParser(
description="Xiaomi Mi Temperature and Humidity Monitor 2 (LYWSD03MMC) prom exporter")
@ -90,5 +87,6 @@ def main():
start_http_server(exporter_port)
app_metrics.run_metrics_loop()
if __name__ == "__main__":
main()