Update HA Discovery Info on Rename (#3040)

* Update HA Discovery Info on Rename

Update HA discovery info when a device is renamed. Adds a new deviceRenamed
event to the event bus, allowing the HA extension to react to the rename

Fixes #2440

* Handle Groups correctly wrt HA discovery data refresh

* Cleanup group handling

* Cleanup group handling
This commit is contained in:
Kiall Mac Innes
2020-03-04 12:55:08 +01:00
committed by GitHub
parent 3b60a907c8
commit 67b4bcf815
4 changed files with 46 additions and 0 deletions
+2
View File
@@ -3,6 +3,8 @@ const assert = require('assert');
const allowedEvents = [
'deviceRemoved', // Device has been removed
'deviceRenamed', // Device has been renamed
'groupRenamed', // Group has been renamed
'publishEntityState', // Entity state will be published
];
+3
View File
@@ -218,6 +218,9 @@ class BridgeConfig extends BaseExtension {
const isGroup = settings.getGroup(from) !== null;
settings.changeFriendlyName(from, to);
logger.info(`Successfully renamed - ${from} to ${to} `);
const entity = this.zigbee.resolveEntity(to);
const eventData = isGroup ? {group: entity.group} : {device: entity.device};
this.eventBus.emit(`${isGroup ? 'group' : 'device'}Renamed`, eventData);
this.mqtt.log(`${isGroup ? 'group' : 'device'}_renamed`, {from, to});
} catch (error) {
logger.error(`Failed to rename - ${from} to ${to}`);
+7
View File
@@ -1331,6 +1331,7 @@ class HomeAssistant extends BaseExtension {
this.eventBus.on('deviceRemoved', (data) => this.onDeviceRemoved(data.device));
this.eventBus.on('publishEntityState', (data) => this.onPublishEntityState(data));
this.eventBus.on('deviceRenamed', (data) => this.onDeviceRenamed(data.device));
}
onDeviceRemoved(device) {
@@ -1377,6 +1378,12 @@ class HomeAssistant extends BaseExtension {
}
}
onDeviceRenamed(device) {
const mappedModel = zigbeeHerdsmanConverters.findByZigbeeModel(device.modelID);
logger.info(`Refreshing Home Assistant discovery topic for '${device.ieeeAddr}'`);
this.discover(device, mappedModel, true);
}
async onMQTTConnected() {
this.mqtt.subscribe(this.statusTopic);
+34
View File
@@ -688,6 +688,40 @@ describe('HomeAssistant extension', () => {
expect(MQTT.publish).toHaveBeenCalledTimes(1);
});
it('Should refresh discovery when device is renamed', async () => {
controller = new Controller(false);
await controller.start();
await flushPromises();
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/config/rename', '{"old": "weather_sensor", "new": "weather_sensor_renamed"}');
await flushPromises();
const payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor_renamed',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor_renamed',
'name': 'weather_sensor_renamed_temperature',
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
'device': {
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
'name': 'weather_sensor_renamed',
'sw_version': this.version,
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
'manufacturer': 'Xiaomi',
},
'availability_topic': 'zigbee2mqtt/bridge/state',
};
expect(MQTT.publish).toHaveBeenCalledWith(
'homeassistant/sensor/0x0017880104e45522/temperature/config',
JSON.stringify(payload),
{ retain: true, qos: 0 },
expect.any(Function),
);
});
it('Should discover update_available sensor when device supports it', async () => {
controller = new Controller(false);
await controller.start();