Skip to content

Commit

Permalink
Improved some typing. Only assignIds on main accessory
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Oct 12, 2020
1 parent c8b5791 commit 9b30452
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/internal-types.ts
Expand Up @@ -40,7 +40,7 @@ export interface CharacteristicJsonObject {
maxLen?: number,
maxDataLen?: number,
"valid-values"?: number[],
"valid-values-range"?: [number, number],
"valid-values-range"?: [min: number, max: number],
}

export interface ServiceJsonObject {
Expand Down
10 changes: 6 additions & 4 deletions src/lib/Accessory.ts
Expand Up @@ -967,8 +967,10 @@ export class Accessory extends EventEmitter {
/**
* Returns a JSON representation of this accessory without characteristic values.
*/
private internalHAPRepresentation(): AccessoryJsonObject[] {
this._assignIDs(this._identifierCache!); // make sure our aid/iid's are all assigned
private internalHAPRepresentation(assignIds: boolean = true): AccessoryJsonObject[] {
if (assignIds) {
this._assignIDs(this._identifierCache!); // make sure our aid/iid's are all assigned
}
assert(this.aid, "aid cannot be undefined for accessory '" + this.displayName + "'");
assert(this.services.length, "accessory '" + this.displayName + "' does not have any services!");

Expand All @@ -981,7 +983,7 @@ export class Accessory extends EventEmitter {

if (!this.bridged) {
for (const accessory of this.bridgedAccessories) {
accessories.push(accessory.internalHAPRepresentation()[0]);
accessories.push(accessory.internalHAPRepresentation(false)[0]);
}
}

Expand Down Expand Up @@ -1077,7 +1079,7 @@ export class Accessory extends EventEmitter {
// get our accessory information in HAP format and determine if our configuration (that is, our
// Accessories/Services/Characteristics) has changed since the last time we were published. make
// sure to omit actual values since these are not part of the "configuration".
const config = this.internalHAPRepresentation(); // TODO ensure this stuff is ordered
const config = this.internalHAPRepresentation(false); // TODO ensure this stuff is ordered
this._accessoryInfo.checkForCurrentConfigurationNumberIncrement(config, true);

this.validateAccessory(true);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/Characteristic.ts
Expand Up @@ -132,7 +132,7 @@ export interface CharacteristicProps {
* Two element array where the first value specifies the lowest valid value and
* the second element specifies the highest valid value.
*/
validValueRanges?: [number, number];
validValueRanges?: [min: number, max: number];
adminOnlyAccess?: Access[];
}

Expand Down Expand Up @@ -582,7 +582,7 @@ export class Characteristic extends EventEmitter {
this.props.format = props.format;
}
if (props.perms) {
assert(props.perms, "characteristic prop perms cannot be empty array");
assert(props.perms.length, "characteristic prop perms cannot be empty array");
this.props.perms = props.perms;
}
if (props.unit !== undefined) {
Expand Down Expand Up @@ -1008,15 +1008,15 @@ export class Characteristic extends EventEmitter {
case Formats.ARRAY:
return [];
default:
return this.props.minValue || 0;
return this.props.minValue ?? 0;
}
}

private roundNumericValue(value: number): number {
if (!this.props.minStep) {
return Math.round(value); // round to 0 decimal places
}
const base = this.props.minValue || 0;
const base = this.props.minValue ?? 0;
const times = ((value - base) / this.props.minStep); // this value could become very large, so this doesn't really support little minStep values
return Math.round(times) * this.props.minStep + base;
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/util/eventedhttp.ts
Expand Up @@ -357,6 +357,8 @@ export class HAPConnection extends EventEmitter {
}],
});
} else {
// TODO should a new event not remove a previous event (to support censor open -> censor closed :thinking:)
// any only remove previous events if the same value was set?
this.queuedEvents.set(eventName, {
aid: aid,
iid: iid,
Expand Down

0 comments on commit 9b30452

Please sign in to comment.