Skip to content

Instantly share code, notes, and snippets.

@pabloav pabloav/dashproxy.conf
Last active Apr 5, 2016

What would you like to do?
SmartThings integration with Amazon Dash buttons
# This is the xinetd configuration file for the dashproxy file
service dashproxy
socket_type = stream
protocol = tcp
wait = no
port = 9999
user = nobody
server = /usr/local/bin/
per_source = 1 # Very important! This is what combines with the sleep in the python script to avoid duplicate events
import sys
import syslog
import requests
import os
import string
import json
import time
import os
#from scapy.all import *
def oauth_setup():
# See
# for some idea of how to get these values
client_code="<your client code>" # From the first OAUTH request
oauth_client_id="00000000-38a8-4de7-bb19-2e3585aba2a2" # From the SmartApp page
oauth_client_secret="00000000-2aee-409f-af80-870763db0ded" # From the SmartApp page
# This is what the response of the second request gets you:
# {"access_token":"00000000-4a5a-4191-baa5-a03b9807329c","token_type":"bearer","expires_in":1576799999,"scope":"app"}
access_token = "00000000-4a5a-4191-baa5-a03b9807329c"
return { "access_token": access_token,
"token_type": "bearer",
"expires_in": 1576799999,
"client_code": client_code,
# The endpoint comes from the third API request
"endpoint_uri": "",
"headers": {"Authorization": "Bearer %s" % access_token },
"scope": "app"}
def light_name_to_id(light_name):
url = "%s/%s" % (info['endpoint_uri'], "switches")
# syslog.syslog("Endpoint: %s" % url)
r = requests.get(url, headers=info['headers'])
if r.status_code != 200:
syslog.syslog("HTTP Status: %s" % r.status_code)
return None
# syslog.syslog( r.text)
data = json.loads(r.text)
return data.get(light_name)
def toggle_st(light_name):
url = "%s/switches/%s" % (info['endpoint_uri'], switch_id)
syslog.syslog("Toggling %s with URL: %s" % ( light_name, url ))
data = '{"command":"toggle"}'
r = requests.put(url, data=data, headers=info['headers'])
def ip_to_light(ip):
octet_lookup = {
'117': "VSDash-Bounty", # Bounty
'160': "VSDash-Huggies", # Huggies
'228': "VSDash-Ziploc", # Ziploc
'238': "VSDash-Glad" # Glad
last_octet = string.split(remote_ip, '.')[3]
return octet_lookup.get(last_octet)
remote_ip = os.environ.get('REMOTE_HOST')
if remote_ip == None:
syslog.syslog("No IP detected")
light_name = ip_to_light(remote_ip)
if light_name == None:
syslog.syslog("Unknown IP connected - %s" % remote_ip)
syslog.syslog("%s - %s" % (remote_ip, light_name))
# This causes xinetd to close the connection
# We keep sleeping so that the retry attempts are suppressed by xinetd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.