From 74cff6a983edbb54ce04fe897d824826d370fa69 Mon Sep 17 00:00:00 2001 From: Valentin Date: Thu, 23 Dec 2021 21:18:27 +0100 Subject: [PATCH] Call onEvent when device options change (#10322) * Notify when options change * publish device info after options change * Refactor Co-authored-by: Koen Kanters --- lib/eventBus.ts | 7 +++++++ lib/extension/bridge.ts | 1 + lib/extension/onEvent.ts | 7 +++++++ lib/types/types.d.ts | 1 + 4 files changed, 16 insertions(+) diff --git a/lib/eventBus.ts b/lib/eventBus.ts index f38c9fcca..ebbc45dea 100644 --- a/lib/eventBus.ts +++ b/lib/eventBus.ts @@ -77,6 +77,13 @@ export default class EventBus { this.on('deviceJoined', callback, key); } + public emitEntityOptionsChanged(data: eventdata.EntityOptionsChanged): void { + this.emitter.emit('entityOptionsChanged', data); + } + public onEntityOptionsChanged(key: ListenerKey, callback: (data: eventdata.EntityOptionsChanged) => void): void { + this.on('entityOptionsChanged', callback, key); + } + public emitDeviceLeave(data: eventdata.DeviceLeave): void { this.emitter.emit('deviceLeave', data); } diff --git a/lib/extension/bridge.ts b/lib/extension/bridge.ts index 1a9da8278..9368ec25b 100644 --- a/lib/extension/bridge.ts +++ b/lib/extension/bridge.ts @@ -399,6 +399,7 @@ export default class Bridge extends Extension { logger.info(`Changed config for ${entityType} ${ID}`); + this.eventBus.emitEntityOptionsChanged({from: oldOptions, to: newOptions, entity}); return utils.getResponse(message, {from: oldOptions, to: newOptions, id: ID}, null); } diff --git a/lib/extension/onEvent.ts b/lib/extension/onEvent.ts index 43f6039c7..9adf03719 100644 --- a/lib/extension/onEvent.ts +++ b/lib/extension/onEvent.ts @@ -19,6 +19,13 @@ export default class OnEvent extends Extension { (data) => this.callOnEvent(data.device, 'deviceAnnounce', this.convertData(data))); this.eventBus.onDeviceNetworkAddressChanged(this, (data) => this.callOnEvent(data.device, 'deviceNetworkAddressChanged', this.convertData(data))); + this.eventBus.onEntityOptionsChanged(this, + (data) => { + if (data.entity.isDevice()) { + this.callOnEvent(data.entity, 'deviceOptionsChanged', data) + .then(() => this.eventBus.emitDevicesChanged()); + } + }); } private convertData(data: KeyValue): KeyValue { diff --git a/lib/types/types.d.ts b/lib/types/types.d.ts index bd545ca83..674adf39f 100644 --- a/lib/types/types.d.ts +++ b/lib/types/types.d.ts @@ -142,6 +142,7 @@ declare global { type DeviceAnnounce = { device: Device }; type DeviceInterview = { device: Device, status: 'started' | 'successful' | 'failed' }; type DeviceJoined = { device: Device }; + type EntityOptionsChanged = { entity: Device | Group, from: KeyValue, to: KeyValue }; type Reconfigure = { device: Device }; type DeviceLeave = { ieeeAddr: string, name: string }; type GroupMembersChanged = {group: Group, action: 'remove' | 'add' | 'remove_all',