diff --git a/src/connectorAccessory.ts b/src/connectorAccessory.ts index 1d94838..f295b7a 100644 --- a/src/connectorAccessory.ts +++ b/src/connectorAccessory.ts @@ -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; @@ -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')); diff --git a/src/connectorhub/connectorDeviceHandler.ts b/src/connectorhub/connectorDeviceHandler.ts index d270bae..c4722f3 100644 --- a/src/connectorhub/connectorDeviceHandler.ts +++ b/src/connectorhub/connectorDeviceHandler.ts @@ -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 || (ack)?.actionResult || (!this.usesBinaryState() && ack.data[this.fields.currentPosition] === undefined)); }