[AppDaemon] Flic.io Buttons as Physical Switch

Hi all!

In 0.35.0 support was added for the Flic Smart Button, a BLE button that can connect to your smartphone or, more conveniently for us in the HASS community, a Linux environment. I had purchased 5 of these buttons from Shortcut Labs many months prior as I tried to build my own HA platform. These buttons are GREAT!

I first started off with hacking the Amazon Dash button as it would be a cheap way to have an internet connected physical button, but quickly found out it was too hit or miss and not fast enough for realtime use. Flic buttons don’t have that problem, check it out: https://youtu.be/PkgeFv3LPmY

Enough talking already, show me the code!



Code below, but also can be found here, if you prefer to see the syntax highlighted.

import appdaemon.appapi as appapi

class FlicSwitches(appapi.AppDaemon):

    def initialize(self):

        self.flic_switches = {
            'flic_80e4da716bb6': ['light.island_trio'],
            'flic_80e4da716bb7': ['light.entryway_overhead', 'light.entryway_hall', 'light.island_trio'],
            'flic_80e4da7178e4': ['light.living_room_lamp', 'light.island_trio'],
            'flic_80e4da71adc7': [],
            'flic_80e4da71adc9': ['light.bathroom_trio']
        }

        self.listen_event(self.switch_entity, event='flic_click')

    def switch_entity(self, event_name, data, kwargs):
        for entity_id in self.flic_switches[data['button_name']]:
            if data['click_type'] == 'single':
                self.toggle(entity_id)

It really is this simple! Let’s walk through this example like I have in my past posts.

In the def initialize section of this app, we’ll create a dictionary of all our flic_switches that are able to register events. What this will allow us to do is customize each button’s purpose and abilities. Then we will register an event listener with our callback being a function that allows us to toggle the entities associated with each button.

In def switch_entity itself, it’s as simple as looping through each of the entities paired with the button_name and, if the click_type was single, toggle that entity_id! I made it specific to datatype single so that the other click types could still be programmable and to show you how simple it is to specify which click should do what. :slight_smile:



This is a very basic example, which can certainly be expanded upon. Some ideas I can think of off the top of my head…

  • implement data['click_type'] for double and hold
  • ignore data['click_type'] == double via your configuration
  • allows you to implement a Queue of single click events to allow for triple, quad, penta, etc
  • use the datetime library or self.sun_up() or self.sun_down() to add time-sensitive contexts
  • call services from within HomeAssistant
  • order your favorite item off Amazon with their api and the requests library
  • trigger an IFTTT recipe
  • any number of other things!!

I personally use my Flic buttons as light switches, but I’ve only scratched the surface. single is toggle, but time-sensitive and tunes my lights to different brightness levels depending on the time of day. double will raise the brightness level by 20%. hold always turns the given lights off. I eventually have plans to disable the double event and add more contexts for automation - possibly even giving each button a SOS or panic mode ala Life Alert. :smiley:

If you need help with setup of the Flic service, I would check this post first – but you can always send me PM as well!



The next project I share will probably be a bit more complex than my last few. As always, more to come!

Thanks for reading,
SupahNoob

3 Likes