Skip to content

Commit

Permalink
✨ Avoid flapping alerts, decrease update interval
Browse files Browse the repository at this point in the history
jerr0328 committed Oct 18, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 55d2241 commit b454f74
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions co2mini/homekit.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,12 @@
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import CATEGORY_SENSOR

# PPM at which to trigger alert
CO2_ALERT_THRESHOLD = 1200
# PPM at which to clear alert (set lower to avoid flapping alerts)
CO2_ALERT_CLEAR_THRESHOLD = 1100
# Seconds between updates to homekit
UPDATE_INTERVAL_SECONDS = 60


class CO2Sensor(Accessory):
@@ -15,6 +20,7 @@ class CO2Sensor(Accessory):
def __init__(self, co2meter, *args, **kwargs):
super().__init__(*args, **kwargs)
self.co2meter = co2meter
self.alert = False

serv_temp = self.add_preload_service("TemperatureSensor")
serv_co2 = self.add_preload_service(
@@ -24,15 +30,16 @@ def __init__(self, co2meter, *args, **kwargs):
self.char_co2_detected = serv_co2.configure_char("CarbonDioxideDetected")
self.char_co2 = serv_co2.configure_char("CarbonDioxideLevel")

@Accessory.run_at_interval(3)
@Accessory.run_at_interval(UPDATE_INTERVAL_SECONDS)
async def run(self):
values = self.co2meter.get_data()
if "temperature" in values:
self.char_temp.set_value(values["temperature"])
if "co2" in values:
self.char_co2.set_value(values["co2"])
co2_detected = 1 if values["co2"] >= CO2_ALERT_THRESHOLD else 0
self.char_co2_detected.set_value(co2_detected)
threshold = CO2_ALERT_CLEAR_THRESHOLD if self.alert else CO2_ALERT_THRESHOLD
self.alert = values["co2"] >= threshold
self.char_co2_detected.set_value(1 if self.alert else 0)

async def stop(self):
self.co2meter.running = False

0 comments on commit b454f74

Please sign in to comment.