Skip to content

Commit

Permalink
allow to redefine entity type and id in homeassistant autodiscovery
Browse files Browse the repository at this point in the history
  • Loading branch information
lbg-marco-massarotto committed Oct 12, 2020
1 parent 24188a3 commit 4a3b377
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/extension/homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,19 @@ class HomeAssistant extends Extension {
configs = configs.filter((c) => c !== cfg.sensor_action && c !== cfg.sensor_click);
}

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

if (resolvedEntity.settings.hasOwnProperty('homeassistant')) {
configs.forEach((config) => {
const configOverride = resolvedEntity.settings.homeassistant[config.object_id];
if (configOverride) {
config.object_id = configOverride.object_id || config.object_id;
config.type = configOverride.type || config.type;
}
});
}

return configs;
}

Expand Down
47 changes: 47 additions & 0 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,53 @@ describe('HomeAssistant extension', () => {
);
});

it('Should discover devices with overriden user configuration affecting type and object_id', async () => {
settings.set(['devices', '0x0017880104e45541'], {
friendly_name: 'my_switch',
homeassistant: {
switch: {
type: 'light',
object_id: 'light'
}
},
})

controller = new Controller(false);
await controller.start();

let payload;
await flushPromises();

payload = {
"availability_topic": "zigbee2mqtt/bridge/state",
"command_topic": "zigbee2mqtt/my_switch/set",
"device": {
"identifiers": [
"zigbee2mqtt_0x0017880104e45541"
],
"manufacturer": "Xiaomi",
"model": "Aqara single key wired wall switch without neutral wire. Doesn't work as a router and doesn't support power meter (QBKG04LM)",
"name": "my_switch",
"sw_version": "Zigbee2MQTT 1.15.0-dev"
},
"json_attributes_topic": "zigbee2mqtt/my_switch",
"name": "my_switch_light",
"payload_off": "OFF",
"payload_on": "ON",
"state_topic": "zigbee2mqtt/my_switch",
"unique_id": "0x0017880104e45541_light_zigbee2mqtt",
"value_template": "{{ value_json.state }}"
}

expect(MQTT.publish).toHaveBeenCalledWith(
'homeassistant/light/0x0017880104e45541/light/config',
stringify(payload),
{ retain: true, qos: 0 },
expect.any(Function),
);

});

it('Shouldnt discover devices when homeassistant null is set in device options', async () => {
settings.set(['devices', '0x0017880104e45522'], {
homeassistant: null,
Expand Down

0 comments on commit 4a3b377

Please sign in to comment.