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 13, 2020
1 parent 24188a3 commit 8a4845c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
17 changes: 16 additions & 1 deletion 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 Expand Up @@ -1735,7 +1748,9 @@ class HomeAssistant extends Extension {
if (resolvedEntity.settings.hasOwnProperty('homeassistant')) {
const add = (obj) => {
Object.keys(obj).forEach((key) => {
if (['number', 'string', 'boolean'].includes(typeof obj[key])) {
if (['type', 'object_id'].includes(key)) {
return;
} else if (['number', 'string', 'boolean'].includes(typeof obj[key])) {
payload[key] = obj[key];
} else if (obj[key] === null) {
delete payload[key];
Expand Down
52 changes: 52 additions & 0 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,58 @@ 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'
},
light: {
type: 'this should be ignored',
name: 'my_light_name_override'
}

},
})

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_light_name_override",
"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 8a4845c

Please sign in to comment.