Skip to content

Commit

Permalink
Fixed a regression where floats were rounded incorrectly when no minV…
Browse files Browse the repository at this point in the history
…alue was provided (fixes #971) (#972)
  • Loading branch information
Supereg authored Sep 19, 2022
1 parent a5f134b commit 63bc589
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/lib/Characteristic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,22 @@ describe("Characteristic", () => {
expect(characteristic.value).toEqual(0.1);
});

it("should accept Formats.FLOAT with non-defined min/max value", () => {
const characteristic = createCharacteristicWithProps({
format: Formats.FLOAT,
minStep: 0.01,
perms: [Perms.PAIRED_READ, Perms.NOTIFY],
}, uuid.generate("051"));

// @ts-expect-error - spying on private property
const mock = jest.spyOn(characteristic, "characteristicWarning");

mock.mockReset();
characteristic.updateValue(0.09);
expect(characteristic.value).toEqual(0.09);
expect(mock).toBeCalledTimes(0);
});

it("should validate Formats.FLOAT with precision", () => {
const characteristic = new Characteristic.CurrentAmbientLightLevel();

Expand Down
10 changes: 5 additions & 5 deletions src/lib/Characteristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,19 +602,19 @@ function extractHAPStatusFromError(error: Error) {
}

function maxWithUndefined(a?: number, b?: number): number | undefined {
if (a === undefined) {
if (a == null) {
return b;
} else if (b === undefined) {
} else if (b == null) {
return a;
} else {
return Math.max(a, b);
}
}

function minWithUndefined(a?: number, b?: number): number | undefined {
if (a === undefined) {
if (a == null) {
return b;
} else if (b === undefined) {
} else if (b == null) {
return a;
} else {
return Math.min(a, b);
Expand Down Expand Up @@ -2042,7 +2042,7 @@ export class Characteristic extends EventEmitter {
}

if (stepValue != null && stepValue > 0) {
const minValue = numericMin != null ? numericMin : 0;
const minValue = this.props.minValue != null ? this.props.minValue : 0;
value = stepValue * Math.round((value - minValue) / stepValue) + minValue;
}

Expand Down

0 comments on commit 63bc589

Please sign in to comment.