Summary: HAP-NodeJS has a logic error / oversight in validateUserInput in Characteristic.ts which can cause accessories to fail to add to HomeKit with 'out of compliance' errors.
To reproduce, create a service with a characteristic e.g. HeatingThresholdTemperature, set up minValue, maxValue and minStep props such that the maxValue is not a multiple of the minStep, e.g. { minValue: 0, maxValue: 25, minStep: 2 }. Call updateValue on that characteristic to 25. Try to add accessory to HomeKit. Observe Apple Home produce 'out of compliance' error and accessory fails to add. If accessory is already added, it will go to 'no response' when this happens.
The cause is that validateUserInput first quantizes to the step, and then truncates to minValue and maxValue. But this isn't the logic that HK uses: instead, if maxValue - minValue isn't a multiple of the step, maxValue is adjusted as follows:
adjustedMaxValue := stepValue * Math.floor((maxValue - minValue) / stepValue) + minValue
So in the example case, if we set { minValue: 0, maxValue: 25, minStep: 2 } HK will adjust the maxValue internally to 24, and give OOC errors if larger values are sent.
So HAP-NodeJS needs to do the same adjustment, otherwise HAP-NodeJS can produce invalid characteristic updates which cause HK to fail the bounds check, even when submitted characteristic values are within the minValue to maxValue range. Search Github and you will see people reporting plug-in issues which are caused by this bug.
Summary: HAP-NodeJS has a logic error / oversight in
validateUserInputinCharacteristic.tswhich can cause accessories to fail to add to HomeKit with 'out of compliance' errors.To reproduce, create a service with a characteristic e.g. HeatingThresholdTemperature, set up
minValue,maxValueandminStepprops such that themaxValueis not a multiple of theminStep, e.g.{ minValue: 0, maxValue: 25, minStep: 2 }. CallupdateValueon that characteristic to25. Try to add accessory to HomeKit. Observe Apple Home produce 'out of compliance' error and accessory fails to add. If accessory is already added, it will go to 'no response' when this happens.The cause is that
validateUserInputfirst quantizes to the step, and then truncates tominValueandmaxValue. But this isn't the logic that HK uses: instead, ifmaxValue - minValueisn't a multiple of the step,maxValueis adjusted as follows:adjustedMaxValue := stepValue * Math.floor((maxValue - minValue) / stepValue) + minValueSo in the example case, if we set
{ minValue: 0, maxValue: 25, minStep: 2 }HK will adjust themaxValueinternally to24, and give OOC errors if larger values are sent.So HAP-NodeJS needs to do the same adjustment, otherwise HAP-NodeJS can produce invalid characteristic updates which cause HK to fail the bounds check, even when submitted characteristic values are within the
minValuetomaxValuerange. Search Github and you will see people reporting plug-in issues which are caused by this bug.