Skip to content

Commit

Permalink
Handle errors thrown by active reads when an invalid app key is confi…
Browse files Browse the repository at this point in the history
…gured
  • Loading branch information
gormanb committed Aug 7, 2023
1 parent 04c718a commit 291489b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/connectorAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ export class ConnectorAccessory extends ConnectorDeviceHandler {
this.passiveReadTicker ? ReadDeviceType.kPassive :
ReadDeviceType.kActive));

// Check whether the response from the hub is valid.
if (newState && this.isInvalidAck(newState)) {
Log.error('Error received from hub. App key may be invalid:', newState);
return;
}

// Update the cached current and last-good copies of the device status.
this.lastState = (this.currentState || this.lastState);
this.currentState = newState;
Expand Down Expand Up @@ -221,12 +227,9 @@ export class ConnectorAccessory extends ConnectorDeviceHandler {
return this.client.setDeviceState(targetReq);
})();

// Check whether the ack we received is valid for the request we sent.
const invalidAck = this.isInvalidTargetAck(ack);

// If we didn't receive an ack, or if the ack reports an exception from the
// hub, or if the ack is invalid, throw a communications error to Homekit.
if (!ack || ack.actionResult || invalidAck) {
if (!ack || this.isInvalidAck(ack)) {
Log.error(
`Failed to set ${this.accessory.displayName} to ${targetVal}:`,
(ack || 'No response from hub'));
Expand Down
4 changes: 2 additions & 2 deletions src/connectorhub/connectorDeviceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export class ConnectorDeviceHandler {
}

// Check whether a targeting request response is invalid.
protected isInvalidTargetAck(ack: WriteDeviceResponse) {
protected isInvalidAck(ack: WriteDeviceResponse|ReadDeviceResponse) {
return ack &&
(!ack.data ||
(!ack.data || (<WriteDeviceAck>ack)?.actionResult ||
(!this.usesBinaryState() &&
ack.data[this.fields.currentPosition] === undefined));
}
Expand Down

0 comments on commit 291489b

Please sign in to comment.