github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/trigger_alexa/trigger_alexa_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_alexa 20 21 import ( 22 "context" 23 "testing" 24 "time" 25 26 "github.com/e154/smart-home/adaptors" 27 "github.com/e154/smart-home/common" 28 m "github.com/e154/smart-home/models" 29 "github.com/e154/smart-home/plugins/alexa" 30 "github.com/e154/smart-home/system/automation" 31 "github.com/e154/smart-home/system/bus" 32 "github.com/e154/smart-home/system/scripts" 33 "github.com/e154/smart-home/system/supervisor" 34 . "github.com/e154/smart-home/tests/plugins" 35 . "github.com/smartystreets/goconvey/convey" 36 ) 37 38 func TestTriggerAlexa(t *testing.T) { 39 40 const ( 41 task3SourceScript = ` 42 automationTriggerAlexa = (msg)-> 43 #print '---trigger---' 44 p = msg.payload 45 Done p 46 #msg.trigger_name 47 #msg.task_name 48 #msg.entity_id 49 return false 50 ` 51 ) 52 53 Convey("trigger alexa", t, func(ctx C) { 54 _ = container.Invoke(func(adaptors *adaptors.Adaptors, 55 scriptService scripts.ScriptService, 56 supervisor supervisor.Supervisor, 57 automation automation.Automation, 58 eventBus bus.Bus) { 59 60 // register plugins 61 AddPlugin(adaptors, "triggers") 62 AddPlugin(adaptors, "alexa") 63 64 serviceCh := WaitService(eventBus, time.Second*5, "Supervisor", "Automation") 65 pluginsCh := WaitPlugins(eventBus, time.Second*5, "triggers", "alexa") 66 automation.Start() 67 supervisor.Start(context.Background()) 68 defer automation.Shutdown() 69 defer supervisor.Shutdown(context.Background()) 70 So(<-serviceCh, ShouldBeTrue) 71 So(<-pluginsCh, ShouldBeTrue) 72 73 var ch = make(chan alexa.EventAlexaAction) 74 defer close(ch) 75 scriptService.PushFunctions("Done", func(msg alexa.EventAlexaAction) { 76 ch <- msg 77 }) 78 79 // add scripts 80 // ------------------------------------------------ 81 82 task3Script, err := AddScript("task3", task3SourceScript, adaptors, scriptService) 83 So(err, ShouldBeNil) 84 85 // automation 86 // ------------------------------------------------ 87 trigger := &m.NewTrigger{ 88 Enabled: true, 89 Name: "alexa", 90 ScriptId: common.Int64(task3Script.Id), 91 PluginName: "alexa", 92 Payload: m.Attributes{ 93 alexa.TriggerOptionSkillId: { 94 Name: alexa.TriggerOptionSkillId, 95 Type: common.AttributeInt, 96 Value: 1, 97 }, 98 }, 99 } 100 triggerId, err := AddTrigger(trigger, adaptors, eventBus) 101 So(err, ShouldBeNil) 102 103 //TASK3 104 newTask := &m.NewTask{ 105 Name: "Toggle plug ON", 106 Enabled: true, 107 TriggerIds: []int64{triggerId}, 108 Condition: common.ConditionAnd, 109 } 110 _, err = AddTask(newTask, adaptors, eventBus) 111 So(err, ShouldBeNil) 112 113 time.Sleep(time.Millisecond * 500) 114 115 // ------------------------------------------------ 116 117 eventBus.Publish(alexa.TopicPluginAlexa, alexa.EventAlexaAction{ 118 SkillId: 1, 119 IntentName: "FlatLights", 120 Payload: "kitchen_on", 121 }) 122 123 time.Sleep(time.Millisecond * 500) 124 125 // wait message 126 msg, ok := WaitT[alexa.EventAlexaAction](time.Second*2, ch) 127 ctx.So(ok, ShouldBeTrue) 128 129 ctx.So(msg.Payload, ShouldEqual, "kitchen_on") 130 ctx.So(msg.SkillId, ShouldEqual, 1) 131 ctx.So(msg.IntentName, ShouldEqual, "FlatLights") 132 133 time.Sleep(time.Second) 134 135 }) 136 }) 137 }