Sunday, July 24, 2016

Home Assistant, custom events and Amazon Dash Button

Last post had high level over view of Amazon Dash hacking options that have already been discovered, along with an experiment with the Amazon Dash button with my router.

HA (like many home hub solution) offers custom events, docs/wiki for this are at https://home-assistant.io/getting-started/automation-trigger/

I was able to get a custom event/trigger created with the following config:

automation:
  - alias: Turn on a single light when custom event fires
    # curl -X POST http://localhost:8123/api/events/MY_CUSTOM_EVENT
    trigger:
      platform: event
      event_type: MY_CUSTOM_EVENT
    action:
      service: homeassistant.turn_on
      entity_id: switch.switch_name

The switch name can be either derived by scanning through your config or by copy/pasting from the web UI http://localhost:8123/devState (or what ever machie HA is running on). It could even be a group etc.

Triggering this can be easily achieved via the REST interface HA offer https://home-assistant.io/developers/rest_api/ 

As per the config example comment above, using curl is an easy way to send the event. This example is without any security, a password can be setup (presumably TLS over https too?).

My router is running dd-wrt and does NOT have curl so my terrible idea is to use netcat, and forgo security (i.e. anyone on the network can inject events) by updating the script that DNSMasq fires by manually sending an incomplete/minimal POST message:

#!/bin/sh

# To be called by/from DNSMasq as dhcp-script # More information, see http://somnambulistic-monkey.blogspot.com/2016/07/home-assistant-custom-events-and-amazon.html if [ -z "$2" ] then echo 'Missing param 2 (MAC address)' echo This script should be called by DNSMasq exit 1 fi HA_IP_ADDRESS=192.168.1.1 # Edit me! HA_IP_PORT=8123 # Send custom event to Home Assistant server # this really should use curl but it is not available # so make do with nc (netcat) { echo POST /api/events/MY_CUSTOM_EVENT_${2} HTTP/1.1.$'\r' echo Host: ${HA_IP_ADDRESS}$'\r' echo $'\r' } | nc ${HA_IP_ADDRESS} ${HA_IP_PORT}

This includes the MAC address of the Dash button in the event name (rather than making use of POST data payload, i.e. custom event parameter "event_data")

This means a custom trigger is created for each button and the HA config is the only place that needs updating, rather than the dns script.

I need to experiment with the range of the buttons now, so far all testing has been within 5m of the access point. But I'm now all setup for using these how I want :-)

One thing that other posts have mentioned is that uninstalling the Amazon store app may be desirable as it will remind you that the Dash button setup is not complete. This has been understated. I had about 15 notifications in just a few minutes!

2 comments:

  1. Thanks for putting your experience into this post. I think we shared a lot of the same pain: DD-WRT not supporting cURL, being shipped with a version of wget that doesn't allow headers to be set (wtf?).

    I almost went the route of installing opkg on the router, which would have involved mounting a USB drive and following years-old (and probably no longer working) instructions, to be able to install cURL.

    The first time I saw your post and your usage of netcat I noped right out of here but ended up coming crawling back to use it exactly as you had it.

    I also was going the MQTT route (there's an opkg library for mosquitto, but didn't feel like installing opkg). I found a standalone MQTT client that did the job, but home-assistant doesn't have a built in event handler for MQTT messages, or at least none that I could find. There's just support for specific states being sent (e.g. ON and OFF) which isn't exactly conducive to the dash button.

    FYI instead of uninstalling the Amazon store app, you can disable notifcations for the dash button in My Account -> Dash Devices -> Manage Dash Buttons -> Notifications Settings

    What are you using your dash button for?

    ReplyDelete
  2. Thanks for taking the time to read and post feedback (good to hear about the disable notifications option). I knew I could use netcat as a fake curl but I ummed-and-ahhed over implementing it for a while as it just felt so wrong!

    I have two buttons:

    1) is used as an ALL OFF button and it lives near the front door of the house. When I leave I know it will shut everything off. Right now that means lights but I may hook up the thermostat to turn off any overrides (e.g. ensure fan is off if it has been overridden and turn heat back to schedule if it has been overridden). The short delay the button has is not an issue here. If I had some good presence-detection mechanisms in place this would not be needed :-)

    2) is dedicated to a single lamp and toggles the state between ON/OFF. I briefly toyed with the idea of doing some fancy dimming progression but I don't need it for this button.

    RE MQTT I briefly looked into it, I believe the built in MQTT queue HA has can be used but I don't have a use case for it.

    ReplyDelete