Smartthings Control via Amazon Dash Button

Intro

Previously, I had shown you how to hack your Amazon dash button and use it for other things. I recently discovered that Smartthings has a REST API. I love REST APIs because it abstracts a lot of the technologies between manufacturers and allows you to do all sorts of automation. What we’re going to attempt here is to use the button as a replacement for a Wifi switch. Wifi switches are very popular and usually come in around $20-ish a pop. I will use one of my $1 dash buttons I got on sale to try and accomplish the same functionality.

If you haven’t already, check out my earlier post on how to setup the script that will be monitoring your network for activity. This is required as it will know when a button was pressed and take appropriate action.

After setting up the dash button and having it be recognized by our network, we need to get started on our script. Our script will be very simple. It needs to do two things:

  • Check the current status of the light we want to control
  • Toggle to the alternate state

For this to work you need to get a Smartthings API token. Luckily, Samsung is able to provide a personal token so you don’t have to go through all the OAuth flow you would normally have to when creating integrations with Smartthings.

Go here and get a new token. Make sure you make note of the token as it’s the only time you’ll be able to see it. Also, make sure you grant the following scopes when prompted:

  • x:devices:*
  • l:devices
  • r:devices:*

This will allow you to read, list and execute commands on devices.

Get Device IDs

In order to control the devices via the Smartthings REST API, we need to know the device IDs. You can do this via REST as well. I personally like to use Insomnia for this. It’s simple and easy.

Create a GET request with the following URL

https://api.smartthings.com/v1/devices

Remember to setup the authorization to be Bearer and add your token.

Python script

Now the fun part. At this point you’ve setup your button to be recognized on the network. We will ask amazon-dash to execute following script whenever it sees the device on the network.

#!/home/pi/pythonScripts/smartthingsRest/bin/python3 # path to your executable
import requests

# --------- User Settings ---------
TOKEN = 'yourtoken' # This is your smartthings token
SMARTTHINGS_URI = 'https://api.smartthings.com/v1'
MAIN_FLOOR_DEVICES = ['dc4af68a-3ccd-440f-8eec-35baee078154','ccd93b1c-852e-4383-ad13-86b063a8078e'] # array of device IDs you want to control
PREFERRED_BRIGHTNESS = 50 # my switches are dimmers, so I want to set brightness to 50%
headers = {"Authorization":"Bearer " + TOKEN}
# ---------------------------------

# this function turns on the device
def turnOn(device):
    try:
        payload = {
            "commands": [
                    {
                        "component": "main",
                        "capability": "switch",
                        "command": "on",
                        "arguments": []
                    },
                    {
                        "component": "main",
                        "capability": "switchLevel",
                        "command": "setLevel",
                        "arguments": [
                            PREFERRED_BRIGHTNESS
                        ]
                    }
                ]
        }
        url = SMARTTHINGS_URI + '/devices/' + device + '/commands'
        r = requests.post(url,headers = headers,json=payload)
        return True
    except exception as e:
        print("could not turn on")
        print(e)
        return False

def turnOff(device):
    try:
        payload = {
            "commands": [
                    {
                        "component": "main",
                        "capability": "switch",
                        "command": "off",
                        "arguments": []
                    }
                ]
        }
        url = SMARTTHINGS_URI + '/devices/' + device + '/commands'
        r = requests.post(url,headers = headers,json=payload)
        return True
    except exception as e:
        print("could not turn off")
        print(e)
        return False


try:
    for device in MAIN_FLOOR_DEVICES:
        url = SMARTTHINGS_URI + '/devices/' + device + '/status/'
        r = requests.get(url = url,headers = headers)
        if r.json()['components']['main']['switch']['switch']['value'] == "on":
            turnOff(device)
        elif r.json()['components']['main']['switch']['switch']['value'] == "off":
            turnOn(device)
except Exception as e:
    print("some error occured")
    print(e)

Conclusion

It works! The next step for me will be to set this up so that the button is recognized by Smartthings and can be programmed within the app. I haven’t tried it yet, but from the looks of the API it seems doable.

Let me know what you think or what ideas you have for something similar.

Leave a Reply

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