Skip to content

Commit

Permalink
Fix disabling HA discovery after device was discovered
Browse files Browse the repository at this point in the history
  • Loading branch information
nurikk committed Dec 7, 2020
1 parent 92b942e commit 768568d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ class HomeAssistant extends Extension {
// deep clone of the config objects
configs = JSON.parse(JSON.stringify(configs));

if (resolvedEntity.settings.hasOwnProperty('homeassistant')) {
if (resolvedEntity.settings.homeassistant) {
configs.forEach((config) => {
const configOverride = resolvedEntity.settings.homeassistant[config.object_id];
if (configOverride) {
Expand Down Expand Up @@ -854,6 +854,9 @@ class HomeAssistant extends Extension {
const objectID = discoveryMatch[3];
clear = !this.getConfigs(resolvedEntity).find((c) => c.type === type && c.object_id === objectID);
}
// Device was flagged to be excluded from homeassistant discovery
clear = clear || (resolvedEntity.settings.hasOwnProperty('homeassistant')
&& !resolvedEntity.settings.homeassistant);

if (clear) {
logger.debug(`Clearing Home Assistant config '${topic}'`);
Expand Down
19 changes: 19 additions & 0 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const fs = require('fs');
const path = require('path');
const HomeAssistant = require('../lib/extension/homeassistant');

const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});

describe('HomeAssistant extension', () => {
beforeEach(async () => {
this.version = await require('../lib/util/utils').getZigbee2mqttVersion();
Expand Down Expand Up @@ -1432,5 +1434,22 @@ describe('HomeAssistant extension', () => {
await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2_not_existing/action_button_3_single/config', stringify({topic: 'zigbee2mqtt_different/0x000b57fffec6a5b2_not_existing/availability'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(0);

// Device was flagged to be excluded from homeassistant discovery
await controller.stop();
await flushPromises();
settings.set(['devices', '0x000b57fffec6a5b2', 'homeassistant'], null);
controller = new Controller(false);
await controller.start();
await flushPromises();
MQTT.publish.mockClear();

await MQTT.events.message('homeassistant/sensor/0x000b57fffec6a5b2/update_available/config', stringify({availability: [{topic: 'zigbee2mqtt/bridge/state'}]}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/sensor/0x000b57fffec6a5b2/update_available/config', null, {qos: 0, retain: true}, expect.any(Function));
MQTT.publish.mockClear();
await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2/action_button_3_single/config', stringify({topic: 'zigbee2mqtt/0x000b57fffec6a5b2/availability'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/device_automation/0x000b57fffec6a5b2/action_button_3_single/config', null, {qos: 0, retain: true}, expect.any(Function));
});
});

0 comments on commit 768568d

Please sign in to comment.