Skip to content

Commit

Permalink
Merge pull request #305 from tim-hellhake/fetch-all-initial-values
Browse files Browse the repository at this point in the history
Fetch all initial values including running_state
  • Loading branch information
tim-hellhake authored Mar 19, 2021
2 parents a6a3241 + 9598afc commit 3a859da
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/zigbee2mqtt/zigbee2mqtt-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down
84 changes: 32 additions & 52 deletions src/zigbee2mqtt/zigbee2mqtt-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
Zigbee2MqttProperty,
WRITE_BIT,
parseType,
isReadable,
parseUnit,
HeatingCoolingProperty,
} from './zigbee2mqtt-property';
Expand All @@ -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;
Expand Down Expand Up @@ -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<string, unknown> = 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<string, unknown> {
let properties: Record<string, unknown> = {};

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 {
Expand Down Expand Up @@ -456,4 +405,35 @@ export class Zigbee2MqttDevice extends Device {
});
});
}

fetchValues(): void {
const { properties } = (this as unknown) as {
properties: Map<string, Zigbee2MqttProperty<PropertyValue>>;
};

const payload: Record<string, string> = {};

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`);
}
}
}
13 changes: 9 additions & 4 deletions src/zigbee2mqtt/zigbee2mqtt-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class Zigbee2MqttProperty<T extends PropertyValue> extends Property<T> {
constructor(
device: Zigbee2MqttDevice,
name: string,
expose: Expos,
protected expose: Expos,
private client: mqtt.Client,
private deviceTopic: string,
additionalProperties?: PropertySchema
Expand Down Expand Up @@ -154,6 +154,11 @@ export class Zigbee2MqttProperty<T extends PropertyValue> extends Property<T> {
}
}

isReadable(): boolean {
console.log(`${this.getName()} ${this.expose.access} ${isReadable(this.expose.access ?? 0)}`);
return isReadable(this.expose.access ?? 0);
}

update(value: unknown, _: Record<string, unknown>): void {
this.setCachedValueAndNotify(value as T);
}
Expand Down Expand Up @@ -213,7 +218,7 @@ export class BrightnessProperty extends Zigbee2MqttProperty<number> {
constructor(
device: Zigbee2MqttDevice,
name: string,
private expose: Expos,
expose: Expos,
client: mqtt.Client,
deviceTopic: string
) {
Expand Down Expand Up @@ -395,7 +400,7 @@ export class HeatingCoolingProperty extends Zigbee2MqttProperty<string> {
});
}

update(value: string): void {
this.setCachedValueAndNotify(convertHeatingCoolingValue(value));
update(value: string, update: Record<string, unknown>): void {
super.update(convertHeatingCoolingValue(value), update);
}
}

0 comments on commit 3a859da

Please sign in to comment.