-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_button.go
49 lines (37 loc) · 897 Bytes
/
entity_button.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package hal
import (
"log/slog"
"time"
)
// buttonPressTimeout is the amount of time to listen for repeat presses.
const buttonPressTimeout = 2 * time.Second
// Button is an event entity that represents a button.
type Button struct {
*Entity
lastPressed time.Time
pressedTimes int32
}
func NewButton(id string) *Button {
return &Button{Entity: NewEntity(id)}
}
func (b *Button) Name() string {
return b.GetID()
}
func (b *Button) Entities() Entities {
return Entities{b}
}
func (b *Button) Action(_ EntityInterface) {
if b.Entity.GetState().Attributes["event_type"] != "initial_press" {
return
}
if time.Since(b.lastPressed) < buttonPressTimeout {
b.pressedTimes++
} else {
b.pressedTimes = 1
}
slog.Info("Button pressed", "entity", b.GetID(), "times", b.pressedTimes)
b.lastPressed = time.Now()
}
func (b *Button) PressedTimes() int32 {
return b.pressedTimes
}