Honestly I don’t - I finally gave up on the dashbutton binding, as there were always issues after a restart: 1 to 2 of my three dashbuttons not coming online.
I chose a different approach by giving up on the dashbutton-binding and using a python script to listen to the network and get the DHCP request of the button when being clicked. It’s about 1-2 seconds faster than the binding approach (YMMV):
Prerequisite: you need to install the python ‘scapy’ and ‘requests’ libraries to scan for the DHCP messages the dashbutton is broadcasting when pressed:
sudo apt-get install python-scapy
sudo apt-get install python-requests
(the alternative way with
python -m pip install scapy
python -m pip install requests
should work as well).
In addition you need to install the tcpdump package for scapy to work:
sudo apt-get install tcpdump
Then these files have to be created:
/etc/openhab2/scripts/dashbutton/dash.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
from scapy.all import *
import sys, os
import requests, datetime
import syslog
import sys
def arp_display(pkt):
if pkt.haslayer(DHCP):
print (pkt[Ether].src)
for index in range(len(dash_mac)):
if pkt[Ether].src == dash_mac[index]: #die MAC Adresse des Button mit Kleinbuchstaben!
print "Dash-Button %d detected" % (index +1)
if (time.time() - dash_last[index]) > 5.0:
dash_last[index] = time.time()
url = ohabserver + dash_item[index] + "/state"
s = "ON"
print url +"=" + s
try:
myResponse = requests.put(url, s)
except:
syslog.syslog("Fehler beim Abruf von %(url)s : %(status)s" % {"url":url, "status":myResponse.status_code} )
else:
print "Aber doppelt, deswegen ignoriert..."
reload(sys)
sys.setdefaultencoding("utf-8")
# MODIFY THE FOLLOWING LINES ACCORDING TO YOUR NEEDS AND YOUR ENVIRONMENT
# The URL of your openhab-installation
ohabserver = "http://127.0.0.1:8080/rest/items/"
# The MAC addresses of the dashbuttons, please note the additional empty entry at the end of the real dashbuttons!!
dash_mac = ( "88:71:e5:5e:e0:d4", "fc:a6:67:48:09:15", "b4:7c:9c:e1:c0:d9", "")
# The item-names of the corresponding Dashboard entries in openhab (plus an empty one at the end)
dash_item = ("DashButton1", "DashButton2", "DashButton3", "")
# As many entries as there are dashbuttons plus 1
dash_last = [0.0, 0.0, 0.0, 0.0]
print sniff(prn=arp_display, store=0,count=0)
(please note that you have to amend the script in the last few lines according to your environment).
The corresponding service definition:
dash.service
[Unit]
Description=Amazon Dashbutton Reader
[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/bin/python /etc/openhab2/scripts/dashbutton/dash.py
Restart=always
[Install]
WantedBy=multi-user.target
to be installed with
sudo cp dash.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable dash.service
sudo systemctl start dash.service
systemctl status dash.service
For every dashbutton you need a corresponding openhab-item:
Switch DashButton1
[... more Dashbuttons...]
and a rule that gets fired when the python-Script sends a command to this button:
rule Dash1
Item DashButton1 received update ON
then
logInfo("dash.rules", "DashButton 1 was pressed")
// BEGIN OF ACTION
// e.g. otherButton.sendCommand(ON)
// END OF ACTION
// Reset the state of button, that it will see the next update...
DashButton1.postUpdate(OFF)
end
Drawback: the python-script needs root privileges
Advantage: rock solid