Reorganized code, added google home intergrations and added zigbee2mqtt kettle
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
50
presence/presence.go
Normal file
50
presence/presence.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package presence
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
type Presence struct {
|
||||
Presence chan bool
|
||||
devices map[string]bool
|
||||
current *bool
|
||||
}
|
||||
|
||||
func New() Presence {
|
||||
return Presence{Presence: make(chan bool), devices: make(map[string]bool), current: nil}
|
||||
}
|
||||
|
||||
// Handler got automation/presence/+
|
||||
func (p *Presence) PresenceHandler(client mqtt.Client, msg mqtt.Message) {
|
||||
name := strings.Split(msg.Topic(), "/")[2]
|
||||
if len(msg.Payload()) == 0 {
|
||||
// @TODO What happens if we delete a device that does not exist
|
||||
delete(p.devices, name)
|
||||
} else {
|
||||
value, err := strconv.Atoi(string(msg.Payload()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p.devices[name] = value == 1
|
||||
}
|
||||
|
||||
present := false
|
||||
fmt.Println(p.devices)
|
||||
for _, value := range p.devices {
|
||||
if value {
|
||||
present = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if p.current == nil || *p.current != present {
|
||||
p.current = &present
|
||||
p.Presence <- present
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user