diff --git a/lib/extension/homeassistant.js b/lib/extension/homeassistant.js index f57dab845c..92155446bb 100644 --- a/lib/extension/homeassistant.js +++ b/lib/extension/homeassistant.js @@ -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; } diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 4ae16bc450..ad33d6598b 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -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,