Expose bme680 measurements as prometheus metrics.

This commit is contained in:
Denis-Cosmin Nutiu 2022-06-13 13:37:49 +03:00
parent 2b93c5f8d3
commit c07b1dbabd
2 changed files with 40 additions and 10 deletions

View file

@ -1,2 +1,3 @@
bme680==1.1.1
HAP-python==4.4.0
prometheus-client==0.14.1

View file

@ -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()