Skip to content

vbaksa/smartthings-go-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smartthings Golang SDK

Build Status Go Report Card

SmartThings Golang SDK gives you ability to control and integrate smart home devices programmatically. SDK controls your home devices by interacting with SmarThings API

Usage samples

API is using Oauth2 Bearer Token authentication method. You can generate token by using Personal Token Page

Getting device details

Usage example below demonstrates how to retrieve device details and capabilities

client := sdk.NewClient("access-token-goes-here")
deviceList, err := client.DevicesList()
if err != nil {
    panic(err)
}
for _, device := range deviceList.Devices {

    fmt.Printf("Device name: %s \n", device.Label)

    //Following code retrieves device full details
    status, _ := client.DeviceFullStatus(device.DeviceID)

    //Let check does the device supports color controls
    if status.Component["main"].ColorControl != nil {
        fmt.Printf("Device %s hue level is %v \n", device.Label, status.Component["main"].ColorControl.Hue)
    }
    //Let's check does the device supports switch capability
    if status.Component["main"].Switch != nil {
        fmt.Printf("Device %s Switch is %v \n", device.Label, status.Component["main"].Switch.Switch.Value)
    }
}

Code could be simplified by using SDK method sdk.DeviceSupportsCapability

Usage example

client := sdk.NewClient("access-token-goes-here")
deviceList, err := client.DevicesList()
if err != nil {
    panic(err)
}
for _, device := range deviceList.Devices {
    if sdk.DeviceSupportsCapability(capabilities.WaterSensor, device) {
        status, _ := client.DeviceFullStatus(device.DeviceID)
        fmt.Printf("Water Leak Sensor %s status is %v \n", device.Label, deviceStatus.Component["main"].WaterSensor.Water.Value)
    }
}

Controlling devices

Various device capabilities and components can be controlled by using SDK. Some usage examples are below.

Example: Changing color of all lights

client := sdk.NewClient("access-token-goes-here")
deviceList, err := client.DevicesList()
if err != nil {
    panic(err)
}
for _, device := range deviceList.Devices {
    if sdk.DeviceSupportsCapability(capabilities.ColorControl, device) {
        err = client.DeviceCommand(device.DeviceID, sdk.NewColorControlHex(colors.GetRandomColorInHex()))
        if err != nil {
            panic(err)
        }
    }
}

Example: Switching off all switches

client := sdk.NewClient("access-token-goes-here")
deviceList, err := client.DevicesList()
if err != nil {
    panic(err)
}
for _, device := range deviceList.Devices {
    if sdk.DeviceSupportsCapability(capabilities.Switch, device) {
        err = client.DeviceCommand(device.DeviceID, sdk.NewSwitchOff())
        if err != nil {
            panic(err)
        }
    }
}

Example: Issuing custom device command

client := sdk.NewClient("access-token-goes-here")
//Create custom command
command := &sdk.DeviceCommand{
    Component:  "main",
    Capability: "switch",
    Command:    "on",
    Arguments:  []map[string]interface{}{},
}
//issue the command
err = client.DeviceCommand(device.DeviceID, *command)

if err != nil {
    panic(err)
}

About

Unofficial SmartThings Golang SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages