From 0a42b37da1d5298b4d6048c29f4066e8e73b4b48 Mon Sep 17 00:00:00 2001 From: Arno Moonen Date: Sat, 4 Jan 2025 23:17:26 +0100 Subject: [PATCH] Prevent TypeError when handling JSON availability payload (#986) --- CHANGELOG.md | 4 ++++ src/platform.ts | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88708bf6..53dfda29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ Since version 1.0.0, we try to follow the [Semantic Versioning](https://semver.o - For numeric characteristics that have a range set, the range is automatically updated if an out of range value is received from Zigbee2MQTT. +### Fixed + +- Processing JSON availability payload should not result in a TypeError anymore. + ## [1.11.0-beta.7] - 2025-01-04 ### Changed diff --git a/src/platform.ts b/src/platform.ts index 688d9261..f8b3e42f 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -407,9 +407,9 @@ export class Zigbee2mqttPlatform implements DynamicPlatformPlugin { // Check if payload is a JSON object or a plain string let isAvailable = false; if (statePayload.includes('{')) { - const state = JSON.parse(statePayload).availability; - if ('state' in state) { - isAvailable = state.state === 'online'; + const json = JSON.parse(statePayload); + if (json !== undefined && 'availability' in json && json.availability !== undefined && 'state' in json.availability) { + isAvailable = json.availability.state === 'online'; } } else { isAvailable = statePayload === 'online';