Skip to content

Commit

Permalink
fix(ignore): Fix Home Assistant device_automation being cleared due…
Browse files Browse the repository at this point in the history
… to ef68cc3
  • Loading branch information
Koenkk committed May 27, 2024
1 parent b984a2d commit ae8a59d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,8 @@ export default class HomeAssistant extends Extension {
config.mockProperties?.forEach((mockProperty) => discovered.mockProperties.add(mockProperty));
});
lastDiscoverdTopics.forEach((topic) => {
if (!newDiscoveredTopics.has(topic)) {
const isDeviceAutomation = topic.match(this.discoveryRegexWoTopic)[1] === 'device_automation';
if (!newDiscoveredTopics.has(topic) && !isDeviceAutomation) {
this.mqtt.publish(topic, null, {retain: true, qos: 1}, this.discoveryTopic, false, false);
}
});
Expand Down
35 changes: 26 additions & 9 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@ describe('HomeAssistant extension', () => {

it('Should not discovery devices which are already discovered', async() => {
await resetExtension(false);
const topic = 'homeassistant/sensor/0x0017880104e45522/humidity/config';
const payload = stringify({
const topic1 = 'homeassistant/sensor/0x0017880104e45522/humidity/config';
const payload1 = stringify({
'unit_of_measurement': '%',
'device_class': 'humidity',
'state_class': 'measurement',
Expand All @@ -446,24 +446,41 @@ describe('HomeAssistant extension', () => {
},
'availability': [{topic: 'zigbee2mqtt/bridge/state'}],
});
const topic2 = 'homeassistant/device_automation/0x0017880104e45522/action_double/config';
const payload2 = stringify({
"automation_type":"trigger",
"type":"action",
"subtype":"double",
"payload":"double",
"topic":"zigbee2mqtt/weather_sensor_renamed/action",
'origin': origin,
"device":{
"identifiers":[
"zigbee2mqtt_0x0017880104e45522"
],
"name":"weather_sensor_renamed",
"sw_version": null,
"model":"Temperature and humidity sensor (WSDCGQ11LM)",
"manufacturer":"Aqara",
"via_device": "zigbee2mqtt_bridge_0x00124b00120144ae",
}
});

// Should subscribe to `homeassistant/#` to find out what devices are already discovered.
expect(MQTT.subscribe).toHaveBeenCalledWith(`homeassistant/#`);

// Retained Home Assistant discovery message arrives
await MQTT.events.message(topic, payload);
await MQTT.events.message(topic1, payload1);
await MQTT.events.message(topic2, payload2);

jest.runOnlyPendingTimers();

// Should unsubscribe to not receive all messages that are going to be published to `homeassistant/#` again.
expect(MQTT.unsubscribe).toHaveBeenCalledWith(`homeassistant/#`);

expect(MQTT.publish).not.toHaveBeenCalledWith(
'homeassistant/sensor/0x0017880104e45522/humidity/config',
expect.any(String),
expect.any(Object),
expect.any(Function),
);
expect(MQTT.publish).not.toHaveBeenCalledWith(topic1, expect.anything(), expect.any(Object), expect.any(Function));
// Device automation should not be cleared
expect(MQTT.publish).not.toHaveBeenCalledWith(topic2, null, expect.any(Object), expect.any(Function));
expect(logger.debug).toHaveBeenCalledWith(`Skipping discovery of 'sensor/0x0017880104e45522/humidity/config', already discovered`)
});

Expand Down

0 comments on commit ae8a59d

Please sign in to comment.