mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-27 21:20:03 +00:00
Experimental support for home assistant device registry. https://github.com/Koenkk/zigbee2mqtt/issues/646
This commit is contained in:
@@ -426,30 +426,30 @@ class HomeAssistant {
|
||||
|
||||
// MQTT discovery of all paired devices on startup.
|
||||
this.zigbee.getAllClients().forEach((device) => {
|
||||
const mappedDevice = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
|
||||
if (mappedDevice) {
|
||||
this.discover(device.ieeeAddr, mappedDevice.model, true);
|
||||
const mappedModel = zigbeeShepherdConverters.findByZigbeeModel(device.modelId);
|
||||
if (mappedModel) {
|
||||
this.discover(device.ieeeAddr, mappedModel, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
discover(ieeeAddr, model, force=false) {
|
||||
discover(ieeeAddr, mappedModel, force=false) {
|
||||
// Check if already discoverd and check if there are configs.
|
||||
const discover = force || !this.discovered[ieeeAddr];
|
||||
if (!discover || !mapping[model] || !settings.getDevice(ieeeAddr)) {
|
||||
if (!discover || !mapping[mappedModel.model] || !settings.getDevice(ieeeAddr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const friendlyName = settings.getDevice(ieeeAddr).friendly_name;
|
||||
|
||||
mapping[model].forEach((config) => {
|
||||
mapping[mappedModel.model].forEach((config) => {
|
||||
const topic = `${config.type}/${ieeeAddr}/${config.object_id}/config`;
|
||||
const payload = {...config.discovery_payload};
|
||||
payload.state_topic = `${settings.get().mqtt.base_topic}/${friendlyName}`;
|
||||
payload.availability_topic = `${settings.get().mqtt.base_topic}/bridge/state`;
|
||||
|
||||
// Set unique names in cases this device produces multiple entities in homeassistant.
|
||||
payload.name = mapping[model].length > 1 ? `${friendlyName}_${config.object_id}` : friendlyName;
|
||||
payload.name = mapping[mappedModel.model].length > 1 ? `${friendlyName}_${config.object_id}` : friendlyName;
|
||||
|
||||
// Only set unique_id when user did not set a friendly_name yet,
|
||||
// see https://github.com/Koenkk/zigbee2mqtt/issues/138
|
||||
@@ -467,6 +467,15 @@ class HomeAssistant {
|
||||
payload.command_topic += 'set';
|
||||
}
|
||||
|
||||
// Add Home Assistant device registry payload
|
||||
payload.device = {
|
||||
identifiers: ieeeAddr,
|
||||
name: mappedModel.description,
|
||||
sw_version: 'test',
|
||||
model: mappedModel.model,
|
||||
manufacturer: mappedModel.vendor,
|
||||
};
|
||||
|
||||
this.mqtt.publish(topic, JSON.stringify(payload), {retain: true, qos: 0}, null, 'homeassistant');
|
||||
});
|
||||
|
||||
@@ -494,9 +503,9 @@ class HomeAssistant {
|
||||
return true;
|
||||
}
|
||||
|
||||
onZigbeeMessage(message, device, mappedDevice) {
|
||||
if (device && mappedDevice) {
|
||||
this.discover(device.ieeeAddr, mappedDevice.model);
|
||||
onZigbeeMessage(message, device, mappedModel) {
|
||||
if (device && mappedModel) {
|
||||
this.discover(device.ieeeAddr, mappedModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user