Skip to content

Commit 3a859da

Browse files
authored
Merge pull request #305 from tim-hellhake/fetch-all-initial-values
Fetch all initial values including running_state
2 parents a6a3241 + 9598afc commit 3a859da

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

src/zigbee2mqtt/zigbee2mqtt-adapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ export class Zigbee2MqttAdapter extends Adapter {
184184
const device = new Zigbee2MqttDevice(this, id, deviceDefinition, client, this.prefix);
185185
this.handleDeviceAdded(device);
186186
this.deviceByFriendlyName[deviceDefinition.friendly_name as string] = device;
187+
device.fetchValues();
187188
} else if (debug()) {
188189
console.log(`Device ${id} already exists`);
189190
}

src/zigbee2mqtt/zigbee2mqtt-device.ts

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
Zigbee2MqttProperty,
1919
WRITE_BIT,
2020
parseType,
21-
isReadable,
2221
parseUnit,
2322
HeatingCoolingProperty,
2423
} from './zigbee2mqtt-property';
@@ -29,7 +28,7 @@ function debug(): boolean {
2928
return DEBUG_FLAG.DEBUG_zigbee2mqtt;
3029
}
3130

32-
const IGNORED_PROPERTIES = ['linkquality', 'local_temperature_calibration', 'running_state'];
31+
const IGNORED_PROPERTIES = ['linkquality', 'local_temperature_calibration'];
3332

3433
export class Zigbee2MqttDevice extends Device {
3534
private deviceTopic: string;
@@ -59,56 +58,6 @@ export class Zigbee2MqttDevice extends Device {
5958
} else {
6059
this.setTitle(`Zigbee2MQTT (${id})`);
6160
}
62-
63-
const exposes = deviceDefinition?.definition?.exposes;
64-
65-
if (Array.isArray(exposes)) {
66-
const properties: Record<string, unknown> = this.getKeys(exposes);
67-
68-
if (Object.keys(properties).length > 0) {
69-
const readTopic = `${this.deviceTopic}/get`;
70-
const readPayload = JSON.stringify(properties);
71-
72-
client.publish(readTopic, readPayload, (error) => {
73-
if (error) {
74-
console.warn(`Could not send ${readPayload} to ${readTopic}: ${console.error()}`);
75-
}
76-
});
77-
}
78-
}
79-
}
80-
81-
private getKeys(exposes: Expos[]): Record<string, unknown> {
82-
let properties: Record<string, unknown> = {};
83-
84-
for (const expose of exposes) {
85-
if (expose.name) {
86-
if (this.hasReadableProperties(expose)) {
87-
properties[expose.name] = '';
88-
}
89-
} else if (Array.isArray(expose.features)) {
90-
properties = {
91-
...properties,
92-
...this.getKeys(expose.features),
93-
};
94-
}
95-
}
96-
97-
return properties;
98-
}
99-
100-
private hasReadableProperties(expose: Expos): boolean {
101-
if (typeof expose.access === 'number') {
102-
return isReadable(expose.access);
103-
} else if (Array.isArray(expose.features)) {
104-
for (const feature of expose.features) {
105-
if (this.hasReadableProperties(feature)) {
106-
return true;
107-
}
108-
}
109-
}
110-
111-
return false;
11261
}
11362

11463
protected detectProperties(deviceDefinition: DeviceDefinition): void {
@@ -456,4 +405,35 @@ export class Zigbee2MqttDevice extends Device {
456405
});
457406
});
458407
}
408+
409+
fetchValues(): void {
410+
const { properties } = (this as unknown) as {
411+
properties: Map<string, Zigbee2MqttProperty<PropertyValue>>;
412+
};
413+
414+
const payload: Record<string, string> = {};
415+
416+
for (const property of properties.values()) {
417+
if (property.isReadable()) {
418+
payload[property.getName()] = '';
419+
}
420+
}
421+
422+
if (Object.keys(payload).length > 0) {
423+
const readTopic = `${this.deviceTopic}/get`;
424+
const readPayload = JSON.stringify(payload);
425+
426+
if (debug()) {
427+
console.log(`Sending ${readPayload} to ${readTopic}`);
428+
}
429+
430+
this.client.publish(readTopic, readPayload, (error) => {
431+
if (error) {
432+
console.warn(`Could not send ${readPayload} to ${readTopic}: ${console.error()}`);
433+
}
434+
});
435+
} else if (debug()) {
436+
console.log(`${this.getTitle()} has no readable properties`);
437+
}
438+
}
459439
}

src/zigbee2mqtt/zigbee2mqtt-property.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class Zigbee2MqttProperty<T extends PropertyValue> extends Property<T> {
6464
constructor(
6565
device: Zigbee2MqttDevice,
6666
name: string,
67-
expose: Expos,
67+
protected expose: Expos,
6868
private client: mqtt.Client,
6969
private deviceTopic: string,
7070
additionalProperties?: PropertySchema
@@ -154,6 +154,11 @@ export class Zigbee2MqttProperty<T extends PropertyValue> extends Property<T> {
154154
}
155155
}
156156

157+
isReadable(): boolean {
158+
console.log(`${this.getName()} ${this.expose.access} ${isReadable(this.expose.access ?? 0)}`);
159+
return isReadable(this.expose.access ?? 0);
160+
}
161+
157162
update(value: unknown, _: Record<string, unknown>): void {
158163
this.setCachedValueAndNotify(value as T);
159164
}
@@ -213,7 +218,7 @@ export class BrightnessProperty extends Zigbee2MqttProperty<number> {
213218
constructor(
214219
device: Zigbee2MqttDevice,
215220
name: string,
216-
private expose: Expos,
221+
expose: Expos,
217222
client: mqtt.Client,
218223
deviceTopic: string
219224
) {
@@ -395,7 +400,7 @@ export class HeatingCoolingProperty extends Zigbee2MqttProperty<string> {
395400
});
396401
}
397402

398-
update(value: string): void {
399-
this.setCachedValueAndNotify(convertHeatingCoolingValue(value));
403+
update(value: string, update: Record<string, unknown>): void {
404+
super.update(convertHeatingCoolingValue(value), update);
400405
}
401406
}

0 commit comments

Comments
 (0)