Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Aqara TVOC Sensor Support #1020

Merged
merged 1 commit into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions lib/HueSensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ function HueSensor (accessory, id, obj) {
this.infoService = this.accessory.getInfoService(this)

let durationKey = 'duration'
let temperatureHistory = 'weather'
switch (this.obj.type) {
case 'ZGPSwitch':
case 'ZLLSwitch':
Expand Down Expand Up @@ -956,6 +957,9 @@ function HueSensor (accessory, id, obj) {
) {
// Develco smoke sensor
// Develco heat sensor
} else if (this.obj.modelid === 'lumi.airmonitor.acn01') {
// Xiaomi Aquara TVOC Sensor
temperatureHistory = 'room2'
} else {
this.log.warn(
'%s: %s: warning: unknown %s sensor %j',
Expand All @@ -971,10 +975,39 @@ function HueSensor (accessory, id, obj) {
key: 'temperature',
name: 'temperature',
unit: '°C',
history: 'weather',
history: temperatureHistory,
homekitValue: function (v) { return v ? Math.round(v / 10) / 10 : 0 }
}
break
case 'ZHAAirQuality':
if (
this.obj.manufacturername === 'LUMI' &&
this.obj.modelid === 'lumi.airmonitor.acn01'
) {
// Xiaomi Aqara airquality sensor
} else {
this.log.warn(
'%s: %s: warning: unknown %s sensor %j',
this.bridge.name, this.resource, this.obj.type, this.obj
)
}
// falls through
case 'CLIPAirQuality':
this.service = new Service.AirQualitySensor(this.name, this.subtype)
this.serviceList.push(this.service)
this.service
.addOptionalCharacteristic(Characteristic.AirQuality)
this.type = {
Characteristic: Characteristic.VOCDensity,
key: 'airqualityppb',
name: 'Air Quality',
unit: 'voc',
history: 'room2',
homekitValue: function (v) {
return v ? Math.round(v * 4.56) : 0
}
}
break
case 'ZLLLightLevel': // 2.7 - Hue Motion Sensor
case 'ZHALightLevel':
if (
Expand Down Expand Up @@ -1057,6 +1090,9 @@ function HueSensor (accessory, id, obj) {
) {
// Xiaomi Aqara weather sensor
// Xiaomi Mi temperature/humidity sensor
} else if (this.obj.modelid === 'lumi.airmonitor.acn01') {
// Xiaomi Aquara TVOC Sensor
temperatureHistory = 'room2'
} else if (
this.obj.manufacturername === 'Heiman' &&
(this.obj.modelid === 'TH-H_V15' || this.obj.modelid === 'TH-T_V15')
Expand All @@ -1077,7 +1113,7 @@ function HueSensor (accessory, id, obj) {
key: 'humidity',
name: 'humidity',
unit: '%',
history: 'weather',
history: temperatureHistory,
homekitValue: function (v) { return v ? Math.round(v / 100) : 0 }
}
break
Expand Down Expand Up @@ -1529,6 +1565,11 @@ function HueSensor (accessory, id, obj) {
this.history.entry.humidity = 0
this.history.entry.pressure = 0
break
case 'room2':
this.history.entry.temp = 0
this.history.entry.humidity = 0
this.history.entry.voc = 0
break
default:
break
}
Expand Down Expand Up @@ -1761,6 +1802,9 @@ HueSensor.prototype.checkAttr = function (attr, event) {
HueSensor.prototype.checkState = function (state, event) {
for (const key in state) {
switch (key) {
case 'airquality':
this.checkAirQuality(state.airquality)
break
case 'angle':
break
case 'battery':
Expand Down Expand Up @@ -2005,6 +2049,17 @@ HueSensor.prototype.addEntry = function (changed) {
}
}
break
case 'room2':
{
const key = this.type.key === 'airqualityppb'
? 'voc'
: (this.type.key === 'temperature' ? 'temp' : this.type.key)
this.history.entry[key] = this.hk[this.type.key]
if (changed || this.type.key !== this.history.resource.type.key) {
return
}
}
break
default:
return
}
Expand Down Expand Up @@ -2349,6 +2404,40 @@ HueSensor.prototype.checkVoltage = function (voltage) {
}
}

HueSensor.prototype.checkAirQuality = function (airquality) {
if (this.obj.state.airquality !== airquality) {
this.log.debug(
'%s: airquality changed from %j to %j', this.name,
this.obj.state.airquality, airquality
)
this.obj.state.airquality = airquality
}
const qualities = {
excellent: Characteristic.AirQuality.EXCELLENT,
good: Characteristic.AirQuality.GOOD,
moderate: Characteristic.AirQuality.FAIR,
poor: Characteristic.AirQuality.INFERIOR,
unhealthy: Characteristic.AirQuality.POOR
}

let hkAirQuality = qualities[airquality]
if (!hkAirQuality) {
hkAirQuality = Characteristic.AirQuality.UNKNOWN
}

if (this.hk.airquality !== hkAirQuality) {
if (this.hk.airquality !== undefined) {
this.log.info(
'%s: set homekit airquality from %s V to %s V', this.name,
this.hk.airquality, hkAirQuality
)
}
this.hk.airquality = hkAirQuality
this.service.getCharacteristic(Characteristic.AirQuality)
.updateValue(this.hk.airquality)
}
}

HueSensor.prototype.checkConfig = function (config) {
for (const key in config) {
switch (key) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"node": "^14.17.6"
},
"dependencies": {
"fakegato-history": "~0.6.1",
"fakegato-history": "~0.6.2",
"homebridge-lib": "~5.1.13",
"semver": "^7.3.5",
"ws": "^8.2.1",
Expand Down