Handle IOError in main.py

This commit is contained in:
Denis-Cosmin Nutiu 2023-04-25 21:31:17 +03:00
parent 03183353c1
commit 37d55a3943

View file

@ -1,10 +1,21 @@
import logging
import os
import signal
from pyhap.accessory import Accessory, Bridge
import sys
import bme680
from prometheus_client import Gauge, start_http_server
from pyhap.accessory import Accessory, Bridge
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import CATEGORY_SENSOR
from prometheus_client import start_http_server, Gauge
def fail(message: str):
"""
Fail crashes the program and logs the message.
"""
logging.critical(message)
sys.exit(1)
class Bme680Sensor(Accessory):
@ -54,14 +65,7 @@ class Bme680Sensor(Accessory):
"CurrentRelativeHumidity"
)
@Accessory.run_at_interval(30)
def run(self):
"""We override this method to implement what the accessory will do when it is
started.
We set the current temperature to a random number. The decorator runs this method
every 3 seconds.
"""
def _run(self):
if self.sensor.get_sensor_data():
self.temp_value.set_value(self.sensor.data.temperature)
self.humidity_value.set_value(self.sensor.data.humidity)
@ -74,6 +78,20 @@ class Bme680Sensor(Accessory):
if self.sensor.data.heat_stable:
self._gas_resistance_metric.set(self.sensor.data.gas_resistance)
@Accessory.run_at_interval(30)
def run(self):
"""We override this method to implement what the accessory will do when it is
started.
We set the current temperature to a random number. The decorator runs this method
every 3 seconds.
"""
try:
self._run()
except IOError as e:
# This happens from time to time, best we stop and let systemd restart us.
fail(f"Failed due to IOError: {e}")
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.