Skip to content

Commit

Permalink
Allow to disable homeassistant legacy triggers. #3033
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Mar 1, 2020
1 parent ed75c85 commit 9f1b8cf
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/extension/deviceReceive.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class DeviceReceive extends BaseExtension {
} else {
this.publishEntityState(data.device.ieeeAddr, payload);

if (settings.get().homeassistant) {
if (settings.get().homeassistant && settings.get().advanced.homeassistant_legacy_triggers) {
/**
* Publish an empty value for click and action payload, in this way Home Assistant
* can use Home Assistant entities in automations.
Expand Down
7 changes: 6 additions & 1 deletion lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,10 +1364,15 @@ class HomeAssistant extends BaseExtension {
}

getConfigs(mappedModel) {
const configs = mapping[mappedModel.model].slice();
let configs = mapping[mappedModel.model].slice();
if (mappedModel.hasOwnProperty('ota')) {
configs.push(cfg.binary_sensor_update_available);
}

if (!settings.get().advanced.homeassistant_legacy_triggers) {
configs = configs.filter((c) => c !== cfg.sensor_action && c !== cfg.sensor_click);
}

return configs;
}

Expand Down
7 changes: 7 additions & 0 deletions lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ const defaults = {
*/
homeassistant_status_topic: 'hass/status',

/**
* Home Assistant legacy triggers, when enabled:
* - Zigbee2mqt will send an empty 'action' or 'click' after one has been send
* - A 'sensor_action' and 'sensor_click' will be discoverd
*/
homeassistant_legacy_triggers: true,

/**
* Configurable timestampFormat
* https://github.com/Koenkk/zigbee2mqtt/commit/44db557a0c83f419d66755d14e460cd78bd6204e
Expand Down
79 changes: 79 additions & 0 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,12 @@ describe('HomeAssistant extension', () => {
controller = new Controller(false);
await controller.start();
await flushPromises();

const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
expect(discovered.length).toBe(4);
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/click/config');
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/action/config');

MQTT.publish.mockClear();

const device = zigbeeHerdsman.devices.WXKG11LM;
Expand Down Expand Up @@ -762,6 +768,20 @@ describe('HomeAssistant extension', () => {
expect.any(Function),
);

expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
JSON.stringify({click: "single", linkquality: 10}),
{ retain: false, qos: 0 },
expect.any(Function),
);

expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
JSON.stringify({linkquality: 10, click: ""}),
{ retain: false, qos: 0 },
expect.any(Function),
);

// Should only discover it once
MQTT.publish.mockClear();
await zigbeeHerdsman.events.message(payload);
Expand All @@ -780,4 +800,63 @@ describe('HomeAssistant extension', () => {
expect.any(Function),
);
});

it('Should disable Home Assistant legacy triggers', async () => {
settings.set(['advanced', 'homeassistant_legacy_triggers'], false);
controller = new Controller(false);
await controller.start();
await flushPromises();

const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
expect(discovered.length).toBe(2);
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/click/config');
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/action/config');

MQTT.publish.mockClear();

const device = zigbeeHerdsman.devices.WXKG11LM;
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
await zigbeeHerdsman.events.message(payload);
await flushPromises();

const discoverPayload = {
"automation_type":"trigger",
"type":"click",
"subtype":"single",
"payload":"single",
"topic":"zigbee2mqtt/button/click",
"device":{
"identifiers":[
"zigbee2mqtt_0x0017880104e45520"
],
"name":"button",
"sw_version": this.version,
"model":"Aqara wireless switch (WXKG11LM)",
"manufacturer":"Xiaomi"
}
};

expect(MQTT.publish).toHaveBeenCalledWith(
'homeassistant/device_automation/0x0017880104e45520/click_single/config',
JSON.stringify(discoverPayload),
{ retain: true, qos: 0 },
expect.any(Function),
);

expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button/click',
'single',
{ retain: false, qos: 0 },
expect.any(Function),
);

expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
JSON.stringify({click: "single", linkquality: 10}),
{ retain: false, qos: 0 },
expect.any(Function),
);

expect(MQTT.publish).toHaveBeenCalledTimes(3);
});
});

0 comments on commit 9f1b8cf

Please sign in to comment.