mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2024-11-16 18:39:09 -07:00
Refactor
This commit is contained in:
parent
552eec7baa
commit
e71485cd50
@ -231,22 +231,25 @@ class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async publishEntityState(IDorName, payload, stateChangeReason=null) {
|
async publishEntityState(IDorName, payload, stateChangeReason=null) {
|
||||||
const entity = this.zigbee.resolveEntity(IDorName);
|
const resolvedEntity = this.zigbee.resolveEntity(IDorName);
|
||||||
if (!entity || !entity.settings) {
|
if (!resolvedEntity || !resolvedEntity.settings) {
|
||||||
logger.error(`'${IDorName}' does not exist, skipping publish`);
|
logger.error(`'${IDorName}' does not exist, skipping publish`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity.type === 'device' && settings.get().advanced.last_seen !== 'disable' && entity.device.lastSeen) {
|
const isDevice = resolvedEntity.type === 'device';
|
||||||
payload.last_seen = utils.formatDate(entity.device.lastSeen, settings.get().advanced.last_seen);
|
|
||||||
|
if (isDevice && settings.get().advanced.last_seen !== 'disable' && resolvedEntity.device.lastSeen) {
|
||||||
|
payload.last_seen = utils.formatDate(resolvedEntity.device.lastSeen, settings.get().advanced.last_seen);
|
||||||
}
|
}
|
||||||
|
|
||||||
let messagePayload = {...payload};
|
let messagePayload = {...payload};
|
||||||
const currentState = this.state.exists(entity.settings.ID) ? this.state.get(entity.settings.ID) : {};
|
const currentState = this.state.exists(resolvedEntity.settings.ID) ?
|
||||||
|
this.state.get(resolvedEntity.settings.ID) : {};
|
||||||
const newState = objectAssignDeep.noMutate(currentState, payload);
|
const newState = objectAssignDeep.noMutate(currentState, payload);
|
||||||
|
|
||||||
// Update state cache with new state.
|
// Update state cache with new state.
|
||||||
this.state.set(entity.settings.ID, newState, stateChangeReason);
|
this.state.set(resolvedEntity.settings.ID, newState, stateChangeReason);
|
||||||
|
|
||||||
if (settings.get().advanced.cache_state) {
|
if (settings.get().advanced.cache_state) {
|
||||||
// Add cached state to payload
|
// Add cached state to payload
|
||||||
@ -255,50 +258,49 @@ class Controller {
|
|||||||
|
|
||||||
const deviceOptions = settings.get().device_options;
|
const deviceOptions = settings.get().device_options;
|
||||||
const options = {
|
const options = {
|
||||||
retain: utils.getObjectsProperty([entity.settings, deviceOptions], 'retain', false),
|
retain: utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'retain', false),
|
||||||
qos: utils.getObjectsProperty([entity.settings, deviceOptions], 'qos', 0),
|
qos: utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'qos', 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
const retention = utils.getObjectsProperty([entity.settings, deviceOptions], 'retention', false);
|
const retention = utils.getObjectsProperty([resolvedEntity.settings, deviceOptions], 'retention', false);
|
||||||
if (retention !== false) {
|
if (retention !== false) {
|
||||||
options.properties = {messageExpiryInterval: retention};
|
options.properties = {messageExpiryInterval: retention};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (entity.type === 'device' && settings.get().mqtt.include_device_information) {
|
if (isDevice && settings.get().mqtt.include_device_information) {
|
||||||
const device = this.zigbee.getDeviceByIeeeAddr(entity.device.ieeeAddr);
|
|
||||||
const attributes = [
|
const attributes = [
|
||||||
'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource',
|
'ieeeAddr', 'networkAddress', 'type', 'manufacturerID', 'manufacturerName', 'powerSource',
|
||||||
'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID',
|
'applicationVersion', 'stackVersion', 'zclVersion', 'hardwareVersion', 'dateCode', 'softwareBuildID',
|
||||||
];
|
];
|
||||||
|
|
||||||
messagePayload.device = {
|
messagePayload.device = {
|
||||||
friendlyName: entity.name,
|
friendlyName: resolvedEntity.name,
|
||||||
model: entity.definition ? entity.definition.model : 'unknown',
|
model: resolvedEntity.definition ? resolvedEntity.definition.model : 'unknown',
|
||||||
};
|
};
|
||||||
|
|
||||||
attributes.forEach((a) => messagePayload.device[a] = device[a]);
|
attributes.forEach((a) => messagePayload.device[a] = resolvedEntity.device[a]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter mqtt message attributes
|
// filter mqtt message attributes
|
||||||
if (deviceOptions.filtered_attributes) {
|
if (deviceOptions.filtered_attributes) {
|
||||||
deviceOptions.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
deviceOptions.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
||||||
}
|
}
|
||||||
if (entity.settings.filtered_attributes) {
|
if (resolvedEntity.settings.filtered_attributes) {
|
||||||
entity.settings.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
resolvedEntity.settings.filtered_attributes.forEach((a) => delete messagePayload[a]);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.eventBus.emit('publishEntityState', {payload: messagePayload, entity});
|
this.eventBus.emit('publishEntityState', {payload: messagePayload, entity: resolvedEntity});
|
||||||
|
|
||||||
if (Object.entries(messagePayload).length) {
|
if (Object.entries(messagePayload).length) {
|
||||||
if (settings.get().experimental.output === 'attribute_and_json') {
|
if (settings.get().experimental.output === 'attribute_and_json') {
|
||||||
await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options);
|
await this.mqtt.publish(resolvedEntity.name, JSON.stringify(messagePayload), options);
|
||||||
await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options);
|
await this.iteratePayloadAttributeOutput(`${resolvedEntity.name}/`, messagePayload, options);
|
||||||
} else if (settings.get().experimental.output === 'json') {
|
} else if (settings.get().experimental.output === 'json') {
|
||||||
await this.mqtt.publish(entity.name, JSON.stringify(messagePayload), options);
|
await this.mqtt.publish(resolvedEntity.name, JSON.stringify(messagePayload), options);
|
||||||
} else {
|
} else {
|
||||||
/* istanbul ignore else */
|
/* istanbul ignore else */
|
||||||
if (settings.get().experimental.output === 'attribute') {
|
if (settings.get().experimental.output === 'attribute') {
|
||||||
await this.iteratePayloadAttributeOutput(`${entity.name}/`, messagePayload, options);
|
await this.iteratePayloadAttributeOutput(`${resolvedEntity.name}/`, messagePayload, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user