More refactoring, moved kettle auto off out of the kettle implementation and into a seperate automation
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:
@@ -13,6 +13,7 @@ import (
|
||||
type DeviceInterface interface {
|
||||
device.Basic
|
||||
|
||||
IsGoogleDevice()
|
||||
Sync() *Device
|
||||
Query() DeviceState
|
||||
Execute(execution Execution, updatedState *DeviceState) (errCode string, online bool)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package kasa
|
||||
|
||||
import (
|
||||
"automation/device"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
@@ -12,6 +13,9 @@ import (
|
||||
// https://www.softscheck.com/en/blog/tp-link-reverse-engineering/
|
||||
|
||||
type Device interface {
|
||||
device.Basic
|
||||
|
||||
IsKasaDevice()
|
||||
GetIP() string
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,9 @@ func NewOutlet(name device.InternalName, ip string) *Outlet {
|
||||
|
||||
// kasa.Device
|
||||
var _ Device = (*Outlet)(nil)
|
||||
func (*Outlet) IsKasaDevice() {}
|
||||
|
||||
// kasa.Device
|
||||
func (o *Outlet) GetIP() string {
|
||||
return o.ip
|
||||
}
|
||||
|
||||
@@ -35,6 +35,9 @@ func (c *computer) GetID() device.InternalName {
|
||||
|
||||
// google.DeviceInterface
|
||||
var _ google.DeviceInterface = (*computer)(nil)
|
||||
func (*computer) IsGoogleDevice() {}
|
||||
|
||||
// google.DeviceInterface
|
||||
func (c *computer) Sync() *google.Device {
|
||||
device := google.NewDevice(c.GetID().String(), google.TypeScene)
|
||||
device.AddSceneTrait(false)
|
||||
|
||||
@@ -30,6 +30,8 @@ func DevicesHandler(client paho.Client, home *home.Home) {
|
||||
}
|
||||
|
||||
// Send sync request
|
||||
// @TODO Instead of sending a sync request we should do something like home.sync <- interface{}
|
||||
// This will then restart a timer, that way the sync will only trigger once everything has settled from multiple locations
|
||||
home.Service.RequestSync(context.Background(), home.Username)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,21 +20,12 @@ type kettle struct {
|
||||
|
||||
updated chan bool
|
||||
|
||||
timerLength time.Duration
|
||||
timer *time.Timer
|
||||
stop chan interface{}
|
||||
|
||||
isOn bool
|
||||
online bool
|
||||
}
|
||||
|
||||
func NewKettle(info Info, client paho.Client, service *google.Service) *kettle {
|
||||
k := &kettle{info: info, client: client, service: service, updated: make(chan bool, 1), timerLength: 5 * time.Minute, stop: make(chan interface{})}
|
||||
k.timer = time.NewTimer(k.timerLength)
|
||||
k.timer.Stop()
|
||||
|
||||
// Start function
|
||||
go k.timerFunc()
|
||||
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 {
|
||||
log.Println(token.Error())
|
||||
@@ -64,27 +55,6 @@ func (k *kettle) stateHandler(client paho.Client, msg paho.Message) {
|
||||
k.service.ReportState(context.Background(), id, map[string]google.DeviceState{
|
||||
id: k.getState(),
|
||||
})
|
||||
|
||||
if k.isOn {
|
||||
k.timer.Reset(k.timerLength)
|
||||
} else {
|
||||
k.timer.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func (k *kettle) timerFunc() {
|
||||
for {
|
||||
select {
|
||||
case <- k.timer.C:
|
||||
log.Println("Turning kettle automatically off")
|
||||
if token := k.client.Publish(fmt.Sprintf("zigbee2mqtt/%s/set", k.info.FriendlyName), 1, false, `{"state": "OFF"}`); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
|
||||
case <- k.stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (k *kettle) getState() google.DeviceState {
|
||||
@@ -94,21 +64,20 @@ func (k *kettle) getState() google.DeviceState {
|
||||
|
||||
// zigbee.Device
|
||||
var _ Device = (*kettle)(nil)
|
||||
func (k *kettle) Delete() {
|
||||
k.stop <- struct{}{}
|
||||
func (k *kettle) IsZigbeeDevice() {}
|
||||
|
||||
if token := k.client.Subscribe(fmt.Sprintf("zigbee2mqtt/%s", k.info.FriendlyName), 1, k.stateHandler); token.Wait() && token.Error() != nil {
|
||||
// zigbee.Device
|
||||
func (k *kettle) Delete() {
|
||||
if token := k.client.Unsubscribe(fmt.Sprintf("zigbee2mqtt/%s", k.info.FriendlyName)); token.Wait() && token.Error() != nil {
|
||||
log.Println(token.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (k *kettle) IsZigbeeDevice() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// google.DeviceInterface
|
||||
var _ google.DeviceInterface = (*kettle)(nil)
|
||||
func (*kettle) IsGoogleDevice() {}
|
||||
|
||||
// google.DeviceInterface
|
||||
func (k *kettle) Sync() *google.Device {
|
||||
device := google.NewDevice(k.GetID().String(), google.TypeKettle)
|
||||
device.AddOnOffTrait(false, false)
|
||||
|
||||
@@ -14,7 +14,6 @@ type Info struct {
|
||||
type Device interface {
|
||||
device.Basic
|
||||
|
||||
// This function only exists to make this interface unique
|
||||
IsZigbeeDevice() bool
|
||||
IsZigbeeDevice()
|
||||
Delete()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user