Skip to content

Commit f55d1c9

Browse files
Fix: Add null safety for doorState property to prevent crash with Yale Assure Lock 2 (#165)
The plugin was crashing with `Cannot read properties of undefined (reading 'includes')` when used with Yale Assure Lock 2 devices. The issue occurred because the lock's API response contains a lockStatus object like `{"status":"unknown"}` without a `doorState` property, but the code assumed this property would always exist. The crash happened in the `parseStatus` method when trying to call `.includes()` on the undefined `doorState`: ```typescript // This would crash when doorState is undefined this.lockStatus.doorState.includes('open') this.lockStatus.doorState.includes('closed') ``` The fix adds optional chaining to safely handle cases where `doorState` is undefined: ```typescript // Safe version that returns undefined instead of crashing this.lockStatus.doorState?.includes('open') this.lockStatus.doorState?.includes('closed') ``` This minimal change: - Prevents the crash by gracefully handling undefined `doorState` - Maintains backward compatibility for locks that do provide `doorState` - Preserves all existing functionality and logic flow - Allows the plugin to continue reading battery status and other lock information The contact sensor functionality will fall back to the existing `ContactSensorState` when `doorState` is unavailable, ensuring the plugin remains functional even without door state information. Fixes #127. <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: donavanbecker <[email protected]> Co-authored-by: Donavan Becker <[email protected]>
1 parent f0f6ece commit f55d1c9

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/devices/lock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ export class LockMechanism extends deviceBase {
208208
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
209209
: this.lockStatus.state.closed
210210
? this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
211-
: this.lockStatus.doorState.includes('open')
211+
: this.lockStatus.doorState?.includes('open')
212212
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
213-
: this.lockStatus.doorState.includes('closed')
213+
: this.lockStatus.doorState?.includes('closed')
214214
? this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
215215
: this.ContactSensor.ContactSensorState
216216
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)

0 commit comments

Comments
 (0)