Skip to content

Commit

Permalink
Add Air Quality Sensor support (pm25).
Browse files Browse the repository at this point in the history
  • Loading branch information
0x5e committed Oct 23, 2022
1 parent f265a53 commit 3d5db78
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- [TemperatureSensor/HumiditySensor] Add Temperature and Humidity Sensor support (`wsdcg`).
- [LightSensor] Add Light Sensor support (`ldcg`).
- [MotionSensor] Add Motion Sensor support (`pir`).
- [AirQualitySensor] Add PM2.5 Detector support (`pm25`).

### Known issue
- `LightAccessory` may not work properly, espasially on work mode change. need more test and feedbacks.
Expand Down
2 changes: 1 addition & 1 deletion SUPPORTED_DEVICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Most category code is pinyin abbreviation of Chinese name.
| Luminance Sensor | 亮度传感器 | ldcg | Light Sensor ||
| Pressure Sensor | 压力传感器 | ylcg | | |
| Emergency Button | 紧急按钮 | sos | | |
| PM2.5 Detector | PM2.5传感器 | pm25 | | |
| PM2.5 Detector | PM2.5传感器 | pm25 | Air Quality Sensor | |
| CO Detector | CO报警传感器 | cobj | Carbon Monoxide Sensor ||
| CO2 Detector | CO2报警传感器 | co2bj | Carbon Dioxide Sensor ||
| Multi-functional Sensor | 多功能传感器 | dgnbj | | |
Expand Down
4 changes: 4 additions & 0 deletions src/accessory/AccessoryFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import SmokeSensorAccessory from './SmokeSensorAccessory';
import TemperatureHumiditySensorAccessory from './TemperatureHumiditySensorAccessory';
import LightSensorAccessory from './LightSensorAccessory';
import MotionSensorAccessory from './MotionSensorAccessory';
import AirQualitySensorAccessory from './AirQualitySensorAccessory';

import LegacyAccessoryFactory from './LegacyAccessoryFactory';

Expand Down Expand Up @@ -85,6 +86,9 @@ export default class AccessoryFactory {
case 'pir':
handler = new MotionSensorAccessory(platform, accessory);
break;
case 'pm25':
handler = new AirQualitySensorAccessory(platform, accessory);
break;
}

if (!handler) {
Expand Down
55 changes: 55 additions & 0 deletions src/accessory/AirQualitySensorAccessory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { PlatformAccessory } from 'homebridge';
import { TuyaPlatform } from '../platform';
import BaseAccessory from './BaseAccessory';

export default class AirQualitySensorAccessory extends BaseAccessory {

constructor(platform: TuyaPlatform, accessory: PlatformAccessory) {
super(platform, accessory);

const service = this.accessory.getService(this.Service.AirQualitySensor)
|| this.accessory.addService(this.Service.AirQualitySensor);

service.getCharacteristic(this.Characteristic.AirQuality)
.onGet(() => {
const status = this.device.getDeviceStatus('pm25_value');
if (status) {
let pm25 = Math.max(0, status?.value as number);
pm25 = Math.min(1000, pm25);
if (pm25 <= 50) {
return this.Characteristic.AirQuality.GOOD;
} else if (pm25 <= 100) {
return this.Characteristic.AirQuality.FAIR;
} else if (pm25 <= 200) {
return this.Characteristic.AirQuality.INFERIOR;
} else {
return this.Characteristic.AirQuality.POOR;
}
}

return this.Characteristic.AirQuality.UNKNOWN;
});

if (this.device.getDeviceStatus('pm25_value')) {
service.getCharacteristic(this.Characteristic.PM2_5Density)
.onGet(() => {
const status = this.device.getDeviceStatus('pm25_value');
let pm25 = Math.max(0, status?.value as number);
pm25 = Math.min(1000, pm25);
return pm25;
});
}

if (this.device.getDeviceStatus('pm10')) {
service.getCharacteristic(this.Characteristic.PM10Density)
.onGet(() => {
const status = this.device.getDeviceStatus('pm10');
let pm25 = Math.max(0, status?.value as number);
pm25 = Math.min(1000, pm25);
return pm25;
});
}

}

}

0 comments on commit 3d5db78

Please sign in to comment.