102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
|
package internal
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"github.com/jcollie/go-homeassistant"
|
||
|
"github.com/jcollie/go-streamdeck"
|
||
|
)
|
||
|
|
||
|
// DynamicIconTextButton .
|
||
|
type DynamicIconTextButton struct {
|
||
|
sync.Mutex
|
||
|
ha *homeassistant.Connection
|
||
|
sd *streamdeck.StreamDeck
|
||
|
lastState string
|
||
|
pressed bool
|
||
|
EntityID string
|
||
|
Button ButtonInfo
|
||
|
Icon DynamicIconTextInfo
|
||
|
Service ServiceInfo
|
||
|
}
|
||
|
|
||
|
// DynamicIconTextInfo .
|
||
|
type DynamicIconTextInfo struct {
|
||
|
ForState func(bool, string) streamdeck.IconTextButton
|
||
|
}
|
||
|
|
||
|
// NewDynamicIconTextButton .
|
||
|
func NewDynamicIconTextButton(
|
||
|
ha *homeassistant.Connection,
|
||
|
sd *streamdeck.StreamDeck,
|
||
|
entityID string,
|
||
|
button ButtonInfo,
|
||
|
icon DynamicIconTextInfo,
|
||
|
service ServiceInfo,
|
||
|
) *DynamicIconTextButton {
|
||
|
b := new(DynamicIconTextButton)
|
||
|
b.ha = ha
|
||
|
b.sd = sd
|
||
|
b.EntityID = entityID
|
||
|
b.Button = button
|
||
|
b.Icon = icon
|
||
|
b.Service = service
|
||
|
myButtons = append(myButtons, b)
|
||
|
sd.SetCallback(b.Button.X, b.Button.Y, b)
|
||
|
return b
|
||
|
}
|
||
|
|
||
|
// InitializeButton .
|
||
|
func (b *DynamicIconTextButton) InitializeButton() {
|
||
|
b.Lock()
|
||
|
defer b.Unlock()
|
||
|
iconTextButton := b.Icon.ForState(b.pressed, b.lastState)
|
||
|
b.sd.FillIconText(b.Button.X, b.Button.Y, iconTextButton)
|
||
|
}
|
||
|
|
||
|
// StateChange .
|
||
|
func (b *DynamicIconTextButton) StateChange(entityID string, newState string) {
|
||
|
if entityID == b.EntityID {
|
||
|
b.Lock()
|
||
|
defer b.Unlock()
|
||
|
b.lastState = newState
|
||
|
iconTextButton := b.Icon.ForState(b.pressed, b.lastState)
|
||
|
b.sd.FillIconText(b.Button.X, b.Button.Y, iconTextButton)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// ButtonPressed .
|
||
|
func (b *DynamicIconTextButton) ButtonPressed(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) {
|
||
|
b.Lock()
|
||
|
defer b.Unlock()
|
||
|
log.Printf("pressed %d %d %s\n", x, y, timestamp)
|
||
|
b.pressed = true
|
||
|
iconTextButton := b.Icon.ForState(b.pressed, b.lastState)
|
||
|
b.sd.FillIconText(b.Button.X, b.Button.Y, iconTextButton)
|
||
|
}
|
||
|
|
||
|
// ButtonReleased .
|
||
|
func (b *DynamicIconTextButton) ButtonReleased(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) {
|
||
|
b.Lock()
|
||
|
defer b.Unlock()
|
||
|
log.Printf("released %d %d %s\n", x, y, timestamp)
|
||
|
b.pressed = false
|
||
|
iconTextButton := b.Icon.ForState(b.pressed, b.lastState)
|
||
|
b.sd.FillIconText(b.Button.X, b.Button.Y, iconTextButton)
|
||
|
b.ha.CallService(b.Service.Domain, b.Service.Service, b.Service.Data, b)
|
||
|
}
|
||
|
|
||
|
// HandleResult .
|
||
|
func (b *DynamicIconTextButton) HandleResult(ha *homeassistant.Connection, id uint64, success bool, result json.RawMessage) {
|
||
|
log.Printf("result: %v %s\n", success, string(result))
|
||
|
ha.RemoveHandler(id)
|
||
|
}
|
||
|
|
||
|
// HandleClose .
|
||
|
func (b *DynamicIconTextButton) HandleClose(ha *homeassistant.Connection, id uint64) {
|
||
|
log.Printf("home assistant closed")
|
||
|
}
|