github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/trigger_time/trigger_time_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 "testing" 23 "time" 24 25 . "github.com/smartystreets/goconvey/convey" 26 "go.uber.org/atomic" 27 28 "github.com/e154/smart-home/adaptors" 29 "github.com/e154/smart-home/common" 30 m "github.com/e154/smart-home/models" 31 "github.com/e154/smart-home/plugins/triggers" 32 "github.com/e154/smart-home/system/automation" 33 "github.com/e154/smart-home/system/bus" 34 "github.com/e154/smart-home/system/scheduler" 35 "github.com/e154/smart-home/system/scripts" 36 "github.com/e154/smart-home/system/supervisor" 37 . "github.com/e154/smart-home/tests/plugins" 38 ) 39 40 func TestTriggerTime(t *testing.T) { 41 42 const ( 43 task3SourceScript = ` 44 automationTriggerTime = (msg)-> 45 #print '---trigger---' 46 p = msg.payload 47 Done p 48 return false 49 ` 50 ) 51 52 Convey("trigger time", t, func(ctx C) { 53 _ = container.Invoke(func(adaptors *adaptors.Adaptors, 54 scriptService scripts.ScriptService, 55 supervisor supervisor.Supervisor, 56 automation automation.Automation, 57 eventBus bus.Bus, 58 scheduler *scheduler.Scheduler, 59 ) { 60 61 var counter atomic.Int32 62 scriptService.PushFunctions("Done", func(t time.Time) { 63 counter.Inc() 64 }) 65 defer scriptService.PopStruct("Done") 66 67 // add scripts 68 // ------------------------------------------------ 69 70 task3Script, err := AddScript("task4", task3SourceScript, adaptors, scriptService) 71 So(err, ShouldBeNil) 72 73 // automation 74 // ------------------------------------------------ 75 trigger := &m.NewTrigger{ 76 Enabled: true, 77 Name: "trigger1", 78 ScriptId: common.Int64(task3Script.Id), 79 PluginName: "time", 80 Payload: m.Attributes{ 81 triggers.CronOptionTrigger: { 82 Name: triggers.CronOptionTrigger, 83 Type: common.AttributeString, 84 Value: "* * * * * *", //every seconds 85 }, 86 }, 87 } 88 triggerId, err := AddTrigger(trigger, adaptors, eventBus) 89 So(err, ShouldBeNil) 90 91 //TASK3 92 newTask := &m.NewTask{ 93 Name: "Toggle plug OFF", 94 Enabled: true, 95 Condition: common.ConditionAnd, 96 TriggerIds: []int64{triggerId}, 97 } 98 _, err = AddTask(newTask, adaptors, eventBus) 99 So(err, ShouldBeNil) 100 101 time.Sleep(time.Second * 2) 102 103 So(counter.Load(), ShouldBeGreaterThanOrEqualTo, 1) 104 }) 105 }) 106 }