mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-28 05:24:29 +00:00
HA Discover when user change option (#11210)
* HA Discover when user change option * Fix unit tests * Update homeassistant.ts * Update homeassistant.ts Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
This commit is contained in:
@@ -95,6 +95,7 @@ export default class HomeAssistant extends Extension {
|
||||
this.eventBus.onDeviceJoined(this, this.onZigbeeEvent);
|
||||
this.eventBus.onDeviceInterview(this, this.onZigbeeEvent);
|
||||
this.eventBus.onDeviceMessage(this, this.onZigbeeEvent);
|
||||
this.eventBus.onEntityOptionsChanged(this, (data) => this.discover(data.entity, true));
|
||||
|
||||
this.mqtt.subscribe(this.statusTopic);
|
||||
this.mqtt.subscribe(defaultStatusTopic);
|
||||
@@ -896,8 +897,9 @@ export default class HomeAssistant extends Extension {
|
||||
|
||||
let configs: DiscoveryEntry[] = [];
|
||||
if (isDevice) {
|
||||
for (const expose of entity.exposes()) {
|
||||
configs.push(...this.exposeToConfig([expose], 'device', entity.definition, entity.exposes()));
|
||||
const exposes = entity.exposes(); // avoid calling it hundred of times/s
|
||||
for (const expose of exposes) {
|
||||
configs.push(...this.exposeToConfig([expose], 'device', entity.definition, exposes));
|
||||
}
|
||||
|
||||
for (const mapping of legacyMapping) {
|
||||
|
||||
@@ -58,7 +58,12 @@ describe('HomeAssistant extension', () => {
|
||||
configs.forEach((c) => {
|
||||
const id = c['type'] + '/' + c['object_id'];
|
||||
if (cfg_type_object_ids.includes(id)) {
|
||||
duplicated.push(d.model);
|
||||
if (typeof d.exposes == 'function') {
|
||||
// A dynamic function must exposes all possible attributes for the docs
|
||||
console.warn(`${d.model} dynamic exposes contains duplicated ${id}`)
|
||||
} else {
|
||||
duplicated.push(d.model);
|
||||
}
|
||||
} else {
|
||||
cfg_type_object_ids.push(id);
|
||||
}
|
||||
@@ -993,6 +998,19 @@ describe('HomeAssistant extension', () => {
|
||||
await flushPromises();
|
||||
});
|
||||
|
||||
it('Should discover when options change', async () => {
|
||||
const device = controller.zigbee.resolveEntity(zigbeeHerdsman.devices.bulb);
|
||||
MQTT.publish.mockClear();
|
||||
controller.eventBus.emitEntityOptionsChanged({entity: device, from: {}, to: {'test': 123}});
|
||||
await flushPromises();
|
||||
expect(MQTT.publish).toHaveBeenCalledWith(
|
||||
`homeassistant/light/${device.ID}/light/config`,
|
||||
expect.any(String),
|
||||
expect.any(Object),
|
||||
expect.any(Function),
|
||||
);
|
||||
});
|
||||
|
||||
it('Should send all status when home assistant comes online (default topic)', async () => {
|
||||
data.writeDefaultState();
|
||||
extension.state.load();
|
||||
|
||||
Reference in New Issue
Block a user