From 7f1f1497c68edd2adab0aefae793149d4efcb9dc Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Sun, 8 May 2022 09:58:30 +0200 Subject: [PATCH] Always return a device state --- lib/extension/availability.ts | 2 +- lib/extension/frontend.ts | 6 +----- lib/extension/groups.ts | 2 +- lib/extension/onEvent.ts | 2 +- lib/extension/otaUpdate.ts | 4 ++-- lib/extension/publish.ts | 2 +- lib/state.ts | 2 +- 7 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/extension/availability.ts b/lib/extension/availability.ts index e684dd3e..368fdabf 100644 --- a/lib/extension/availability.ts +++ b/lib/extension/availability.ts @@ -200,7 +200,7 @@ export default class Availability extends Extension { if (item.condition && this.state.get(device) && !item.condition(this.state.get(device))) continue; const converter = device.definition.toZigbee.find((c) => c.key.find((k) => item.keys.includes(k))); await converter?.convertGet?.(device.endpoint(), item.keys[0], - {message: this.state.get(device) || {}, mapped: device.definition}) + {message: this.state.get(device), mapped: device.definition}) .catch((e) => { logger.error(`Failed to read state of '${device.name}' after reconnect (${e.message})`); }); diff --git a/lib/extension/frontend.ts b/lib/extension/frontend.ts index bb74637f..a0089735 100644 --- a/lib/extension/frontend.ts +++ b/lib/extension/frontend.ts @@ -100,11 +100,7 @@ export default class Frontend extends Extension { } for (const device of this.zigbee.devices(false)) { - let payload: KeyValue = {}; - if (this.state.exists(device)) { - payload = {...payload, ...this.state.get(device)}; - } - + const payload = this.state.get(device); const lastSeen = settings.get().advanced.last_seen; /* istanbul ignore if */ if (lastSeen !== 'disable') { diff --git a/lib/extension/groups.ts b/lib/extension/groups.ts index d2ebe118..c1c140e3 100644 --- a/lib/extension/groups.ts +++ b/lib/extension/groups.ts @@ -181,7 +181,7 @@ export default class Groups extends Extension { const device = this.zigbee.resolveEntity(member.getDevice()); if (this.state.exists(device)) { const state = this.state.get(device); - if (state && state.state === 'ON') { + if (state.state === 'ON') { return false; } } diff --git a/lib/extension/onEvent.ts b/lib/extension/onEvent.ts index 26c3e818..f82b8023 100644 --- a/lib/extension/onEvent.ts +++ b/lib/extension/onEvent.ts @@ -40,7 +40,7 @@ export default class OnEvent extends Extension { } private async callOnEvent(device: Device, type: string, data: KeyValue): Promise { - const state = this.state.get(device) || {}; + const state = this.state.get(device); zhc.onEvent(type, data, device.zh, device.options, state); if (device.definition?.onEvent) { diff --git a/lib/extension/otaUpdate.ts b/lib/extension/otaUpdate.ts index 65bce1c9..c396c13f 100644 --- a/lib/extension/otaUpdate.ts +++ b/lib/extension/otaUpdate.ts @@ -65,8 +65,8 @@ export default class OTAUpdate extends Extension { } private removeProgressAndRemainingFromState(device: Device): void { - delete this.state.get(device)?.update?.progress; - delete this.state.get(device)?.update?.remaining; + delete this.state.get(device).update?.progress; + delete this.state.get(device).update?.remaining; } @bind private async onZigbeeEvent(data: eventdata.DeviceMessage): Promise { diff --git a/lib/extension/publish.ts b/lib/extension/publish.ts index 9a61f970..d9896665 100644 --- a/lib/extension/publish.ts +++ b/lib/extension/publish.ts @@ -135,7 +135,7 @@ export default class Publish extends Extension { } const device = re instanceof Device ? re.zh : null; const entitySettings = re.options; - const entityState = this.state.get(re) || {}; + const entityState = this.state.get(re); const membersState = re instanceof Group ? Object.fromEntries(re.zh.members.map((e) => [e.getDevice().ieeeAddr, this.state.get(this.zigbee.resolveEntity(e.getDevice().ieeeAddr))])) : null; diff --git a/lib/state.ts b/lib/state.ts index 427bf67e..2b3e9e87 100644 --- a/lib/state.ts +++ b/lib/state.ts @@ -68,7 +68,7 @@ class State { } get(entity: Group | Device): KeyValue { - return this.state[entity.ID]; + return this.state[entity.ID] || {}; } set(entity: Group | Device, update: KeyValue, reason: string=null): KeyValue {