Skip to content

Commit 8da64e3

Browse files
committed
Support for custom abnormal-CO2 threshold; and tidy up how some defaults are set to be clearer
1 parent 056f778 commit 8da64e3

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

config.schema.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
"title": "Use Compensated Values",
2727
"type": "boolean",
2828
"default": false
29+
},
30+
"co2AlertThreshold": {
31+
"type": "number",
32+
"title": "CO2 Alert Threshold",
33+
"default": 800,
34+
"description": "The CO\u2082 level (ppm) above which HomeKit shows an alert."
2935
}
3036
}
3137
}
@@ -53,10 +59,14 @@
5359
"key": "sensors[].useCompensatedValues",
5460
"type": "checkbox",
5561
"title": "Use Compensated Values"
62+
},
63+
{
64+
"key": "sensors[].co2AlertThreshold",
65+
"placeholder": 800
5666
}
5767
]
5868
}
5969
]
6070
}
6171
]
62-
}
72+
}

src/index.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface AirGradientData {
3939

4040
interface SensorConfig {
4141
serialno: string;
42+
co2AlertThreshold?: number;
4243
pollingInterval?: number;
4344
useCompensatedValues?: boolean;
4445
}
@@ -100,14 +101,23 @@ class AirGradientSensor {
100101
private readonly serviceCO2: Service;
101102
private readonly serviceHumid: Service;
102103
private readonly useCompensatedValues: boolean;
104+
private readonly co2AlertThreshold: number;
103105

104106
constructor(platform: AirGradientPlatform, accessory: PlatformAccessory, sensorConfig: SensorConfig) {
105107
this.platform = platform;
106108
this.accessory = accessory;
107109
this.log = platform.log;
108110
this.serialno = sensorConfig.serialno;
109-
this.pollingInterval = sensorConfig.pollingInterval || 60000; // Default to 1 minute
110-
this.useCompensatedValues = sensorConfig.useCompensatedValues || false;
111+
112+
this.pollingInterval = (sensorConfig.pollingInterval && sensorConfig.pollingInterval > 0)
113+
? sensorConfig.pollingInterval
114+
: 60000; // Default to 1 minute, must be higher than 0
115+
116+
this.useCompensatedValues = sensorConfig.useCompensatedValues ?? false;
117+
118+
this.co2AlertThreshold = (sensorConfig.co2AlertThreshold && sensorConfig.co2AlertThreshold > 0)
119+
? sensorConfig.co2AlertThreshold
120+
: 800;
111121

112122
// Construct the local API URL using the serialno
113123
this.apiUrl = `http://airgradient_${this.serialno}.local/measures/current`;
@@ -241,11 +251,9 @@ class AirGradientSensor {
241251
}
242252

243253
private calculateCO2Detected(co2: number): number {
244-
if (co2 <= 800) {
245-
return hap.Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL;
246-
} else {
247-
return hap.Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL;
248-
}
254+
return co2 > this.co2AlertThreshold
255+
? hap.Characteristic.CarbonDioxideDetected.CO2_LEVELS_ABNORMAL
256+
: hap.Characteristic.CarbonDioxideDetected.CO2_LEVELS_NORMAL;
249257
}
250258

251259
private handleAirQualityGet(callback: (error: Error | null, value?: number) => void) {

0 commit comments

Comments
 (0)