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' ) ;
2018-04-21 13:41:27 -07:00
const YAML = require ( 'json2yaml' ) ;
2018-05-16 10:40:05 -07:00
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' ;
2018-05-12 06:05:18 -07:00
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
}
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-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'
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-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'
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'
2018-05-16 10:40:05 -07:00
const configurations = homeassistant . mapping [ device . model ]
configurations . forEach ( ( d , i ) => {
2018-04-21 14:05:53 -07:00
text += homeassistantConfig ( d ) ;
2018-05-16 10:40:05 -07:00
if ( configurations . length > 1 && i < configurations . length - 1 ) {
2018-04-21 14:05:53 -07:00
text += '\n' ;
}
} )
text += '```\n\n' ;
2018-04-21 13:41:27 -07:00
} ) ;
fs . writeFileSync ( outputdir + '/' + file , text ) ;