Skip to content

Commit

Permalink
Make sure values supplied to HomeKit are within range
Browse files Browse the repository at this point in the history
  • Loading branch information
alexryd committed Aug 1, 2021
1 parent fca38ce commit 0ec02ec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion abilities/battery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = homebridge => {
}

get level() {
return this.device[this._levelProperty]
return Math.min(Math.max(this.device[this._levelProperty], 0), 100)
}

get chargingState() {
Expand Down
4 changes: 4 additions & 0 deletions abilities/humidity-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = homebridge => {
humidityProperty
)
}

_valueToHomeKit(value) {
return Math.min(Math.max(value, 0), 100)
}
}

return HumiditySensorAbility
Expand Down
4 changes: 4 additions & 0 deletions abilities/light-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = homebridge => {

return service
}

_valueToHomeKit(value) {
return Math.min(Math.max(value, 0.0001), 500000)
}
}

return LightSensorAbility
Expand Down
5 changes: 4 additions & 1 deletion abilities/power-meter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ module.exports = homebridge => {
}

get consumption() {
return this.device[this._consumptionProperty]
return Math.min(
Math.max(this.device[this._consumptionProperty], 0),
65535
)
}

get electricCurrent() {
Expand Down
4 changes: 4 additions & 0 deletions abilities/temperature-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = homebridge => {

return service
}

_valueToHomeKit(value) {
return Math.min(Math.max(value, -270), 100)
}
}

return TemperatureSensorAbility
Expand Down

0 comments on commit 0ec02ec

Please sign in to comment.