-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity_input_boolean.go
80 lines (62 loc) · 1.68 KB
/
entity_input_boolean.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package hal
import (
"log/slog"
"github.com/dansimau/hal/hassws"
)
// InputBoolean is a virtual switch that can be turned on or off.
type InputBoolean struct {
*Entity
}
func NewInputBoolean(id string) *InputBoolean {
return &InputBoolean{Entity: NewEntity(id)}
}
func (s *InputBoolean) IsOff() bool {
return s.GetState().State == "off"
}
func (s *InputBoolean) IsOn() bool {
return s.GetState().State == "on"
}
func (s *InputBoolean) TurnOn(attributes ...map[string]any) error {
if s.connection == nil {
slog.Error("InputBoolean not registered", "entity", s.GetID())
return ErrEntityNotRegistered
}
slog.Debug("Turning on virtual switch", "entity", s.GetID())
data := map[string]any{
"entity_id": []string{s.GetID()},
}
for _, attribute := range attributes {
for k, v := range attribute {
data[k] = v
}
}
_, err := s.connection.CallService(hassws.CallServiceRequest{
Type: hassws.MessageTypeCallService,
Domain: "input_boolean",
Service: "turn_on",
Data: data,
})
if err != nil {
slog.Error("Error turning on virtual switch", "entity", s.GetID(), "error", err)
}
return err
}
func (s *InputBoolean) TurnOff() error {
if s.connection == nil {
slog.Error("InputBoolean not registered", "entity", s.GetID())
return ErrEntityNotRegistered
}
slog.Info("Turning off virtual switch", "entity", s.GetID())
_, err := s.connection.CallService(hassws.CallServiceRequest{
Type: hassws.MessageTypeCallService,
Domain: "input_boolean",
Service: "turn_off",
Data: map[string]any{
"entity_id": []string{s.GetID()},
},
})
if err != nil {
slog.Error("Error turning off virtual switch", "entity", s.GetID(), "error", err)
}
return err
}