stjohnjohnson / smartthings-mqtt-bridge

Bridge between SmartThings and MQTT
https://hub.docker.com/r/stjohnjohnson/smartthings-mqtt-bridge/
MIT License
367 stars 243 forks source link

Auto-generate HA Config #52

Open kidhasmoxy opened 8 years ago

kidhasmoxy commented 8 years ago

Do you have any plans to automatically generate a config file (or set of includes) based on the information about the devices and their capabilities for the smart apps?

I rebuilt the functionality of your bridge in Node Red to learn it (and run the bridge on a Pi), and figured I would create a flow to automatically update the HA config file and optionally restart HA when new devices are found. Here's my ugly mustache template to give an idea of what I've hacked together so far:

{{#payload.capabilities.Switch}}
{{^payload.capabilities.SwitchLevel}}
switch {{payload.id}}-switch:
{{/payload.capabilities.SwitchLevel}}
{{#payload.capabilities.SwitchLevel}}
light {{payload.id}}-light:
{{/payload.capabilities.SwitchLevel}}
  platform: mqtt
  name: {{parts.key}}
  state_topic: "{{preface}}/{{parts.key}}/switch/{{state_suffix}}"
  command_topic: "{{preface}}/{{parts.key}}/switch/{{command_suffix}}"
  payload_on: "on"
  payload_off: "off"
{{#payload.capabilities.SwitchLevel}}
  brightness_state_topic: "{{preface}}/{{parts.key}}/level/{{state_suffix}}"
  brightness_command_topic: "{{preface}}/{{parts.key}}/level/{{command_suffix}}"
  brightness_scale: 99
{{/payload.capabilities.SwitchLevel}}
  optimistic: false
  qos: 0
  retain: true
{{/payload.capabilities.Switch}}

{{#payload.capabilities.MotionSensor}}
binary_sensor {{payload.id}}-motion:
  platform: mqtt
  name: {{parts.key}}
  state_topic: "{{preface}}/{{parts.key}}/motion/{{state_suffix}}"
  payload_on: "active"
  payload_off: "inactive"
  sensor_class: motion
  qos: 0
{{/payload.capabilities.MotionSensor}} 
kylerw commented 8 years ago

Very cool, would love to see this.

stjohnjohnson commented 8 years ago

OOooh, that would be quite cool. I'm in favor of this. I don't have any plans to add it personally, but I can see it certainly helping users out quite a bit. Do you want to give a PR to add this?

t0rx commented 8 years ago

I was thinking about something similar, but where the handler on Smartthings side would publish a list of all enabled devices to a single topic along with their configs, and then on the Hass side you just specify that discovery topic and it can then find everything else from there. Not sure which approach would be better.

kidhasmoxy commented 8 years ago

I can paste my node-red flow here, but I haven't modified the code for this app. I ended up changing the SmartApp to have subscribe push a block of device info. That's what I'm using in the Node-RED flow and calling in the template (checking for capabilities of the device to build the all the device blocks.)