github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/trigger_time/trigger_time2_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package trigger_time
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  	"time"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  	"go.uber.org/atomic"
    28  
    29  	"github.com/e154/smart-home/adaptors"
    30  	"github.com/e154/smart-home/common"
    31  	"github.com/e154/smart-home/common/events"
    32  	m "github.com/e154/smart-home/models"
    33  	"github.com/e154/smart-home/plugins/triggers"
    34  	"github.com/e154/smart-home/system/automation"
    35  	"github.com/e154/smart-home/system/bus"
    36  	"github.com/e154/smart-home/system/mqtt"
    37  	"github.com/e154/smart-home/system/scheduler"
    38  	"github.com/e154/smart-home/system/scripts"
    39  	"github.com/e154/smart-home/system/supervisor"
    40  	. "github.com/e154/smart-home/tests/plugins"
    41  )
    42  
    43  func TestTriggerTime2(t *testing.T) {
    44  
    45  	const (
    46  		task3SourceScript = `
    47  entityAction = (entityId, actionName)->
    48      #print '---action---'
    49      Done()
    50  `
    51  	)
    52  
    53  	Convey("trigger time", t, func(ctx C) {
    54  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    55  			scriptService scripts.ScriptService,
    56  			supervisor supervisor.Supervisor,
    57  			mqttServer mqtt.MqttServ,
    58  			automation automation.Automation,
    59  			eventBus bus.Bus,
    60  			scheduler *scheduler.Scheduler,
    61  		) {
    62  
    63  			var counter atomic.Int32
    64  			scriptService.PushFunctions("Done", func() {
    65  				counter.Inc()
    66  			})
    67  			defer scriptService.PopStruct("Done")
    68  
    69  			// add scripts
    70  			// ------------------------------------------------
    71  
    72  			task3Script, err := AddScript("task3", task3SourceScript, adaptors, scriptService)
    73  			So(err, ShouldBeNil)
    74  
    75  			// add entity
    76  			// ------------------------------------------------
    77  
    78  			sensorEnt := GetNewSensor("device1")
    79  			sensorEnt.Actions = []*m.EntityAction{
    80  				{
    81  					Name:        "CHECK",
    82  					Description: "condition check",
    83  					Script:      task3Script,
    84  				},
    85  			}
    86  			err = adaptors.Entity.Add(context.Background(), sensorEnt)
    87  			ctx.So(err, ShouldBeNil)
    88  
    89  			sensorEnt, err = adaptors.Entity.GetById(context.Background(), sensorEnt.Id)
    90  			ctx.So(err, ShouldBeNil)
    91  
    92  			eventBus.Publish("system/models/entities/"+sensorEnt.Id.String(), events.EventCreatedEntityModel{
    93  				EntityId: sensorEnt.Id,
    94  			})
    95  
    96  			// automation
    97  			// ------------------------------------------------
    98  			trigger := &m.NewTrigger{
    99  				Enabled:    true,
   100  				Name:       "trigger1",
   101  				PluginName: "time",
   102  				Payload: m.Attributes{
   103  					triggers.CronOptionTrigger: {
   104  						Name:  triggers.CronOptionTrigger,
   105  						Type:  common.AttributeString,
   106  						Value: "* * * * * *", //every seconds
   107  					},
   108  				},
   109  			}
   110  			triggerId, err := AddTrigger(trigger, adaptors, eventBus)
   111  			So(err, ShouldBeNil)
   112  
   113  			action := &m.Action{
   114  				Name:             "action1",
   115  				EntityId:         common.NewEntityId(string(sensorEnt.Id)),
   116  				EntityActionName: common.String(sensorEnt.Actions[0].Name),
   117  			}
   118  			action.Id, err = adaptors.Action.Add(context.Background(), action)
   119  			So(err, ShouldBeNil)
   120  
   121  			//TASK3
   122  			newTask := &m.NewTask{
   123  				Name:       "Toggle plug OFF",
   124  				Enabled:    true,
   125  				Condition:  common.ConditionAnd,
   126  				TriggerIds: []int64{triggerId},
   127  				ActionIds:  []int64{action.Id},
   128  			}
   129  			_, err = AddTask(newTask, adaptors, eventBus)
   130  			So(err, ShouldBeNil)
   131  
   132  			time.Sleep(time.Second * 2)
   133  
   134  			So(counter.Load(), ShouldBeGreaterThanOrEqualTo, 1)
   135  
   136  		})
   137  	})
   138  }