From c07b1dbabd5861ccb710df9da6aadf1f052d2b83 Mon Sep 17 00:00:00 2001 From: Denis-Cosmin Nutiu Date: Mon, 13 Jun 2022 13:37:49 +0300 Subject: [PATCH] Expose bme680 measurements as prometheus metrics. --- requirements.txt | 1 + sensors/main.py | 49 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/requirements.txt b/requirements.txt index 7fe8b29..f541eb4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ bme680==1.1.1 HAP-python==4.4.0 +prometheus-client==0.14.1 diff --git a/sensors/main.py b/sensors/main.py index a14079e..5833c45 100644 --- a/sensors/main.py +++ b/sensors/main.py @@ -3,6 +3,7 @@ from pyhap.accessory import Accessory, Bridge import bme680 from pyhap.accessory_driver import AccessoryDriver from pyhap.const import CATEGORY_SENSOR +from prometheus_client import start_http_server, Gauge class Bme680Sensor(Accessory): @@ -24,12 +25,27 @@ class Bme680Sensor(Accessory): self.sensor.set_temperature_oversample(bme680.OS_8X) self.sensor.set_filter(bme680.FILTER_SIZE_3) - # Add the services that this Accessory will support with add_preload_service here - temp_service = self.add_preload_service('TemperatureSensor') - humidity_service = self.add_preload_service('HumiditySensor') + self._temperature_metric = Gauge( + "bme680_tempetaure_celsius", "The temperature measurement in celsius." + ) + self._humidity_metric = Gauge( + "bme680_humidity_rh", "The humidity measurement in relative humidity." + ) + self._pressure_metric = Gauge( + "bme680_pressure_hpa", "The pressure measurement in hectopascals." + ) + self._gas_resistance_metric = Gauge( + "bme860_gas_resistance_ohm", "The gas resistance measurement in ohms." + ) - self.temp_value = temp_service.get_characteristic('CurrentTemperature') - self.humidity_value = humidity_service.get_characteristic("CurrentRelativeHumidity") + # Add the services that this Accessory will support with add_preload_service here + temp_service = self.add_preload_service("TemperatureSensor") + humidity_service = self.add_preload_service("HumiditySensor") + + self.temp_value = temp_service.get_characteristic("CurrentTemperature") + self.humidity_value = humidity_service.get_characteristic( + "CurrentRelativeHumidity" + ) @Accessory.run_at_interval(3) def run(self): @@ -43,21 +59,34 @@ class Bme680Sensor(Accessory): self.temp_value.set_value(self.sensor.data.temperature) self.humidity_value.set_value(self.sensor.data.humidity) + # Update prometheus metrics. + self._temperature_metric.set(self.sensor.data.temperature) + self._humidity_metric.set(self.sensor.data.humidity) + self._pressure_metric.set(self.sensor.data.pressure) + + if self.sensor.data.heat_stable: + self._gas_resistance_metric.set(self.sensor.data.gas_resistance) + def stop(self): """We override this method to clean up any resources or perform final actions, as this is called by the AccessoryDriver when the Accessory is being stopped. """ - print('Stopping accessory.') + print("Stopping accessory.") def get_bridge(accessory_driver): - bridge = Bridge(accessory_driver, 'Bridge') - bridge.add_accessory(Bme680Sensor(accessory_driver, 'Sensor')) + bridge = Bridge(accessory_driver, "Bridge") + bridge.add_accessory(Bme680Sensor(accessory_driver, "Sensor")) return bridge -if __name__ == '__main__': - driver = AccessoryDriver(port=51826, persist_file="/home/pi/bme680-homekit/sensors/accessory.state") +if __name__ == "__main__": + # Prometheus' metrics server + start_http_server(8000) + # Python HAP + driver = AccessoryDriver( + port=51826, persist_file="/home/pi/bme680-homekit/sensors/accessory.state" + ) driver.add_accessory(accessory=get_bridge(driver)) signal.signal(signal.SIGTERM, driver.signal_handler) driver.start()