MCPcopy
hub / github.com/Koenkk/zigbee2mqtt / publishEntityState

Method publishEntityState

lib/controller.ts:382–460  ·  view source on GitHub ↗
(entity: Group | Device, payload: KeyValue, stateChangeReason?: StateChangeReason)

Source from the content-addressed store, hash-verified

380 }
381
382 @bind async publishEntityState(entity: Group | Device, payload: KeyValue, stateChangeReason?: StateChangeReason): Promise<void> {
383 let message: Zigbee2MQTTAPI["{friendlyName}"] = {...payload};
384
385 // Update state cache with new state.
386 const newState = this.state.set(entity, payload, stateChangeReason);
387
388 if (settings.get().advanced.cache_state) {
389 // Add cached state to payload
390 message = newState;
391 }
392
393 const options: MakePartialExcept<MqttPublishOptions, "clientOptions" | "meta"> = {
394 clientOptions: {
395 retain: utils.getObjectProperty(entity.options, "retain", false),
396 qos: utils.getObjectProperty(entity.options, "qos", 0),
397 },
398 meta: {
399 isEntityState: true,
400 },
401 };
402 const retention = utils.getObjectProperty<number | false>(entity.options, "retention", false);
403
404 if (retention !== false) {
405 options.clientOptions.properties = {messageExpiryInterval: retention};
406 }
407
408 if (entity.isDevice() && settings.get().mqtt.include_device_information) {
409 message.device = {
410 friendlyName: entity.name,
411 model: entity.definition?.model,
412 ieeeAddr: entity.ieeeAddr,
413 networkAddress: entity.zh.networkAddress,
414 type: entity.zh.type,
415 manufacturerID: entity.zh.manufacturerID,
416 powerSource: entity.zh.powerSource,
417 applicationVersion: entity.zh.applicationVersion,
418 stackVersion: entity.zh.stackVersion,
419 zclVersion: entity.zh.zclVersion,
420 hardwareVersion: entity.zh.hardwareVersion,
421 dateCode: entity.zh.dateCode,
422 softwareBuildID: entity.zh.softwareBuildID,
423 // Manufacturer name can contain \u0000, remove this.
424 // https://github.com/home-assistant/core/issues/85691
425 /* v8 ignore next */
426 manufacturerName: entity.zh.manufacturerName?.split("\u0000")[0],
427 };
428 }
429
430 // Add lastseen
431 const lastSeen = settings.get().advanced.last_seen;
432 if (entity.isDevice() && lastSeen !== "disable" && entity.zh.lastSeen) {
433 message.last_seen = utils.formatDate(entity.zh.lastSeen, lastSeen);
434 }
435
436 // Add device linkquality.
437 if (entity.isDevice() && entity.zh.linkquality !== undefined) {
438 message.linkquality = entity.zh.linkquality;
439 }

Callers 14

startMethod · 0.95
onStateChangeMethod · 0.80
renameEntityMethod · 0.80
onZigbeeEventMethod · 0.80
onMQTTMessageMethod · 0.80
#updateMethod · 0.80
publishDebounceMethod · 0.80
publishMethod · 0.80
retrieveStateMethod · 0.80
onPublishEntityStateMethod · 0.80
onMQTTMessageMethod · 0.80
onMQTTMessageMethod · 0.80

Calls 7

setMethod · 0.80
getMethod · 0.80
isDeviceMethod · 0.45
publishMethod · 0.45

Tested by

no test coverage detected