Skip to content

Commit

Permalink
Merge pull request #56 from deanlyoung/awair-pm25
Browse files Browse the repository at this point in the history
Add 'awair-pm' air quality tile calculation.
  • Loading branch information
DMBlakeley authored Feb 6, 2021
2 parents 55b6cd6 + a442874 commit a8e7bc6
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to this project will be documented in this file. This project uses [Semantic Versioning](https://semver.org/).

## v5.7.3
* [New] Add `awair-pm` 'air quality method'. When 'awair-pm' selected, the HomeKit Air Quality tile only reflects the particulates value, which is useful for automating air purifiers.

## v5.7.2
* [Enhancement] Update config schema titles to provide better description and consistency across titles.
* [Enhancement] Update README.md to be consistent with config schema changes. Clarify that when upgrading from 5.6.4 to 5.7.x that you should first uninstall plug-in, reboot, reinstall, configure and reboot. This is due to change in device accessory cache format.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ Parameter | Optional? | Description
:-- | :----: | :---
`platform` | | The Homebridge Accessory (REQUIRED, must be exactly: `Awair2`)
`token` | | Developer Token (REQUIRED, see [Installation](#installation)) above.
`userType` | Y | The type of user account (Default = `users/self`, options: `users/self` or `orgs/###`, where ### is the Awair Organization `orgId`)
`airQualityMethod` | Y | Air quality calculation method used to define the Air Quality Chracteristic (Default = `awair-aqi`, options: `awair-aqi`, `awair-score` or `nowcast-aqi`)
`userType` | Y | The type of user account (Eefault = `users/self`, options: `users/self` or `orgs/###`, where ### is the Awair Organization `orgId`)
`airQualityMethod` | Y | Air quality calculation method used to define the Air Quality Chracteristic (Default = `awair-aqi`, options: `awair-aqi`, `awair-pm`, `awair-score` or `nowcast-aqi`)

`endpoint` | Y | The `/air-data/` endpoint to use (Default = `15-min-avg`, options: `15-min-avg`, `5-min-avg`, `raw` or `latest`)
`limit` | Y | Number of consecutive data points returned per request, used for custom averaging of sensor values (Default = `1` i.e. one `15-min-avg`). Defaults to 1 for `latest`.
`carbonDioxideThreshold` | Y | The level at which HomeKit will trigger an alert for the CO2 in ppm. (Default = `1000`)
Expand Down
8 changes: 4 additions & 4 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"title": "Air quality calculation method",
"type": "string",
"default": "awair-aqi",
"enum": ["awair-aqi", "awair-score", "nowcast-aqi"],
"description": "Air quality calculation method used to define the Air Quality Chracteristic. Default = 'awair-aqi', Options: 'awair-aqi', 'awair-score' and 'nowcast-aqi'. Nowcast-aqi only available for Omni, Mint, Awair, Awair-R2 and Element. When 'nowcast-api' selected, 'endpoint' defaults to '15-min-avg' and 'number of data points returned' to '48."
"enum": ["awair-aqi", "awair-pm", "awair-score", "nowcast-aqi"],
"description": "Air quality calculation method used to define the Air Quality Chracteristic. Default = 'awair-aqi', Options: 'awair-aqi', 'awair-pm', 'awair-score' and 'nowcast-aqi'. Awair-pm and Nowcast-aqi are only available for Omni, Mint, Awair, Awair-R2 and Element. When 'awair-pm' selected, the HomeKit Air Quality tile only reflects the particulates value, which is useful for automating air purifiers. When 'nowcast-api' selected, 'endpoint' defaults to '15-min-avg' and 'data points returned' to '48. The 'awair-score' method maps the Awair Score to an Air Quality value."
},
"endpoint": {
"title": "The 'air-data' endpoint",
Expand Down Expand Up @@ -79,7 +79,7 @@
"condition": {
"functionBody": "return model.occupancyDetection === true;"
}
},
},
"occupancyRestart": {
"title": "Omni Reset Occupancy Status - reinitialize Occupancy detection measurement on Homebridge reboot.",
"type": "boolean",
Expand Down Expand Up @@ -165,4 +165,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": false,
"displayName": "Homebridge Awair2",
"name": "homebridge-awair2",
"version": "5.7.2",
"version": "5.7.3",
"description": "HomeKit integration of Awair air quality monitor as Dynamic Platform.",
"main": "dist/index.js",
"scripts": {
Expand Down
65 changes: 63 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,15 +675,23 @@ class AwairPlatform implements DynamicPlatformPlugin {
if (this.airQualityMethod === 'awair-aqi') {
airQualityService
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertAwairAqi(accessory, sensors));
} else if ((this.airQualityMethod === 'awair-pm') &&
!((accessory.context.deviceType === 'awair-glow') || (accessory.context.deviceType === 'awair-glow-c'))) {
airQualityService // only use awair-pm for Omni, Mint, Awair, Awair-R2, Element
// eslint-disable-next-line max-len
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertAwairPm(accessory, sensors)); // pass response data
} else if ((this.airQualityMethod === 'nowcast-aqi') &&
!((accessory.context.deviceType === 'awair-glow') || (accessory.context.deviceType === 'awair-glow-c'))) {
airQualityService // only use nowcast-aqi for Omni, Mint, Awair, Awair-R2
airQualityService // only use nowcast-aqi for Omni, Mint, Awair, Awair-R2, Element
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertNowcastAqi(accessory, data)); // pass response data
} else if ((this.airQualityMethod === 'nowcast-aqi')
&& (accessory.context.deviceType === 'awair-glow' || accessory.context.deviceType === 'awair-glow-c')) {
airQualityService // for Glow or Glow-C use awair-aqi if nowcast-aqi selected
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertAwairAqi(accessory, sensors));
} else {
} else if (this.airQualityMethod === 'awair-score') {
airQualityService
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertScore(score));
} else {
airQualityService
.updateCharacteristic(hap.Characteristic.AirQuality, this.convertScore(score));
}
Expand Down Expand Up @@ -1434,6 +1442,59 @@ class AwairPlatform implements DynamicPlatformPlugin {
return Math.max(...aqiArray); // aqi is maximum value of voc, pm25 and dust
}

convertAwairPm(accessory: PlatformAccessory, sensors: any[]): number {
const aqiArray = [];
for (const sensor in sensors) {
switch (sensor) {
case 'pm25':
let aqiPm25 = parseFloat(sensors[sensor]);
if (aqiPm25 >= 0 && aqiPm25 < 15) {
aqiPm25 = 1; // EXCELLENT
} else if (aqiPm25 >= 15 && aqiPm25 < 35) {
aqiPm25 = 2; // GOOD
} else if (aqiPm25 >= 35 && aqiPm25 < 55) {
aqiPm25 = 3; // FAIR
} else if (aqiPm25 >= 55 && aqiPm25 < 75) {
aqiPm25 = 4; // INFERIOR
} else if (aqiPm25 >= 75) {
aqiPm25 = 5; // POOR
} else {
aqiPm25 = 0; // Error
}
aqiArray.push(aqiPm25);
break;
case 'dust':
let aqiDust = parseFloat(sensors[sensor]);
if (aqiDust >= 0 && aqiDust < 50) {
aqiDust = 1; // EXCELLENT
} else if (aqiDust >= 50 && aqiDust < 100) {
aqiDust = 2; // GOOD
} else if (aqiDust >= 100 && aqiDust < 150) {
aqiDust = 3; // FAIR
} else if (aqiDust >= 150 && aqiDust < 250) {
aqiDust = 4; // INFERIOR
} else if (aqiDust >= 250) {
aqiDust = 5; // POOR
} else {
aqiDust = 0; // Error
}
aqiArray.push(aqiDust);
break;
default:
if(this.config.logging && this.config.verbose){
this.log(`[${accessory.context.serial}] convertAwairAqi ignoring ${JSON.stringify(sensor)}: ${parseFloat(sensors[sensor])}`);
}
aqiArray.push(0);
break;
}
}
if(this.config.logging && this.config.verbose){
this.log(`[${accessory.context.serial}] aqi array: ${JSON.stringify(aqiArray)}`);
}
// aqi is maximum value of pm25 and dust, leaving the implementation flexible for additional PM parameters
return Math.max(...aqiArray);
}

convertNowcastAqi(accessory: PlatformAccessory, data: any[]): number {
const pmRawData: number[] = data
.map(sensor => sensor.sensors) // create sensor array of sensors with length 'this.limit'
Expand Down

0 comments on commit a8e7bc6

Please sign in to comment.