Skip to content

Commit d68f93f

Browse files
feat(ha): treat notificationComplete as stateless event like action
Extend Home Assistant extension handling so notificationComplete follows the same pattern as action: clear from cached state after publish (always, unlike action which clears only with legacyActionSensor), publish the value to a dedicated MQTT topic for device triggers, and run device trigger discovery. Adds STATELESS_EVENT_PROPERTIES to list event-like state keys; skip empty values to avoid re-entrant clears. Supports Inovelli ledEffectComplete (zigbee-herdsman-converters) without overloading the action property. Made-with: Cursor
1 parent 0c9ec72 commit d68f93f

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

lib/extension/homeassistant.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const ACTION_PATTERNS: string[] = [
4141
"^(?<action>dial_rotate)_(?<direction>left|right)_(?<speed>step|slow|fast)$",
4242
"^(?<action>brightness_step)(?:_(?<direction>up|down))?$",
4343
];
44+
45+
/**
46+
* Properties that are stateless events: when published, we clear them from state and
47+
* republish to a dedicated MQTT topic for device triggers (same behavior as action).
48+
*/
49+
const STATELESS_EVENT_PROPERTIES: ReadonlyArray<string> = ["action", "notificationComplete"];
50+
4451
const ACCESS_STATE = 0b001;
4552
const ACCESS_SET = 0b010;
4653
const GROUP_SUPPORTED_TYPES: ReadonlyArray<string> = ["light", "switch", "lock", "cover"];
@@ -1414,24 +1421,30 @@ export class HomeAssistant extends Extension {
14141421
}
14151422

14161423
/**
1417-
* Publish an empty value for click and action payload, in this way Home Assistant
1418-
* can use Home Assistant entities in automations.
1419-
* https://github.com/Koenkk/zigbee2mqtt/issues/959#issuecomment-480341347
1424+
* Stateless event properties: clear from state after publish and republish to a dedicated
1425+
* topic so they behave as one-off events, not retained state.
1426+
* - action: clear only when legacyActionSensor (for HA automations).
1427+
* https://github.com/Koenkk/zigbee2mqtt/issues/959#issuecomment-480341347
1428+
* - notificationComplete and others: always clear.
14201429
*/
1421-
if (this.legacyActionSensor && data.message.action) {
1422-
await this.publishEntityState(data.entity, {action: ""});
1423-
}
1430+
for (const key of STATELESS_EVENT_PROPERTIES) {
1431+
const value = data.message[key];
1432+
if (value === undefined || value === "") continue;
14241433

1425-
/**
1426-
* Implements the MQTT device trigger (https://www.home-assistant.io/integrations/device_trigger.mqtt/)
1427-
* The MQTT device trigger does not support JSON parsing, so it cannot listen to zigbee2mqtt/my_device
1428-
* Whenever a device publish an {action: *} we discover an MQTT device trigger sensor
1429-
* and republish it to zigbee2mqtt/my_device/action
1430-
*/
1431-
if (settings.get().advanced.output === "json" && entity.isDevice() && entity.definition && data.message.action) {
1432-
const value = data.message.action.toString();
1433-
await this.publishDeviceTriggerDiscover(entity, "action", value);
1434-
await this.mqtt.publish(`${data.entity.name}/action`, value, {});
1434+
const shouldClear = key === "action" ? this.legacyActionSensor : true;
1435+
if (shouldClear) {
1436+
await this.publishEntityState(data.entity, {[key]: ""});
1437+
}
1438+
1439+
/**
1440+
* MQTT device trigger: republish to zigbee2mqtt/device/{key} so device triggers work.
1441+
* https://www.home-assistant.io/integrations/device_trigger.mqtt/
1442+
*/
1443+
if (settings.get().advanced.output === "json" && entity.isDevice() && entity.definition) {
1444+
const valueStr = value.toString();
1445+
await this.publishDeviceTriggerDiscover(entity, key, valueStr);
1446+
await this.mqtt.publish(`${data.entity.name}/${key}`, valueStr, {});
1447+
}
14351448
}
14361449
}
14371450

0 commit comments

Comments
 (0)