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

Fixed a regression where floats were rounded incorrectly when no minValue was provided #972

Merged
merged 3 commits into from
Sep 19, 2022
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
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