11 "github.com/stretchr/testify/require"
18 validRecord = `{"mac":"FC610E7F98AA","power":0,"stemp":20,"temp":19.5,"sensor":23,"ativo":0,"bloqueio":1}`
20 testMAC = "fc610e7f98aa"
21 testDeviceID = "01ARZ3NDEKTSV4RRFFQ69G5FAV"
22 testSecondID = "01ARZ3NDEKTSV4RRFFQ69G5FAW"
23 testCommandID = "01ARZ3NDEKTSV4RRFFQ69G5FAX"
26 type fakeGateway struct {
38 func (g *fakeGateway) Status(ctx context.Context) ([]State, error) {
47 return append([]State(nil), g.states...), nil
50 func (g *fakeGateway) request(ctx context.Context) error {
51 g.starts = append(g.starts, time.Now())
53 timer := time.NewTimer(g.delay)
66 func (g *fakeGateway) Action(ctx context.Context, mac string, action Action) (State, error) {
70 g.actions = append(g.actions, action)
77 if len(g.actions) == g.failActionAt {
78 return State{}, errors.New("action unavailable")
81 for index := range g.states {
82 state := &g.states[index]
87 if action.Power != nil && !g.ignorePower {
88 state.Power = *action.Power
91 if action.TargetTemperature != nil && !g.ignoreTarget {
92 state.TargetTemperature = *action.TargetTemperature
101 func (g *fakeGateway) startCount() int {
108 type fakeTransport struct {
110 publications []protocol.Publication
111 failKind protocol.Kind
115 func (p *fakeTransport) Publish(_ context.Context, publication protocol.Publication) error {
119 p.publications = append(p.publications, publication)
120 message, err := protocol.Parse(publication.Topic, publication.Data, publication.Retain)
122 return fmt.Errorf("parse fake publication: %w", err)
125 if message.Address.Kind == p.failKind {
132 func (p *fakeTransport) messages(t *testing.T, kind protocol.Kind) []protocol.Message[any] {
137 var messages []protocol.Message[any]
139 for _, publication := range p.publications {
140 message, err := protocol.Parse(publication.Topic, publication.Data, publication.Retain)
141 require.NoError(t, err)
143 if message.Address.Kind == kind {
144 messages = append(messages, message)
151 type cancelingGateway struct {
152 cancel context.CancelFunc
156 func (g *cancelingGateway) Status(_ context.Context) ([]State, error) {
159 return nil, errors.Join(context.Canceled, g.failure)
162 func (g *cancelingGateway) Action(_ context.Context, _ string, _ Action) (State, error) {
165 return State{}, errors.Join(context.Canceled, g.failure)
168 func testDriver(t *testing.T, client Client, transport *fakeTransport, thermostats ...Thermostat) (*Driver, *driver.Router) {
171 if len(thermostats) == 0 {
172 thermostats = []Thermostat{{
173 DeviceID: testDeviceID,
180 for _, thermostat := range thermostats {
181 ids = append(ids, thermostat.DeviceID)
184 router, err := driver.NewRouter(ids)
185 require.NoError(t, err)
187 integration, err := New(Settings{Thermostats: thermostats}, driver.NewPublisher(transport), router, client)
188 require.NoError(t, err)
190 return integration, router
193 func testCommand() driver.Command {
194 expires := time.Now().Add(time.Minute).UTC()
196 return driver.Command{
197 DeviceID: testDeviceID,
198 Capability: "control",
199 Metadata: protocol.Metadata{
200 MessageID: testCommandID,
203 Payload: protocol.ValuePayload{Value: []byte(`{"mode":"heat","target_temperature":22}`)},
207 func testState() State {
210 TargetTemperature: 20,
211 CurrentTemperature: 19.5,
212 FloorTemperature: 23,