Renamed output binary to avoid conflict
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Dreaded_X 2022-11-19 03:55:09 +01:00
parent c091ad0782
commit 20e7e830a6
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
5 changed files with 133 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
storage/ storage/
automation app
.env .env
tmp/ tmp/

View File

@ -7,13 +7,13 @@ RUN go mod download
COPY . . COPY . .
RUN go build RUN go build -o app
FROM golang:alpine FROM golang:alpine
WORKDIR /app WORKDIR /app
COPY --from=build-automation /src/automation /app/automation COPY --from=build-automation /src/app /app/app
COPY --from=build-automation /src/config.yml /app/config.yml COPY --from=build-automation /src/config.yml /app/config.yml
CMD ["/app/automation"] CMD ["/app/app"]

14
automation/automation.go Normal file
View File

@ -0,0 +1,14 @@
package automation
import (
"automation/home"
"automation/integration/hue"
"automation/integration/ntfy"
paho "github.com/eclipse/paho.mqtt.golang"
)
func RegisterAutomations(client paho.Client, hue *hue.Hue, notify *ntfy.Notify, home *home.Home) {
presenceAutomation(client, hue, notify, home)
mixerAutomation(client, home)
}

56
automation/mixer.go Normal file
View File

@ -0,0 +1,56 @@
package automation
import (
"automation/device"
"automation/home"
"automation/integration/kasa"
"encoding/json"
"log"
paho "github.com/eclipse/paho.mqtt.golang"
)
func mixerAutomation(client paho.Client, home *home.Home) {
var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) {
mixer, err := device.GetDevice[*kasa.Outlet](&home.Devices, "living_room/mixer")
if err != nil {
log.Println(err)
return
}
speakers, err := device.GetDevice[*kasa.Outlet](&home.Devices, "living_room/speakers")
if err != nil {
log.Println(err)
return
}
var message struct {
Action string `json:"action"`
}
err = json.Unmarshal(msg.Payload(), &message)
if err != nil {
log.Println(err)
return
}
if message.Action == "on" {
if mixer.GetOnOff() {
mixer.SetOnOff(false)
speakers.SetOnOff(false)
} else {
mixer.SetOnOff(true)
}
} else if message.Action == "brightness_move_up" {
if speakers.GetOnOff() {
speakers.SetOnOff(false)
} else {
speakers.SetOnOff(true)
mixer.SetOnOff(true)
}
}
}
if token := client.Subscribe("test/remote", 1, handler); token.Wait() && token.Error() != nil {
log.Println(token.Error())
}
}

59
automation/presence.go Normal file
View File

@ -0,0 +1,59 @@
package automation
import (
"automation/device"
"automation/home"
"automation/integration/hue"
"automation/integration/ntfy"
"automation/presence"
"encoding/json"
"fmt"
"log"
paho "github.com/eclipse/paho.mqtt.golang"
)
func presenceAutomation(client paho.Client, hue *hue.Hue, notify *ntfy.Notify, home *home.Home) {
var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) {
if len(msg.Payload()) == 0 {
// In this case we clear the persistent message
return
}
var message presence.Message
err := json.Unmarshal(msg.Payload(), &message)
if err != nil {
log.Println(err)
return
}
fmt.Printf("Presence: %t\n", message.State)
// Set presence on the hue bridge
hue.SetFlag(41, message.State)
if !message.State {
log.Println("Turn off all the devices")
// Turn off all devices
// @TODO Maybe allow for exceptions, could be a list in the config that we check against?
for _, dev := range home.Devices {
switch d := dev.(type) {
case device.OnOff:
d.SetOnOff(false)
}
}
// @TODO Turn off nest thermostat
} else {
// @TODO Turn on the nest thermostat again
}
// Notify users of presence update
notify.Presence(message.State)
}
if token := client.Subscribe("test/remote", 1, handler); token.Wait() && token.Error() != nil {
log.Println(token.Error())
}
}