Skip to content

Instantly share code, notes, and snippets.

@gr4y
Last active September 25, 2017 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gr4y/f5dd33cd64f061e6b3b0 to your computer and use it in GitHub Desktop.
Save gr4y/f5dd33cd64f061e6b3b0 to your computer and use it in GitHub Desktop.
Toggle all WeMo switches. If it is on, it will turn off. If it is off, it will turn on. Just a quick little thing.
package main
import (
"fmt"
"github.com/huin/goupnp"
"github.com/huin/goupnp/soap"
)
func main() {
devices, err := goupnp.DiscoverDevices("urn:Belkin:device:controllee")
if err != nil {
fmt.Println("Error while discovering devices: ", err)
return
}
fmt.Println("Devices: ", len(devices))
if len(devices) > 0 {
for _, device := range devices {
services := device.Root.Device.FindService("urn:Belkin:service:basicevent:1")
fmt.Println("Services: ", len(services))
if len(services) > 0 {
for _, service := range services {
soapClient := service.NewSOAPClient()
stateObj := &struct {
BinaryState string
}{}
if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "GetBinaryState", stateObj, stateObj); err != nil {
fmt.Println("Error while performing action: ", err)
return
}
CurrentState, err := soap.UnmarshalBoolean(stateObj.BinaryState)
if err != nil {
fmt.Println("Error while unmarshaling BinaryState: ", err)
return
}
fmt.Println("CurrentState: ", stateObj.BinaryState)
NewState, err := soap.MarshalBoolean(!CurrentState)
if err != nil {
fmt.Println("Error while marshaling BinaryState: ", err)
return
}
fmt.Println("NewState: ", NewState)
stateObj.BinaryState = NewState
if err := soapClient.PerformAction("urn:Belkin:service:basicevent:1", "SetBinaryState", stateObj, stateObj); err != nil {
fmt.Println("Error while performing action: ", err)
return
}
}
}
}
} else {
fmt.Println("No WeMo Switch Devices Found!")
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment