summaryrefslogtreecommitdiff
path: root/shelly.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-03-23 21:34:58 +0100
committerxengineering <me@xengineering.eu>2026-03-23 21:36:30 +0100
commit5296918ff6e1fa5ded9091489050c92b6285e00e (patch)
tree19007bb7d38b5dc1fc8a64c61d41cf66c65d8bd8 /shelly.go
parent2463ce39f4aeb99e38b5d7f83d0179e9547aa3eb (diff)
downloadsia-server-5296918ff6e1fa5ded9091489050c92b6285e00e.tar
sia-server-5296918ff6e1fa5ded9091489050c92b6285e00e.tar.zst
sia-server-5296918ff6e1fa5ded9091489050c92b6285e00e.zip
Add Shelly cover message parsingshelly
This results in the information of which command is to issue and which IP address the command has to be sent to. This is what is needed to deliver the message with Websockets. This delivery is the last step to implement basic Shelly cover support.
Diffstat (limited to 'shelly.go')
-rw-r--r--shelly.go44
1 files changed, 43 insertions, 1 deletions
diff --git a/shelly.go b/shelly.go
index 0d33182..0f133bc 100644
--- a/shelly.go
+++ b/shelly.go
@@ -1,11 +1,53 @@
package main
import (
+ "fmt"
"log"
+ "net"
+ "strings"
)
func ShellyRun(config ShellyConfigs, rx chan MQTTMessage) {
for message := range rx {
- log.Printf("Got MQTT message: %v", message)
+ ip, command, err := parseMessage(config, message)
+ if err != nil {
+ log.Println(err)
+ continue
+ }
+
+ log.Printf("Send '%s' to '%s'.", command, ip)
+ }
+}
+
+func parseMessage(config ShellyConfigs, m MQTTMessage) (ip *net.IP, command string, err error) {
+ elements := strings.Split(m.Topic, "/")
+
+ if len(elements) != 3 {
+ return nil, "", fmt.Errorf(
+ "Expected three topic levels but got %d in '%s'.",
+ len(elements), m.Topic,
+ )
}
+
+ switch string(m.Payload) {
+ case "extend":
+ command = "Cover.Close"
+ case "retract":
+ command = "Cover.Open"
+ case "stop":
+ command = "Cover.Stop"
+ default:
+ return nil, "", fmt.Errorf("Invalid payload '%s'.", m.Payload)
+ }
+
+ id := elements[1]
+
+ for _, c := range config {
+ if c.ID == id {
+ ip := net.ParseIP(c.IP)
+ return &ip, command, nil
+ }
+ }
+
+ return nil, "", fmt.Errorf("Got message for unknown cover '%s'", id)
}