2018-04-25 11:54:41 -07:00
|
|
|
const settings = require('./util/settings');
|
|
|
|
|
|
|
|
const configurations = {
|
2018-04-23 09:29:35 -07:00
|
|
|
// Binary sensor
|
|
|
|
'binary_sensor_occupancy': {
|
|
|
|
type: 'binary_sensor',
|
|
|
|
object_id: 'occupancy',
|
|
|
|
discovery_payload: {
|
2018-04-23 12:44:06 -07:00
|
|
|
payload_on: true,
|
|
|
|
payload_off: false,
|
2018-04-23 09:29:35 -07:00
|
|
|
value_template: '{{ value_json.occupancy }}',
|
|
|
|
device_class: 'motion',
|
|
|
|
json_attributes: ['battery']
|
|
|
|
}
|
|
|
|
},
|
2018-04-23 12:44:06 -07:00
|
|
|
'binary_sensor_contact': {
|
2018-04-23 09:29:35 -07:00
|
|
|
type: 'binary_sensor',
|
2018-04-23 12:44:06 -07:00
|
|
|
object_id: 'contact',
|
2018-04-23 09:29:35 -07:00
|
|
|
discovery_payload: {
|
2018-04-23 12:44:06 -07:00
|
|
|
payload_on: false,
|
|
|
|
payload_off: true,
|
|
|
|
value_template: '{{ value_json.contact }}',
|
2018-04-23 09:29:35 -07:00
|
|
|
device_class: 'door',
|
|
|
|
json_attributes: ['battery']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'binary_sensor_water_leak': {
|
|
|
|
type: 'binary_sensor',
|
|
|
|
object_id: 'water_leak',
|
|
|
|
discovery_payload: {
|
|
|
|
payload_on: true,
|
|
|
|
payload_off: false,
|
|
|
|
value_template: '{{ value_json.water_leak }}',
|
|
|
|
device_class: 'moisture',
|
|
|
|
json_attributes: ['battery']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Sensor
|
|
|
|
'sensor_illuminance': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'illuminance',
|
|
|
|
discovery_payload: {
|
|
|
|
unit_of_measurement: 'lx',
|
|
|
|
icon: 'mdi:theme-light-dark',
|
|
|
|
value_template: '{{ value_json.illuminance }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sensor_humidity': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'humidity',
|
|
|
|
discovery_payload: {
|
|
|
|
unit_of_measurement: '%',
|
|
|
|
icon: 'mdi:water-percent',
|
|
|
|
value_template: '{{ value_json.humidity }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sensor_temperature': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'temperature',
|
|
|
|
discovery_payload: {
|
|
|
|
unit_of_measurement: '°C',
|
|
|
|
icon: 'mdi:temperature-celsius',
|
|
|
|
value_template: '{{ value_json.temperature }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'sensor_pressure': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'pressure',
|
|
|
|
discovery_payload: {
|
|
|
|
unit_of_measurement: 'Pa',
|
|
|
|
icon: 'mdi:speedometer',
|
|
|
|
value_template: '{{ value_json.pressure }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
2018-05-08 11:13:01 -07:00
|
|
|
'sensor_click': {
|
2018-04-23 09:29:35 -07:00
|
|
|
type: 'sensor',
|
2018-05-08 11:13:01 -07:00
|
|
|
object_id: 'click',
|
2018-04-23 09:29:35 -07:00
|
|
|
discovery_payload: {
|
|
|
|
icon: 'mdi:toggle-switch',
|
|
|
|
value_template: '{{ value_json.click }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
2018-04-23 13:36:30 -07:00
|
|
|
'sensor_power': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'power',
|
|
|
|
discovery_payload: {
|
|
|
|
unit_of_measurement: 'Watt',
|
|
|
|
icon: 'mdi:flash',
|
|
|
|
value_template: '{{ value_json.power }}',
|
|
|
|
}
|
|
|
|
},
|
2018-05-08 11:20:57 -07:00
|
|
|
'sensor_action': {
|
|
|
|
type: 'sensor',
|
|
|
|
object_id: 'action',
|
|
|
|
discovery_payload: {
|
|
|
|
icon: 'mdi:cube',
|
|
|
|
value_template: '{{ value_json.action }}',
|
|
|
|
json_attributes: ['battery'],
|
|
|
|
}
|
|
|
|
},
|
2018-04-23 09:29:35 -07:00
|
|
|
|
|
|
|
// Light
|
|
|
|
'light_brightness_colortemp_xy': {
|
|
|
|
type: 'light',
|
|
|
|
object_id: 'light',
|
|
|
|
discovery_payload: {
|
|
|
|
brightness: true,
|
|
|
|
color_temp: true,
|
|
|
|
xy: true,
|
|
|
|
platform: 'mqtt_json',
|
|
|
|
command_topic: true
|
2018-04-25 11:54:41 -07:00
|
|
|
}
|
2018-04-23 09:29:35 -07:00
|
|
|
},
|
|
|
|
'light_brightness_colortemp': {
|
|
|
|
type: 'light',
|
|
|
|
object_id: 'light',
|
|
|
|
discovery_payload: {
|
|
|
|
brightness: true,
|
|
|
|
color_temp: true,
|
|
|
|
platform: 'mqtt_json',
|
|
|
|
command_topic: true
|
2018-04-25 11:54:41 -07:00
|
|
|
}
|
2018-04-23 09:29:35 -07:00
|
|
|
},
|
2018-05-06 09:08:10 -07:00
|
|
|
'light_brightness': {
|
|
|
|
type: 'light',
|
|
|
|
object_id: 'light',
|
|
|
|
discovery_payload: {
|
|
|
|
brightness: true,
|
|
|
|
platform: 'mqtt_json',
|
|
|
|
command_topic: true
|
|
|
|
}
|
|
|
|
},
|
2018-04-23 13:36:30 -07:00
|
|
|
|
|
|
|
// Switch
|
|
|
|
'switch': {
|
|
|
|
type: 'switch',
|
|
|
|
object_id: 'switch',
|
|
|
|
discovery_payload: {
|
2018-04-24 10:03:09 -07:00
|
|
|
payload_off: 'OFF',
|
|
|
|
payload_on: 'ON',
|
2018-04-23 13:36:30 -07:00
|
|
|
value_template: '{{ value_json.state }}',
|
|
|
|
command_topic: true
|
2018-04-25 11:54:41 -07:00
|
|
|
}
|
2018-04-24 10:30:56 -07:00
|
|
|
},
|
2018-04-29 05:09:49 -07:00
|
|
|
'switch_l1': {
|
|
|
|
type: 'switch',
|
|
|
|
object_id: 'switch_l1',
|
|
|
|
discovery_payload: {
|
|
|
|
payload_off: 'OFF',
|
|
|
|
payload_on: 'ON',
|
|
|
|
value_template: '{{ value_json.state_l1 }}',
|
|
|
|
command_topic: true,
|
|
|
|
command_topic_prefix: 'l1'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'switch_l2': {
|
|
|
|
type: 'switch',
|
|
|
|
object_id: 'switch_l2',
|
|
|
|
discovery_payload: {
|
|
|
|
payload_off: 'OFF',
|
|
|
|
payload_on: 'ON',
|
|
|
|
value_template: '{{ value_json.state_l2 }}',
|
|
|
|
command_topic: true,
|
|
|
|
command_topic_prefix: 'l2'
|
|
|
|
}
|
|
|
|
},
|
2018-04-23 09:29:35 -07:00
|
|
|
};
|
|
|
|
|
2018-04-25 11:54:41 -07:00
|
|
|
// A map of all discoverd devices
|
|
|
|
const discovered = {};
|
|
|
|
|
|
|
|
function discover(deviceID, configs, mqtt) {
|
|
|
|
// Check if already discoverd and check if there are configs.
|
|
|
|
if (discovered[deviceID] || !configs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const friendlyName = settings.getDevice(deviceID).friendly_name;
|
|
|
|
|
|
|
|
configs.forEach((config) => {
|
|
|
|
const topic = `${config.type}/${deviceID}/${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.
|
|
|
|
if (configs.length > 1) {
|
|
|
|
payload.name = `${friendlyName}_${config.object_id}`;
|
|
|
|
} else {
|
|
|
|
payload.name = friendlyName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.command_topic) {
|
2018-04-29 05:09:49 -07:00
|
|
|
payload.command_topic = `${settings.get().mqtt.base_topic}/${friendlyName}/`;
|
|
|
|
|
|
|
|
if (payload.command_topic_prefix) {
|
|
|
|
payload.command_topic += `${payload.command_topic_prefix}/`;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.command_topic += 'set';
|
2018-04-25 11:54:41 -07:00
|
|
|
}
|
|
|
|
|
2018-05-15 09:42:26 -07:00
|
|
|
mqtt.publish(topic, JSON.stringify(payload), {retain: true, qos: 0}, null, 'homeassistant');
|
2018-04-25 11:54:41 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
discovered[deviceID] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
configurations: configurations,
|
|
|
|
discover: (deviceID, configs, mqtt) => discover(deviceID, configs, mqtt),
|
|
|
|
};
|