mystreamdeck/internal/dynamiciconbutton.go
2020-10-04 15:28:04 -05:00

126 lines
3 KiB
Go

package internal
import (
"encoding/json"
"log"
"sync"
"time"
"github.com/jcollie/go-homeassistant"
"github.com/jcollie/go-streamdeck"
)
// ServiceInfo .
type ServiceInfo struct {
Domain string
Service string
Data interface{}
}
// DynamicIconButton .
type DynamicIconButton struct {
sync.Mutex
ha *homeassistant.Connection
sd *streamdeck.StreamDeck
lastState string
pressed bool
EntityID string
Button ButtonInfo
Icon DynamicIconInfo
Service ServiceInfo
}
// DynamicIconInfo .
type DynamicIconInfo struct {
ForState func(bool, string) streamdeck.IconButton
}
// NewDynamicIconButton .
func NewDynamicIconButton(
ha *homeassistant.Connection,
sd *streamdeck.StreamDeck,
entityID string,
button ButtonInfo,
icon DynamicIconInfo,
service ServiceInfo,
) *DynamicIconButton {
b := new(DynamicIconButton)
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 *DynamicIconButton) InitializeButton() {
b.Lock()
defer b.Unlock()
iconButton := b.Icon.ForState(b.pressed, b.lastState)
err := b.sd.FillIcon(b.Button.X, b.Button.Y, iconButton)
if err != nil {
log.Printf("problem trying to fill icon: %+v", err)
}
}
// StateChange .
func (b *DynamicIconButton) StateChange(entityID string, newState string) {
if entityID == b.EntityID {
b.Lock()
defer b.Unlock()
b.lastState = newState
iconButton := b.Icon.ForState(b.pressed, b.lastState)
err := b.sd.FillIcon(b.Button.X, b.Button.Y, iconButton)
if err != nil {
log.Printf("problem trying to fill icon: %+v", err)
}
}
}
// HomeAssistantToggle .
type HomeAssistantToggle struct {
EntityID string `json:"entity_id"`
}
// ButtonPressed .
func (b *DynamicIconButton) 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
iconButton := b.Icon.ForState(b.pressed, b.lastState)
err := b.sd.FillIcon(b.Button.X, b.Button.Y, iconButton)
if err != nil {
log.Printf("problem trying to fill icon: %+v", err)
}
}
// ButtonReleased .
func (b *DynamicIconButton) 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
iconButton := b.Icon.ForState(b.pressed, b.lastState)
err := b.sd.FillIcon(b.Button.X, b.Button.Y, iconButton)
if err != nil {
log.Printf("problem trying to fill icon: %+v", err)
}
b.ha.CallService(b.Service.Domain, b.Service.Service, b.Service.Data, b)
}
// HandleResult .
func (b *DynamicIconButton) 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 *DynamicIconButton) HandleClose(ha *homeassistant.Connection, id uint64) {
log.Printf("homeassistant closed")
}