Skip to content

Commit

Permalink
Remove legacy_trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Oct 26, 2024
1 parent 0384773 commit b74c996
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 141 deletions.
22 changes: 4 additions & 18 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ export default class HomeAssistant extends Extension {
private discoveryRegexWoTopic = new RegExp(`(.*)/(.*)/(.*)/config`);
private statusTopic: string;
private entityAttributes: boolean;
private legacyTrigger: boolean;
// @ts-expect-error initialized in `start`
private zigbee2MQTTVersion: string;
// @ts-expect-error initialized in `start`
Expand Down Expand Up @@ -460,7 +459,6 @@ export default class HomeAssistant extends Extension {
this.discoveryRegex = new RegExp(`${haSettings.discovery_topic}/(.*)/(.*)/(.*)/config`);
this.statusTopic = haSettings.status_topic;
this.entityAttributes = haSettings.legacy_entity_attributes;
this.legacyTrigger = haSettings.legacy_triggers;
if (haSettings.discovery_topic === settings.get().mqtt.base_topic) {
throw new Error(`'homeassistant.discovery_topic' cannot not be equal to the 'mqtt.base_topic' (got '${settings.get().mqtt.base_topic}')`);
}
Expand Down Expand Up @@ -1124,6 +1122,10 @@ export default class HomeAssistant extends Extension {
assertEnumExpose(firstExpose);
const valueTemplate = firstExpose.access & ACCESS_STATE ? `{{ value_json.${firstExpose.property} }}` : undefined;

// if (firstExpose.property === 'action' || firstExpose.property === 'click') {
// break; todo
// }

if (firstExpose.access & ACCESS_STATE) {
discoveryEntries.push({
type: 'sensor',
Expand Down Expand Up @@ -1313,18 +1315,6 @@ export default class HomeAssistant extends Extension {
}
}

/**
* Publish an empty value for click and action payload, in this way Home Assistant
* can use Home Assistant entities in automations.
* https://github.com/Koenkk/zigbee2mqtt/issues/959#issuecomment-480341347
*/
if (this.legacyTrigger) {
const keys = ['action', 'click'].filter((k) => data.message[k]);
for (const key of keys) {
await this.publishEntityState(data.entity, {[key]: ''});
}
}

/**
* Implements the MQTT device trigger (https://www.home-assistant.io/integrations/device_trigger.mqtt/)
* The MQTT device trigger does not support JSON parsing, so it cannot listen to zigbee2mqtt/my_device
Expand Down Expand Up @@ -1528,10 +1518,6 @@ export default class HomeAssistant extends Extension {
configs = configs.filter((c) => c !== SENSOR_CLICK);
}

if (!this.legacyTrigger) {
configs = configs.filter((c) => c.object_id !== 'action' && c.object_id !== 'click');
}

// deep clone of the config objects
configs = JSON.parse(JSON.stringify(configs));

Expand Down
1 change: 0 additions & 1 deletion lib/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ declare global {
discovery_topic: string;
status_topic: string;
legacy_entity_attributes: boolean;
legacy_triggers: boolean;
};
availability?: {
active: {timeout: number};
Expand Down
12 changes: 0 additions & 12 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
"type": "object",
"title": "Home Assistant (advanced)",
"properties": {
"legacy_triggers": {
"type": "boolean",
"title": "Home Assistant legacy triggers",
"description": "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",
"default": true
},
"discovery_topic": {
"type": "string",
"title": "Homeassistant discovery topic",
Expand Down Expand Up @@ -733,12 +727,6 @@
"requiresRestart": true,
"examples": ["homeassistant/status"]
},
"homeassistant_legacy_triggers": {
"type": "boolean",
"title": "Home Assistant legacy triggers",
"description": "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 discovered",
"default": true
},
"baudrate": {
"type": "number",
"title": "Baudrate (deprecated)",
Expand Down
10 changes: 2 additions & 8 deletions lib/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ objectAssignDeep(schema, schemaJson);
{
delete schema.properties.advanced.properties.homeassistant_discovery_topic;
delete schema.properties.advanced.properties.homeassistant_legacy_entity_attributes;
delete schema.properties.advanced.properties.homeassistant_legacy_triggers;
delete schema.properties.advanced.properties.homeassistant_status_topic;
delete schema.properties.advanced.properties.baudrate;
delete schema.properties.advanced.properties.rtscts;
Expand Down Expand Up @@ -124,15 +123,10 @@ function loadSettingsWithDefaults(): void {
}

if (_settingsWithDefaults.homeassistant) {
const defaults = {discovery_topic: 'homeassistant', status_topic: 'hass/status', legacy_entity_attributes: true, legacy_triggers: true};
const defaults = {discovery_topic: 'homeassistant', status_topic: 'hass/status', legacy_entity_attributes: true};
const sLegacy = {};
if (_settingsWithDefaults.advanced) {
for (const key of [
'homeassistant_legacy_triggers',
'homeassistant_discovery_topic',
'homeassistant_legacy_entity_attributes',
'homeassistant_status_topic',
]) {
for (const key of ['homeassistant_discovery_topic', 'homeassistant_legacy_entity_attributes', 'homeassistant_status_topic']) {
// @ts-expect-error ignore typing
if (_settingsWithDefaults.advanced[key] !== undefined) {
// @ts-expect-error ignore typing
Expand Down
101 changes: 0 additions & 101 deletions test/extensions/homeassistant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1748,20 +1748,6 @@ describe('Extension: HomeAssistant', () => {
expect.any(Function),
);

expect(mockMQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
stringify({action: '', battery: null, linkquality: null, voltage: null, click: null, power_outage_count: null, device_temperature: null}),
{retain: false, qos: 0},
expect.any(Function),
);

expect(mockMQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
stringify({click: '', action: null, battery: null, linkquality: null, voltage: null, power_outage_count: null, device_temperature: null}),
{retain: false, qos: 0},
expect.any(Function),
);

// Should only discover it once
mockMQTT.publish.mockClear();
await mockZHEvents.message(payload1);
Expand Down Expand Up @@ -1859,58 +1845,6 @@ describe('Extension: HomeAssistant', () => {
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/linkquality/config');
});

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

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

mockMQTT.publish.mockClear();

const device = devices.WXKG11LM;
settings.set(['devices', device.ieeeAddr, 'legacy'], false);
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
await mockZHEvents.message(payload);
await flushPromises();

const discoverPayload = {
automation_type: 'trigger',
type: 'action',
subtype: 'single',
payload: 'single',
topic: 'zigbee2mqtt/button/action',
origin: origin,
device: {
identifiers: ['zigbee2mqtt_0x0017880104e45520'],
name: 'button',
model: 'Wireless mini switch (WXKG11LM)',
manufacturer: 'Aqara',
via_device: 'zigbee2mqtt_bridge_0x00124b00120144ae',
},
};

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

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

expect(mockMQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/button',
stringify({action: 'single', battery: null, linkquality: null, voltage: null, power_outage_count: null, device_temperature: null}),
{retain: false, qos: 0},
expect.any(Function),
);

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

it('Should republish payload to postfix topic with lightWithPostfix config', async () => {
mockMQTT.publish.mockClear();

Expand Down Expand Up @@ -1948,41 +1882,6 @@ describe('Extension: HomeAssistant', () => {
group.members.pop();
});

it('Should counter an action payload with an empty payload', async () => {
mockMQTT.publish.mockClear();
const device = devices.WXKG11LM;
settings.set(['devices', device.ieeeAddr, 'legacy'], false);
const data = {onOff: 1};
const payload = {data, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
await mockZHEvents.message(payload);
await flushPromises();
expect(mockMQTT.publish).toHaveBeenCalledTimes(4);
expect(mockMQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/button');
expect(JSON.parse(mockMQTT.publish.mock.calls[0][1])).toStrictEqual({
action: 'single',
click: null,
battery: null,
linkquality: null,
voltage: null,
power_outage_count: null,
device_temperature: null,
});
expect(mockMQTT.publish.mock.calls[0][2]).toStrictEqual({qos: 0, retain: false});
expect(mockMQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/button');
expect(JSON.parse(mockMQTT.publish.mock.calls[1][1])).toStrictEqual({
action: '',
click: null,
battery: null,
linkquality: null,
voltage: null,
power_outage_count: null,
device_temperature: null,
});
expect(mockMQTT.publish.mock.calls[1][2]).toStrictEqual({qos: 0, retain: false});
expect(mockMQTT.publish.mock.calls[2][0]).toStrictEqual('homeassistant/device_automation/0x0017880104e45520/action_single/config');
expect(mockMQTT.publish.mock.calls[3][0]).toStrictEqual('zigbee2mqtt/button/action');
});

it('Should clear outdated configs', async () => {
// Non-existing group -> clear
mockMQTT.publish.mockClear();
Expand Down
1 change: 0 additions & 1 deletion test/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,6 @@ describe('Settings', () => {
expect(settings.get().homeassistant).toStrictEqual({
discovery_topic: 'new',
legacy_entity_attributes: true,
legacy_triggers: true,
status_topic: 'olds',
});
});
Expand Down

0 comments on commit b74c996

Please sign in to comment.