mystreamdeck/internal/staticiconbutton.go

100 lines
2.4 KiB
Go
Raw Normal View History

2020-10-04 15:28:04 -05:00
package internal
import (
"encoding/json"
"image/color"
"log"
"time"
"github.com/jcollie/go-homeassistant"
"github.com/jcollie/go-streamdeck"
)
// StaticIconButton .
type StaticIconButton struct {
ha *homeassistant.Connection
sd *streamdeck.StreamDeck
Button ButtonInfo
Icon StaticIconInfo
Service ServiceInfo
}
// StaticIconInfo .
type StaticIconInfo struct {
Icon string
FgColor color.Color
BgColor color.Color
PressedFgColor color.Color
PressedBgColor color.Color
}
// NewStaticIconButton .
func NewStaticIconButton(
ha *homeassistant.Connection,
sd *streamdeck.StreamDeck,
button ButtonInfo,
icon StaticIconInfo,
service ServiceInfo,
) *StaticIconButton {
b := new(StaticIconButton)
b.ha = ha
b.sd = sd
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 *StaticIconButton) InitializeButton() {
b.sd.FillIcon(b.Button.X, b.Button.Y,
streamdeck.IconButton{
Icon: b.Icon.Icon,
IconColor: b.Icon.FgColor,
BackgroundColor: b.Icon.BgColor,
})
}
// StateChange .
func (b *StaticIconButton) StateChange(entityID string, newState string) {
// do nothing
}
// ButtonPressed .
func (b *StaticIconButton) ButtonPressed(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) {
log.Printf("pressed %d %d %s\n", x, y, timestamp)
b.sd.FillIcon(b.Button.X, b.Button.Y,
streamdeck.IconButton{
Icon: b.Icon.Icon,
IconColor: b.Icon.PressedFgColor,
BackgroundColor: b.Icon.PressedBgColor,
},
)
}
// ButtonReleased .
func (b *StaticIconButton) ButtonReleased(sd *streamdeck.StreamDeck, x int, y int, timestamp time.Time) {
log.Printf("released %d %d %s\n", x, y, timestamp)
b.sd.FillIcon(b.Button.X, b.Button.Y,
streamdeck.IconButton{
Icon: b.Icon.Icon,
IconColor: b.Icon.FgColor,
BackgroundColor: b.Icon.BgColor,
},
)
b.ha.CallService(b.Service.Domain, b.Service.Service, b.Service.Data, b)
}
// HandleResult .
func (b *StaticIconButton) 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 *StaticIconButton) HandleClose(ha *homeassistant.Connection, id uint64) {
log.Printf("home assistant closed")
}