Prepare Home Assistant discovery for group support. #764

This commit is contained in:
Koen Kanters 2019-02-19 19:43:50 +01:00
parent f3e13f16f3
commit 14f5c4d3c9

View File

@ -2,6 +2,7 @@ const zigbeeShepherdConverters = require('zigbee-shepherd-converters');
const settings = require('../util/settings');
const logger = require('../util/logger');
const zigbee2mqttVersion = require('../../package.json').version;
const utils = require('../util/utils');
const configurations = {
// Binary sensor
@ -541,19 +542,24 @@ class HomeAssistant {
});
}
discover(ieeeAddr, mappedModel, force=false) {
discover(entityID, mappedModel, force=false) {
// Check if already discoverd and check if there are configs.
const discover = force || !this.discovered[ieeeAddr];
if (!discover || !mapping[mappedModel.model] || !settings.getDevice(ieeeAddr)) {
const discover = force || !this.discovered[entityID];
if (!discover) {
return;
}
const friendlyName = settings.getDevice(ieeeAddr).friendly_name;
const entity = utils.resolveEntity(entityID);
if (entity.type === 'device' && (!mapping[mappedModel.model] || !settings.getDevice(entity.ID))) {
return;
} else if (entity.type === 'group' && (!settings.getGroup(entity.ID))) {
return;
}
mapping[mappedModel.model].forEach((config) => {
const topic = `${config.type}/${ieeeAddr}/${config.object_id}/config`;
const topic = `${config.type}/${entityID}/${config.object_id}/config`;
const payload = {...config.discovery_payload};
payload.state_topic = `${settings.get().mqtt.base_topic}/${friendlyName}`;
payload.state_topic = `${settings.get().mqtt.base_topic}/${entity.friendlyName}`;
// Set json_attributes_topic for types which support this
// https://github.com/Koenkk/zigbee2mqtt/issues/840
@ -562,15 +568,15 @@ class HomeAssistant {
}
// Set (unique) name
payload.name = `${friendlyName}_${config.object_id}`;
payload.name = `${entity.friendlyName}_${config.object_id}`;
// Set unique_id
payload.unique_id = `${ieeeAddr}_${config.object_id}_${settings.get().mqtt.base_topic}`;
payload.unique_id = `${entityID}_${config.object_id}_${settings.get().mqtt.base_topic}`;
// Attributes for device registry
payload.device = {
identifiers: `zigbee2mqtt_${ieeeAddr}`,
name: friendlyName,
identifiers: `zigbee2mqtt_${entityID}`,
name: entity.friendlyName,
sw_version: `Zigbee2mqtt ${this.zigbee2mqttVersion}`,
model: `${mappedModel.description} (${mappedModel.model})`,
manufacturer: mappedModel.vendor,
@ -580,13 +586,13 @@ class HomeAssistant {
// When using availability_timeout each device has it's own availability topic.
// If not, use the availability topic of zigbee2mqtt.
if (settings.get().advanced.availability_timeout) {
payload.availability_topic = `${settings.get().mqtt.base_topic}/${friendlyName}/availability`;
payload.availability_topic = `${settings.get().mqtt.base_topic}/${entity.friendlyName}/availability`;
} else {
payload.availability_topic = `${settings.get().mqtt.base_topic}/bridge/state`;
}
// Add precision to value_template
const device = settings.getDevice(ieeeAddr);
const device = settings.getDevice(entityID);
if (device.hasOwnProperty(`${config.object_id}_precision`)) {
const precision = device[`${config.object_id}_precision`];
let template = payload.value_template;
@ -596,7 +602,7 @@ class HomeAssistant {
}
if (payload.command_topic) {
payload.command_topic = `${settings.get().mqtt.base_topic}/${friendlyName}/`;
payload.command_topic = `${settings.get().mqtt.base_topic}/${entity.friendlyName}/`;
if (payload.command_topic_prefix) {
payload.command_topic += `${payload.command_topic_prefix}/`;
@ -626,7 +632,7 @@ class HomeAssistant {
this.mqtt.publish(topic, JSON.stringify(payload), {retain: true, qos: 0}, null, 'homeassistant');
});
this.discovered[ieeeAddr] = true;
this.discovered[entityID] = true;
}
onMQTTMessage(topic, message) {