Skip to content

Commit

Permalink
Fix incorrect handling of openState in Contact Sensor (#1061)
Browse files Browse the repository at this point in the history
## ♻️ Current situation

The Contact Sensor has three states: `open`, `close`, and
`timeOutNotClose`[^1]. `timeOutNotClose` indicating that the door/window
has been open for an extended period. However, in the current
implementation, the `timeOutNotClose` state is not handled correctly,
causing the state to be incorrectly updated to `close`.

## 💡 Proposed solution

I modified the handling so that `timeOutNotClose` behaves the same as
`open` state, and I created a private function for shared handling to
prevent future bugs.

## ⚙️ Release Notes

- Fix incorrect handling of openState in Contact Sensor

## ➕ Additional Information

### Testing

### Reviewer Nudging

[^1]:
https://github.com/OpenWonderLabs/SwitchBotAPI/tree/main?tab=readme-ov-file#contact-sensor-1

Co-authored-by: Donavan Becker <beckersmarthome@icloud.com>
  • Loading branch information
nzws and donavanbecker committed Sep 26, 2024
1 parent fd7b028 commit da8b745
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/device/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ export class Contact extends deviceBase {
async BLEparseStatus(): Promise<void> {
await this.debugLog('BLEparseStatus')
// ContactSensorState
this.ContactSensor.ContactSensorState = this.serviceData.doorState === 'open'
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
this.ContactSensor.ContactSensorState = this.getContactSensorState(this.serviceData.doorState)
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)

// MotionDetected
Expand Down Expand Up @@ -222,9 +220,7 @@ export class Contact extends deviceBase {
async openAPIparseStatus(): Promise<void> {
await this.debugLog('openAPIparseStatus')
// Contact State
this.ContactSensor.ContactSensorState = this.deviceStatus.openState === 'open'
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
this.ContactSensor.ContactSensorState = this.getContactSensorState(this.deviceStatus.openState)
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)

// MotionDetected
Expand Down Expand Up @@ -268,7 +264,7 @@ export class Contact extends deviceBase {
await this.debugLog('parseStatusWebhook')
await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${this.webhookContext.detectionState}, ${this.webhookContext.brightness}, ${this.webhookContext.openState}), current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel}, ${this.ContactSensor.ContactSensorState})`)
// ContactSensorState
this.ContactSensor.ContactSensorState = this.webhookContext.openState === 'open' ? 1 : 0
this.ContactSensor.ContactSensorState = this.getContactSensorState(this.webhookContext.openState)
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
// MotionDetected
Expand Down Expand Up @@ -442,4 +438,10 @@ export class Contact extends deviceBase {
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e)
}

private getContactSensorState(openState: string): CharacteristicValue {
return openState === 'open' || openState === 'timeOutNotClose'
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
}
}

0 comments on commit da8b745

Please sign in to comment.