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' ) ;
2018-04-21 13:41:27 -07:00
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-04-23 13:41:33 -07:00
In case you own a Zigbee device which is * * NOT * * listed here , please create an issue at : https : //github.com/Koenkk/zigbee2mqtt/issues. Support for it can probably be added very easily.
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' ;
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-21 13:41:27 -07:00
2018-04-25 10:41:20 -07:00
file = 'Integrating-with-Home-Assistant.md' ;
2018-04-21 13:41:27 -07:00
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'
2018-04-21 13:41:27 -07:00
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-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'
2018-04-21 13:41:27 -07:00
const homeassistantConfig = ( device ) => {
2018-04-21 13:46:52 -07:00
const payload = {
2018-04-21 13:41:27 -07:00
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:41:27 -07:00
2018-04-21 13:46:52 -07:00
let yml = YAML . stringify ( [ payload ] ) ;
2018-04-21 13:41:27 -07:00
yml = yml . replace ( /(-) \n /g , '- ' ) ;
yml = yml . replace ( '---' , ` ${ device . type } : ` )
return yml ;
}
Object . values ( deviceMapping ) . forEach ( ( device ) => {
2018-04-21 14:05:53 -07:00
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' ;
2018-04-21 13:41:27 -07:00
} ) ;
fs . writeFileSync ( outputdir + '/' + file , text ) ;