How to add custom devices

I have a Raspberry Pi running homegrown software to control an RGB LED strip. Currently it has a REST API to control the strip. It is connected to the same network as homeassistant but does not have any other sort of connectivity (no zwave, zigbee, bluetooth, etc). I’m looking to integrate this device into homeassistant so I can build more robust automations around it. In homeassistant land I imagine it would have the following entities:

  • Brightness: 0-100 (editable)
  • Pattern: Fixed set of enumerations (editable)
  • Online: binary indicating whether the device is available or not (not editable)

I’ve struggled to find documentation on this topic. I’d prefer to stick with a rest API or possibly something UDP based, but the source code is mine so I can modify it as needed.

You would need two things, a REST entity and your device template entity. The template entity handles all the normal HA commands like turn on/off, brightness, etc, and your definition of the template tells it what to do when those are requested and, in your case, send a rest call.

So your rest might look like:

led_turn_on:
  url: "http://10.1.1.1?command=value"
  username: !secret led_username
  password: !secret led_password

And then your device template:

led_strip:
  friendly_name: "LED Strip"
  unique_id: my_led_strip
  turn_on:
    service: rest_command.led_turn_on
  turn_off:  
      ... your off command

It can obviously get more detailed from here, like getting the current state and attribute values via rest or even call a pyscript that handles the entire integration (my preferred method).

(edit) changed it to call the rest directly.

For anyone else that stumbles on this thread looking for a solution. I was able to get this working with the following:

Rest command setup:

# rest_command.yaml
pilights_brightness:
        url: "http://192.168.123.456/brightness/{{ states('input_number.pi_lights_brightness') }}"
        method: post

# configuration.yaml
....
rest_command: !include rest_command.yaml

Create a helper of type number (named pi_lights_brightness in this case)

Create an automation that fires off when the helper changes:

alias: Pi Lights Brightness Set
description: Update Pi Lights Brightness
trigger:
  - platform: state
    entity_id: input_number.pi_lights_brightness
condition: []
action:
  - service: rest_command.pilights_brightness
    data: {}
mode: single

Some notes:

  • I found that mDNS domains don’t work in the rest_command, hence the raw IP in the yaml. I didn’t bother digging deeper since it’s easy to just give the Pi a static lease
  • This implementation assumes that the Rest endpoint is always changed via Home Assistant. If it is changed via another mechanism, then home assistant can get out of sync with what is actually happening at the endpoint. This could be solved via a rest sensor and more templating & automation, but I didn’t bother since I plan to only control it via Home Assistant
  • Same approach can be used for other functions, such as using a Dropdown Helper and another rest command to set the pattern
1 Like

I have some custom devices and I am unable to add them on home assistant.
I have custom made smart LED blub . How can I add it to home assistant.

I’m a little lost on doing this from scratch with a custom device - is there a good overview of this in the documentation somewhere?

This was a while ago, but I vaguely recall having to dig through a lot of forum posts to land on the solution I posted. Which part are you lost on?

UPDATE: Got it working! I’m assuming the “rest_command.yaml” filename is important, I was using an arbitrary filename and it wasn’t working as expected.

The filename shouldn’t be important, but the key in configuration.yaml is important. So:

rest_command: !include rest_command.yaml

Should be no different than

rest_command: !include foobar.yaml