zigbee2mqtt/support/docgen.js

112 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-05-24 04:09:58 -07:00
const deviceMapping = require('zigbee-shepherd-converters').devices;
2018-04-21 10:19:51 -07:00
const fs = require('fs');
const YAML = require('json2yaml');
const homeassistant = require('../lib/homeassistant');
2018-04-11 11:54:22 -07:00
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-05-24 03:50:54 -07:00
const pathFriendly = device.model.replace(new RegExp("/", "g"), '-').replace(new RegExp(":", "g"), '-').replace(new RegExp(" ", "g"), '-');
2018-05-22 09:25:09 -07:00
result += `| ${device.model} | ${device.vendor} ${device.description} (${device.supports}) | ![${pathFriendly}](images/devices/${pathFriendly}.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
}
const vendors = Array.from(new Set(deviceMapping.map((d) => d.vendor)));
2018-04-18 13:27:44 -07:00
vendors.sort();
vendors.forEach((vendor) => {
2018-04-21 10:19:51 -07:00
text += `### ${vendor}\n`;
text += logDevices(deviceMapping.filter((d) => d.vendor === vendor));
2018-04-21 10:19:51 -07:00
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-06-04 08:26:59 -07:00
text += 'If you\'re hosting zigbee2mqtt using [this hassio addon-on](https://github.com/danielwelch/hassio-zigbee2mqtt) use their documentation on how to configure.\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;
}
deviceMapping.forEach((device) => {
text += `### ${device.model}\n`;
text += '```yaml\n'
const configurations = homeassistant.mapping[device.model]
configurations.forEach((d, i) => {
text += homeassistantConfig(d);
if (configurations.length > 1 && i < configurations.length - 1) {
text += '\n';
}
})
text += '```\n\n';
});
fs.writeFileSync(outputdir + '/' + file, text);