bme680-homekit/app/main.py

52 lines
1.5 KiB
Python
Raw Normal View History

2024-02-07 19:24:03 +00:00
import logging
import pprint
import signal
from prometheus_client import start_http_server
from pyhap.accessory import Bridge
from pyhap.accessory_driver import AccessoryDriver
from app.config import Settings
from app.sensors.bme import Bme680Sensor
2024-02-08 20:49:34 +00:00
from app.sensors.pms import Pms5003Sensor
def get_bridge(accessory_driver: AccessoryDriver, settings: Settings):
"""
Returns a Homekit Bridge.
Parameters
----------
accessory_driver: AccessoryDriver
The accessory driver.
"""
bridge = Bridge(accessory_driver, settings.hap.bridge.display_name)
if settings.hap.bridge.bme680.enabled:
bridge.add_accessory(
Bme680Sensor(
2024-02-08 20:49:34 +00:00
accessory_driver, settings=settings
)
)
2024-02-08 20:49:34 +00:00
if settings.hap.bridge.pms5003.enabled:
bridge.add_accessory(
Pms5003Sensor(accessory_driver, settings=settings)
)
return bridge
2024-02-07 19:51:42 +00:00
if __name__ == "__main__":
2024-02-07 19:24:03 +00:00
logging.basicConfig(level="INFO")
settings = Settings()
2024-02-07 19:24:03 +00:00
logging.info("Running with settings:")
logging.info(pprint.pformat(settings.model_dump()))
# Start prometheus metrics server. Any metrics will be registered automatically.
if settings.prometheus.enabled:
start_http_server(settings.prometheus.port)
# Python HAP
driver = AccessoryDriver(
port=settings.hap.port, persist_file=settings.hap.persist_file
)
driver.add_accessory(accessory=get_bridge(driver, settings))
signal.signal(signal.SIGTERM, driver.signal_handler)
2024-02-07 19:51:42 +00:00
driver.start()