Allow redefine autodiscovery type (#4647)

* allow to redefine entity type and id in homeassistant autodiscovery

* Update homeassistant.test.js

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
Marco Massarotto 2020-10-13 19:33:48 +01:00 committed by GitHub
parent 24188a3eea
commit e63a2ac7f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 1 deletions

View File

@ -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;
}
@ -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];

View File

@ -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": this.version
},
"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,