mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-27 21:20:03 +00:00
Use lastSeen from zigbee-herdsman.
This commit is contained in:
+14
-10
@@ -230,18 +230,18 @@ class Controller {
|
||||
}
|
||||
|
||||
async publishEntityState(IDorName, payload) {
|
||||
const entity = settings.getEntity(IDorName);
|
||||
if (!entity) {
|
||||
const entity = await this.zigbee.resolveEntity(IDorName);
|
||||
if (!entity || !entity.settings) {
|
||||
logger.error(`'${IDorName}' does not exist, skipping publish`);
|
||||
return;
|
||||
}
|
||||
|
||||
let messagePayload = {...payload};
|
||||
const currentState = this.state.exists(entity.ID) ? this.state.get(entity.ID) : {};
|
||||
const currentState = this.state.exists(entity.settings.ID) ? this.state.get(entity.settings.ID) : {};
|
||||
const newState = objectAssignDeep.noMutate(currentState, payload);
|
||||
|
||||
// Update state cache with new state.
|
||||
this.state.set(entity.ID, newState);
|
||||
this.state.set(entity.settings.ID, newState);
|
||||
|
||||
if (settings.get().advanced.cache_state) {
|
||||
// Add cached state to payload
|
||||
@@ -249,28 +249,32 @@ class Controller {
|
||||
}
|
||||
|
||||
const options = {
|
||||
retain: entity.hasOwnProperty('retain') ? entity.retain : false,
|
||||
qos: entity.hasOwnProperty('qos') ? entity.qos : 0,
|
||||
retain: entity.settings.hasOwnProperty('retain') ? entity.settings.retain : false,
|
||||
qos: entity.settings.hasOwnProperty('qos') ? entity.settings.qos : 0,
|
||||
};
|
||||
|
||||
if (entity.type === 'device' && settings.get().mqtt.include_device_information) {
|
||||
const device = await this.zigbee.getDevice({ieeeAddr: entity.ID});
|
||||
const device = await this.zigbee.getDevice({ieeeAddr: entity.device.ieeeAddr});
|
||||
const attributes = [
|
||||
'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource',
|
||||
'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID',
|
||||
];
|
||||
|
||||
messagePayload.device = {friendlyName: entity.friendly_name};
|
||||
messagePayload.device = {friendlyName: entity.name};
|
||||
attributes.forEach((a) => messagePayload.device[a] = device[a]);
|
||||
}
|
||||
|
||||
if (entity.type === 'device' && settings.get().advanced.last_seen !== 'disable') {
|
||||
messagePayload.last_seen = utils.formatDate(entity.device.lastSeen, settings.get().advanced.last_seen);
|
||||
}
|
||||
|
||||
if (Object.entries(messagePayload).length) {
|
||||
if (settings.get().experimental.output === 'json') {
|
||||
await this.mqtt.publish(entity.friendly_name, JSON.stringify(messagePayload), options);
|
||||
await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options);
|
||||
} else {
|
||||
/* istanbul ignore else */
|
||||
if (settings.get().experimental.output === 'attribute') {
|
||||
await this.iteratePayloadAttributeOutput(`${entity.friendly_name}/`, messagePayload, options);
|
||||
await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user