diff --git a/src/zigbee2mqtt/zigbee2mqtt-adapter.ts b/src/zigbee2mqtt/zigbee2mqtt-adapter.ts index a514d4e..2763cb7 100644 --- a/src/zigbee2mqtt/zigbee2mqtt-adapter.ts +++ b/src/zigbee2mqtt/zigbee2mqtt-adapter.ts @@ -184,6 +184,7 @@ export class Zigbee2MqttAdapter extends Adapter { const device = new Zigbee2MqttDevice(this, id, deviceDefinition, client, this.prefix); this.handleDeviceAdded(device); this.deviceByFriendlyName[deviceDefinition.friendly_name as string] = device; + device.fetchValues(); } else if (debug()) { console.log(`Device ${id} already exists`); } diff --git a/src/zigbee2mqtt/zigbee2mqtt-device.ts b/src/zigbee2mqtt/zigbee2mqtt-device.ts index 3d36f6a..c90c0b3 100644 --- a/src/zigbee2mqtt/zigbee2mqtt-device.ts +++ b/src/zigbee2mqtt/zigbee2mqtt-device.ts @@ -18,7 +18,6 @@ import { Zigbee2MqttProperty, WRITE_BIT, parseType, - isReadable, parseUnit, HeatingCoolingProperty, } from './zigbee2mqtt-property'; @@ -29,7 +28,7 @@ function debug(): boolean { return DEBUG_FLAG.DEBUG_zigbee2mqtt; } -const IGNORED_PROPERTIES = ['linkquality', 'local_temperature_calibration', 'running_state']; +const IGNORED_PROPERTIES = ['linkquality', 'local_temperature_calibration']; export class Zigbee2MqttDevice extends Device { private deviceTopic: string; @@ -59,56 +58,6 @@ export class Zigbee2MqttDevice extends Device { } else { this.setTitle(`Zigbee2MQTT (${id})`); } - - const exposes = deviceDefinition?.definition?.exposes; - - if (Array.isArray(exposes)) { - const properties: Record = this.getKeys(exposes); - - if (Object.keys(properties).length > 0) { - const readTopic = `${this.deviceTopic}/get`; - const readPayload = JSON.stringify(properties); - - client.publish(readTopic, readPayload, (error) => { - if (error) { - console.warn(`Could not send ${readPayload} to ${readTopic}: ${console.error()}`); - } - }); - } - } - } - - private getKeys(exposes: Expos[]): Record { - let properties: Record = {}; - - for (const expose of exposes) { - if (expose.name) { - if (this.hasReadableProperties(expose)) { - properties[expose.name] = ''; - } - } else if (Array.isArray(expose.features)) { - properties = { - ...properties, - ...this.getKeys(expose.features), - }; - } - } - - return properties; - } - - private hasReadableProperties(expose: Expos): boolean { - if (typeof expose.access === 'number') { - return isReadable(expose.access); - } else if (Array.isArray(expose.features)) { - for (const feature of expose.features) { - if (this.hasReadableProperties(feature)) { - return true; - } - } - } - - return false; } protected detectProperties(deviceDefinition: DeviceDefinition): void { @@ -456,4 +405,35 @@ export class Zigbee2MqttDevice extends Device { }); }); } + + fetchValues(): void { + const { properties } = (this as unknown) as { + properties: Map>; + }; + + const payload: Record = {}; + + for (const property of properties.values()) { + if (property.isReadable()) { + payload[property.getName()] = ''; + } + } + + if (Object.keys(payload).length > 0) { + const readTopic = `${this.deviceTopic}/get`; + const readPayload = JSON.stringify(payload); + + if (debug()) { + console.log(`Sending ${readPayload} to ${readTopic}`); + } + + this.client.publish(readTopic, readPayload, (error) => { + if (error) { + console.warn(`Could not send ${readPayload} to ${readTopic}: ${console.error()}`); + } + }); + } else if (debug()) { + console.log(`${this.getTitle()} has no readable properties`); + } + } } diff --git a/src/zigbee2mqtt/zigbee2mqtt-property.ts b/src/zigbee2mqtt/zigbee2mqtt-property.ts index b2c270b..490af13 100644 --- a/src/zigbee2mqtt/zigbee2mqtt-property.ts +++ b/src/zigbee2mqtt/zigbee2mqtt-property.ts @@ -64,7 +64,7 @@ export class Zigbee2MqttProperty extends Property { constructor( device: Zigbee2MqttDevice, name: string, - expose: Expos, + protected expose: Expos, private client: mqtt.Client, private deviceTopic: string, additionalProperties?: PropertySchema @@ -154,6 +154,11 @@ export class Zigbee2MqttProperty extends Property { } } + isReadable(): boolean { + console.log(`${this.getName()} ${this.expose.access} ${isReadable(this.expose.access ?? 0)}`); + return isReadable(this.expose.access ?? 0); + } + update(value: unknown, _: Record): void { this.setCachedValueAndNotify(value as T); } @@ -213,7 +218,7 @@ export class BrightnessProperty extends Zigbee2MqttProperty { constructor( device: Zigbee2MqttDevice, name: string, - private expose: Expos, + expose: Expos, client: mqtt.Client, deviceTopic: string ) { @@ -395,7 +400,7 @@ export class HeatingCoolingProperty extends Zigbee2MqttProperty { }); } - update(value: string): void { - this.setCachedValueAndNotify(convertHeatingCoolingValue(value)); + update(value: string, update: Record): void { + super.update(convertHeatingCoolingValue(value), update); } }