From 69a177076090e2095f5c30d56e6b4cbb59aa6eea Mon Sep 17 00:00:00 2001 From: Josh Deprez Date: Sun, 23 Apr 2023 10:39:35 +0000 Subject: [PATCH] Add CLI as example usage --- cmd/plugctl/main.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 cmd/plugctl/main.go diff --git a/cmd/plugctl/main.go b/cmd/plugctl/main.go new file mode 100644 index 0000000..7631145 --- /dev/null +++ b/cmd/plugctl/main.go @@ -0,0 +1,35 @@ +// The plugctl command provides manual CLI control over a Kasa KP105 smart plug. +package main + +import ( + "flag" + "fmt" + "log" + + "gitea.drjosh.dev/josh/plugctl" +) + +var kasaPlugAddr = flag.String("kasa-addr", "KP105:9999", "Address of Kasa smart plug") + +func main() { + flag.Parse() + + plug := plugctl.Plug{Addr: *kasaPlugAddr} + + switch flag.Arg(0) { + case "on": + if err := plug.TurnOn(); err != nil { + log.Printf("Couldn't turn on smart plug: %v", err) + } + case "off": + if err := plug.TurnOff(); err != nil { + log.Printf("Couldn't turn off smart plug: %v", err) + } + case "state": + state, err := plug.RelayState() + if err != nil { + log.Printf("Couldn't get current relay state: %v", err) + } + fmt.Println(state) + } +}