zigbee2mqtt/support/docgen.js

120 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-04-11 11:54:22 -07:00
/**
* This script generates the supported devices page.
* Run by executing: npm run docs
*/
2018-04-18 09:25:40 -07:00
const zigbee2mqtt = require('../lib/converters/zigbee2mqtt');
const deviceMapping = require('../lib/devices');
2018-04-21 10:19:51 -07:00
const fs = require('fs');
const YAML = require('json2yaml');
2018-04-11 11:54:22 -07:00
// Sanity check if all supported devices are in deviceMapping
const supportedDevices = new Set();
2018-04-18 09:25:40 -07:00
zigbee2mqtt.forEach((p) => supportedDevices.add(...p.devices));
2018-04-11 11:54:22 -07:00
// Check if in deviceMapping.
supportedDevices.forEach((s) => {
if (!Object.values(deviceMapping).find((d) => d.model === s)) {
console.log(`ERROR: ${s} not in deviceMapping`);
}
});
2018-04-21 10:19:51 -07:00
const outputdir = process.argv[2];
if (!outputdir) {
console.error("Please specify an output directory");
}
let file = 'Supported-devices.md';
let text = '*NOTE: Automatically generated by `npm run docgen`*\n';
text += `
2018-05-01 09:51:38 -07:00
In case you own a Zigbee device which is **NOT** listed here, please see [How to support new devices](https://github.com/Koenkk/zigbee2mqtt/wiki/How-to-support-new-devices).
2018-04-21 10:19:51 -07:00
\n`;
2018-04-13 10:00:17 -07:00
const logDevices = (devices) => {
2018-04-21 10:19:51 -07:00
let result = '';
result += '| Model | Description | Picture |\n';
result += '| ------------- | ------------- | -------------------------- |\n';
devices = new Map(devices.map((d) => [d.model, d]));
2018-04-13 10:00:17 -07:00
devices.forEach((device) => {
2018-04-21 10:19:51 -07:00
result += `| ${device.model} | ${device.vendor} ${device.description} (${device.supports}) | ![${device.model}](images/devices/${device.model.replace('/', '-')}.jpg) |\n`;
2018-04-13 10:00:17 -07:00
});
2018-04-21 10:19:51 -07:00
return result;
2018-04-13 10:00:17 -07:00
}
2018-04-18 13:27:44 -07:00
const vendors = Array.from(new Set(Object.values(deviceMapping).map((d) => d.vendor)));
vendors.sort();
vendors.forEach((vendor) => {
2018-04-21 10:19:51 -07:00
text += `### ${vendor}\n`;
text += logDevices(Object.values(deviceMapping).filter((d) => d.vendor === vendor));
text += '\n';
2018-04-18 13:27:44 -07:00
})
2018-04-21 10:19:51 -07:00
fs.writeFileSync(outputdir + '/' + file, text);
2018-04-25 10:41:20 -07:00
file = 'Integrating-with-Home-Assistant.md';
text = '*NOTE: Automatically generated by `npm run docgen`*\n\n';
2018-04-25 10:41:20 -07:00
text += 'The easiest way to integrate zigbee2mqtt with Home Assistant is by using [MQTT discovery](https://www.home-assistant.io/docs/mqtt/discovery/).'
text += ' To enable MQTT discovery set `homeassistant: true` in your zigbee2mqtt `configuration.yaml` and add the following to your Home Assistant `configuration.yaml`.\n'
text += '```yaml\n'
text += 'mqtt:\n'
text += ' discovery: true\n'
text += '```\n'
text += '\n\n'
2018-04-21 14:01:52 -07:00
2018-04-25 10:41:20 -07:00
text += 'To respond to button clicks you can use the following Home Assistant configuration:\n'
2018-04-21 14:01:52 -07:00
text += '```yaml'
text += `
automation:
- alias: Respond to button clicks
trigger:
platform: mqtt
topic: 'zigbee2mqtt/<FRIENDLY_NAME'
condition:
condition: template
value_template: "{{ 'single' == trigger.payload_json.click }}"
action:
entity_id: light.bedroom
service: light.toggle
`
text += '```\n'
2018-05-11 11:05:33 -07:00
text += '**When changing a `friendly_name` for a device you first have to start zigbee2mqtt and after that restart Home Assistant in order to discover the new device ID.**\n\n';
2018-04-25 10:41:20 -07:00
text += 'In case you **dont** want to use Home Assistant MQTT discovery you can use the configuration below.\n\n'
const homeassistantConfig = (device) => {
2018-04-21 13:46:52 -07:00
const payload = {
platform: 'mqtt',
state_topic: "zigbee2mqtt/<FRIENDLY_NAME>",
availability_topic: "zigbee2mqtt/bridge/state",
...device.discovery_payload,
2018-04-21 13:46:52 -07:00
};
if (payload.command_topic) {
payload.command_topic = `zigbee2mqtt/<FRIENDLY_NAME>/set`;
}
2018-04-21 13:46:52 -07:00
let yml = YAML.stringify([payload]);
yml = yml.replace(/(-) \n /g, '- ');
yml = yml.replace('---', `${device.type}:`)
return yml;
}
Object.values(deviceMapping).forEach((device) => {
text += `### ${device.model}\n`;
text += '```yaml\n'
device.homeassistant.forEach((d, i) => {
text += homeassistantConfig(d);
if (device.homeassistant.length > 1 && i < device.homeassistant.length - 1) {
text += '\n';
}
})
text += '```\n\n';
});
fs.writeFileSync(outputdir + '/' + file, text);