github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/sun/sun_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 sun
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/e154/smart-home/common/events"
    27  
    28  	"github.com/e154/smart-home/adaptors"
    29  	sunPlugin "github.com/e154/smart-home/plugins/sun"
    30  	"github.com/e154/smart-home/system/bus"
    31  	"github.com/e154/smart-home/system/scripts"
    32  	"github.com/e154/smart-home/system/supervisor"
    33  	. "github.com/e154/smart-home/tests/plugins"
    34  	. "github.com/smartystreets/goconvey/convey"
    35  )
    36  
    37  func TestSun(t *testing.T) {
    38  
    39  	Convey("sun", t, func(ctx C) {
    40  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    41  			scriptService scripts.ScriptService,
    42  			supervisor supervisor.Supervisor,
    43  			eventBus bus.Bus) {
    44  
    45  			AddPlugin(adaptors, "sun")
    46  
    47  			// common
    48  			// ------------------------------------------------
    49  			sunEnt := GetNewSun("main")
    50  			err := adaptors.Entity.Add(context.Background(), sunEnt)
    51  			ctx.So(err, ShouldBeNil)
    52  
    53  			ch := make(chan events.EventStateChanged, 2)
    54  			defer close(ch)
    55  			fn := func(topic string, msg interface{}) {
    56  				switch v := msg.(type) {
    57  				case events.EventStateChanged:
    58  					ch <- v
    59  				}
    60  			}
    61  			eventBus.Subscribe("system/entities/"+sunEnt.Id.String(), fn)
    62  			defer eventBus.Unsubscribe("system/entities/"+sunEnt.Id.String(), fn)
    63  
    64  			// add entity
    65  			// ------------------------------------------------
    66  			sun := sunPlugin.NewActor(sunEnt, supervisor.GetService())
    67  
    68  			t.Run("entity", func(t *testing.T) {
    69  				Convey("phase", t, func(ctx C) {
    70  					sunEnt, err = adaptors.Entity.GetById(context.Background(), sunEnt.Id)
    71  					ctx.So(err, ShouldBeNil)
    72  
    73  					ctx.So(sunEnt.Settings[sunPlugin.AttrLat].Float64(), ShouldEqual, 54.9022)
    74  					ctx.So(sunEnt.Settings[sunPlugin.AttrLon].Float64(), ShouldEqual, 83.0335)
    75  				})
    76  			})
    77  
    78  			t.Run("actor", func(t *testing.T) {
    79  				Convey("attrs", t, func(ctx C) {
    80  
    81  					loc, err := time.LoadLocation("Asia/Novosibirsk")
    82  					ctx.So(err, ShouldBeNil)
    83  					sun.UpdateSunPosition(time.Date(2021, 5, 27, 23, 0, 0, 0, loc))
    84  
    85  					// wait message
    86  					msg, ok := WaitT[events.EventStateChanged](time.Second*2, ch)
    87  					ctx.So(ok, ShouldBeTrue)
    88  
    89  					ctx.So(msg.NewState.State, ShouldNotBeNil)
    90  					ctx.So(msg.NewState.State.Name, ShouldEqual, sunPlugin.AttrDusk)
    91  					ctx.So(msg.NewState.State.Description, ShouldEqual, "dusk (evening nautical twilight starts)")
    92  					ctx.So(msg.NewState.Attributes[sunPlugin.AttrAzimuth].String(), ShouldContainSubstring, "326.127522661")
    93  					ctx.So(msg.NewState.Attributes[sunPlugin.AttrElevation].String(), ShouldContainSubstring, "-7.663125133")
    94  					ctx.So(msg.NewState.Attributes[sunPlugin.AttrHorizonState].String(), ShouldEqual, "belowHorizon")
    95  					ctx.So(msg.NewState.Attributes[sunPlugin.AttrPhase].String(), ShouldEqual, "dusk")
    96  				})
    97  			})
    98  		})
    99  	})
   100  }