Skip to content

Logic error in validateUserInput function causes 'out of compliance' HomeKit errors #1077

@adriancable

Description

@adriancable

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions