-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_test.go
85 lines (70 loc) · 1.99 KB
/
connection_test.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
81
82
83
84
85
package hal_test
import (
"sync/atomic"
"testing"
"github.com/dansimau/hal"
"github.com/dansimau/hal/homeassistant"
"github.com/dansimau/hal/testutil"
"github.com/davecgh/go-spew/spew"
)
func TestConnection(t *testing.T) {
t.Parallel()
conn, server, cleanup := testutil.NewClientServer(t)
defer cleanup()
// Create test entity and register it
entity := hal.NewEntity("test.entity")
conn.RegisterEntities(entity)
// Send state change event
server.SendEvent(homeassistant.Event{
EventType: "state_changed",
EventData: homeassistant.EventData{
EntityID: "test.entity",
NewState: &homeassistant.State{State: "on"},
},
})
testutil.WaitFor(t, "verify entity state was updated", func() bool {
return entity.GetState().State == "on"
}, func() {
spew.Dump(entity.GetID(), entity.GetState())
})
}
func TestLoopProtection(t *testing.T) {
t.Parallel()
conn, server, cleanup := testutil.NewClientServer(t)
defer cleanup()
var automationTriggered atomic.Int32
testEntity := hal.NewEntity("test.entity")
conn.RegisterEntities(testEntity)
conn.RegisterAutomations(
hal.NewAutomation().
WithName("test.automation").
WithEntities(testEntity).
WithAction(func(_ hal.EntityInterface) {
automationTriggered.Add(1)
}),
)
// This one should be ignored because it is from the same user
server.SendEvent(homeassistant.Event{
EventType: "state_changed",
EventData: homeassistant.EventData{
EntityID: "test.entity",
NewState: &homeassistant.State{State: "off"},
},
Context: homeassistant.EventMessageContext{
UserID: testutil.TestUserID,
},
})
// This one should cause the automation to be triggered
server.SendEvent(homeassistant.Event{
EventType: "state_changed",
EventData: homeassistant.EventData{
EntityID: "test.entity",
NewState: &homeassistant.State{State: "on"},
},
})
testutil.WaitFor(t, "verify automation was triggered", func() bool {
return automationTriggered.Load() == 1
}, func() {
spew.Dump(automationTriggered.Load())
})
}