More refactoring, made zigbee2mqtt prefix a config option
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -5,12 +5,13 @@ import (
|
||||
"automation/home"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
paho "github.com/eclipse/paho.mqtt.golang"
|
||||
)
|
||||
|
||||
func DevicesHandler(client paho.Client, home *home.Home) {
|
||||
func DevicesHandler(client paho.Client, prefix string, home *home.Home) {
|
||||
var handler paho.MessageHandler = func(client paho.Client, msg paho.Message) {
|
||||
var devices []Info
|
||||
json.Unmarshal(msg.Payload(), &devices)
|
||||
@@ -24,6 +25,7 @@ func DevicesHandler(client paho.Client, home *home.Home) {
|
||||
for _, d := range devices {
|
||||
switch d.Description {
|
||||
case "Kettle":
|
||||
d.MQTTAddress = fmt.Sprintf("%s/%s", prefix, d.FriendlyName.String())
|
||||
kettle := NewKettle(d, client, home.Service)
|
||||
home.AddDevice(kettle)
|
||||
}
|
||||
@@ -35,7 +37,7 @@ func DevicesHandler(client paho.Client, home *home.Home) {
|
||||
home.Service.RequestSync(context.Background(), home.Username)
|
||||
}
|
||||
|
||||
if token := client.Subscribe("zigbee2mqtt/bridge/devices", 1, handler); token.Wait() && token.Error() != nil {
|
||||
if token := client.Subscribe(fmt.Sprintf("%s/bridge/devices", prefix), 1, handler); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ type kettle struct {
|
||||
func NewKettle(info Info, client paho.Client, service *google.Service) *kettle {
|
||||
k := &kettle{info: info, client: client, service: service, updated: make(chan bool, 1)}
|
||||
|
||||
if token := k.client.Subscribe(fmt.Sprintf("zigbee2mqtt/%s", k.info.FriendlyName), 1, k.stateHandler); token.Wait() && token.Error() != nil {
|
||||
if token := k.client.Subscribe(k.info.MQTTAddress, 1, k.stateHandler); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
|
||||
@@ -35,13 +35,11 @@ func NewKettle(info Info, client paho.Client, service *google.Service) *kettle {
|
||||
}
|
||||
|
||||
func (k *kettle) stateHandler(client paho.Client, msg paho.Message) {
|
||||
var payload struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
var payload OnOffState
|
||||
json.Unmarshal(msg.Payload(), &payload)
|
||||
|
||||
// Update the internal state
|
||||
k.isOn = payload.State == "ON"
|
||||
k.isOn = payload.State
|
||||
k.online = true
|
||||
|
||||
// Notify that the state has updated
|
||||
@@ -68,7 +66,7 @@ func (k *kettle) IsZigbeeDevice() {}
|
||||
|
||||
// zigbee.Device
|
||||
func (k *kettle) Delete() {
|
||||
if token := k.client.Unsubscribe(fmt.Sprintf("zigbee2mqtt/%s", k.info.FriendlyName)); token.Wait() && token.Error() != nil {
|
||||
if token := k.client.Unsubscribe(k.info.MQTTAddress); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
}
|
||||
@@ -167,7 +165,7 @@ func (k *kettle) SetOnOff(state bool) {
|
||||
msg = "ON"
|
||||
}
|
||||
|
||||
if token := k.client.Publish(fmt.Sprintf("zigbee2mqtt/%s/set", k.info.FriendlyName), 1, false, fmt.Sprintf(`{ "state": "%s" }`, msg)); token.Wait() && token.Error() != nil {
|
||||
if token := k.client.Publish(fmt.Sprintf("%s/set", k.info.MQTTAddress), 1, false, fmt.Sprintf(`{ "state": "%s" }`, msg)); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
}
|
||||
|
||||
34
integration/zigbee/payload.go
Normal file
34
integration/zigbee/payload.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package zigbee
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type OnOffState struct {
|
||||
State bool
|
||||
}
|
||||
|
||||
func (k *OnOffState) UnmarshalJSON(data []byte) error {
|
||||
var payload struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
k.State = payload.State == "ON"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type RemoteAction string
|
||||
|
||||
const (
|
||||
ACTION_ON RemoteAction = "on"
|
||||
ACTION_OFF = "off"
|
||||
ACTION_BRIGHTNESS_UP = "brightness_move_up"
|
||||
ACTION_BRIGHTNESS_DOWN = "brightness_move_down"
|
||||
ACTION_BRIGHTNESS_STOP = "brightness_move_down"
|
||||
)
|
||||
|
||||
type RemoteState struct {
|
||||
Action RemoteAction `json:"action"`
|
||||
}
|
||||
@@ -9,6 +9,8 @@ type Info struct {
|
||||
Manufacturer string `json:"manufacturer"`
|
||||
ModelID string `json:"model_id"`
|
||||
SoftwareBuildID string `json:"software_build_id"`
|
||||
|
||||
MQTTAddress string `json:"-"`
|
||||
}
|
||||
|
||||
type Device interface {
|
||||
|
||||
Reference in New Issue
Block a user