Flic buttons and Home Assistant

Flic buttons are Bluetooth-powered smart buttons that can be used to control other devices via their smartphone apps (Apple, Android), or using any number of integrations they provide on GitHub: https://github.com/50ButtonsEach/.

As I already use Home Assistant, and I had an Amazon Dash Button controller to work from, I wasn’t interested in installing one of the smartphone apps. Rather, because I already had a few Raspberry Pis, I opted for their Linux SDK, which includes pre-built binaries for the Pi, amongst other platforms.

Be aware that the SDK requires exclusive access to a Bluetooth controller. If you aren’t using Bluetooth for other purposes, the Pi’s controller on hci0 will suffice. Otherwise, you will need a second Bluetooth device.

Lucky enough to have my second Pi’s Bluetooth controller available, I installed flicd on that device; its central placement in our apartment was an added bonus.

Installing flicd

The aforementioned Linux SDK includes builds for several platforms:

Binaries and libraries ha[ve] been compiled for x86_64, i386 and armv6l. The minimum Linux kernel supported is 3.13. All code has been compiled and tested on Ubuntu 15.10 for desktop and Raspbian Jess[ie].

After checking out the latest release, copy the appropriate binary from bin/[RELEASE]/flicd to your device’s /usr/local/bin.

With the binary installed, it’s time to create the directories and init scripts necessary to run the flicd daemon. As my experience is with installing this on a Pi, the following will reflect that. First:

mkdir /home/pi/.flic
sudo touch /etc/systemd/system/flicd.service

Next, open flicd.service in your favorite editor and add the following:

[Unit]
Description=flicd Service
After=bluetooth.service
Requires=bluetooth.service

[Service]
TimeoutStartSec=0
ExecStart=/usr/local/bin/flicd -f /home/pi/.flic/daemon.db -s 0.0.0.0 -l /var/log/flicd.log -w
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

To enable the new flicd service, run:

sudo systemctl enable /etc/systemd/system/flicd.service

Use sudo ps aux | grep "flicd" and sudo netstat -antp | grep "5551" to confirm that flicd is running. If the service isn’t running, check the log at /var/log/flicd.log.

Discovering Buttons

In the same checkout of the SDK that you copied flicd from, locate the simpleclient directory. From there, run the command make to build the simpleclient binary. Once compiled, start the binary with:

./simpleclient 127.0.0.1

Now, discover buttons by running the startScanWizard command and following the prompts. For each button discovered, make note of its MAC address.

Installing the Home Assistant Controller

Note: If you’ve upgraded to Home Assistant 0.35, Flic support is now built in, rendering the balance of this post unnecessary.

Once you’ve captured the Bluetooth addresses for your buttons, it’s time to install and configure the node library that will connect flicd with Home Assistant.

First, install the Controller, as described in the Install section at https://git.ethitter.com/open-source/flic-button-home-assistant-controller.

Next, copy config-sample.json to config.json and update the flicd and home_assistant parameters. Then, using the Bluetooth addresses captured in the preceding section, set the interactions with Home Assistant by way of the buttons object. For example, to control a switch:

"buttons": {
  "aa:bb:cc:dd:ee:ff": {
    "mac": "aa:bb:cc:dd:ee:ff",
    "label": "Switch",
    "status": {
      "entity_id": "switch.ceiling_light"
    },
    "on": {
      "entity": "switch",
      "entity_id": "switch.ceiling_light",
      "entity_action": "turn_on"
    },
    "off": {
      "entity": "switch",
      "entity_id": "switch.ceiling_light",
      "entity_action": "turn_off"
    }
  }
}

Add as many entries to the buttons object as you have Flic buttons.

When done, start the script, preferably using pm2. Otherwise, npm start will suffice. Press your Flic buttons and confirm that the results are as expected.

One thought on “Flic buttons and Home Assistant”

Leave a Reply

Your email address will not be published. Required fields are marked *