Beispiel #1
0
        public async Task SpeakShouldCallCorrectService()
        {
            // ARRANGE
            DefaultHassClientMock.FakeStates["media_player.fakeplayer"] = new HassState
            {
                EntityId = "media_player.fakeplayer",
            };

            DefaultDaemonHost.InternalDelayTimeForTts = 0; // For testing

            // ACT
            var(daemonTask, _) = ReturnRunningDefauldDaemonHostTask();

            DefaultDaemonHost.Speak("media_player.fakeplayer", "Hello test!");

            var(_, expObject) = GetDynamicObject(
                ("entity_id", "media_player.fakeplayer"),
                ("message", "Hello test!")
                );

            await Task.Delay(50);

            // ASSERT
            DefaultHassClientMock.Verify(n => n.CallService("tts", "google_cloud_say", expObject, true));

            await WaitUntilCanceled(daemonTask);
        }
        public async Task StateChangeHasAreaInformation()
        {
            // ARRANGE
            DefaultDaemonHost._hassDevices["device_id"] = new HassDevice {
                AreaId = "area_id"
            };
            DefaultDaemonHost._hassAreas["area_id"] = new HassArea {
                Name = "Correct name", Id = "area_id"
            };
            DefaultDaemonHost._hassEntities["binary_sensor.pir"] = new HassEntity
            {
                EntityId = "binary_sensor.pir",
                DeviceId = "device_id"
            };
            await InitializeFakeDaemon().ConfigureAwait(false);

            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", fromState: "off", toState: "on");

            // ACT

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.Equal("Correct name", DefaultDaemonHost.GetState("binary_sensor.pir")?.Area);
        }
Beispiel #3
0
        public CoreDaemonHostTestBase() : base()
        {
            DefaultDaemonApp = new BaseTestApp
            {
                Id        = "app_id",
                IsEnabled = true
            };

            DefaultDaemonHost.AddRunningApp(DefaultDaemonApp);

            DefaultDaemonRxApp = new BaseTestRxApp
            {
                Id        = "app_rx_id",
                IsEnabled = true
            };
            DefaultDaemonHost.AddRunningApp(DefaultDaemonRxApp);

            DefaultMockedRxApp = new Mock <NetDaemonRxApp>()
            {
                CallBase = true
            };
            DefaultMockedRxApp.Object.Id        = "app_rx_mock_id";
            DefaultMockedRxApp.Object.IsEnabled = true;
            DefaultMockedRxApp.Setup(n => n.CreateObservableIntervall(It.IsAny <TimeSpan>(), It.IsAny <Action>())).Returns(new Mock <IDisposable>().Object);

            DefaultDaemonHost.AddRunningApp(DefaultMockedRxApp.Object);

            var notConnectedHassClientFactoryMock = new HassClientFactoryMock(HassClientMock.MockConnectFalse);

            _notConnectedDaemonHost = new NetDaemonHost(notConnectedHassClientFactoryMock.Object, DefaultDataRepositoryMock.Object, LoggerMock.LoggerFactory);

            SetupFakeData();
        }
Beispiel #4
0
        public async Task EventShouldCallCorrectFunction()
        {
            // ARRANGE

            dynamic helloWorldDataObject = GetDynamicDataObject(HelloWorldData);

            DefaultHassClientMock.AddCustomEvent("CUSTOM_EVENT", helloWorldDataObject);

            var isCalled = false;
            var message  = "";

            // ACT
            DefaultDaemonHost.ListenEvent("CUSTOM_EVENT", (ev, data) =>
            {
                isCalled = true;
                message  = data.Test;
                return(Task.CompletedTask);
            });

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            Assert.True(isCalled);
            Assert.Equal(HelloWorldData, message);
        }
Beispiel #5
0
        public async Task GetStateReturnsCorrectEntityState()
        {
            // ARRANGE

            // Fake what is coming from hass client
            var hassState = new HassState
            {
                EntityId    = "light.testlight",
                State       = "on",
                LastChanged = new DateTime(2020, 1, 2, 1, 2, 3),
                LastUpdated = new DateTime(2020, 1, 2, 3, 2, 1),
                Attributes  = new Dictionary <string, object>
                {
                    ["color_temp"]    = 390,
                    ["brightness"]    = 100,
                    ["friendly_name"] = "The test light"
                }
            };

            DefaultHassClientMock.FakeStates["light.testlight"] = hassState;

            await RunDefauldDaemonUntilCanceled();

            // ACT
            var entity = DefaultDaemonHost.GetState("light.testlight");

            // ASSERT
            Assert.NotNull(entity);
            DefaultHassClientMock.AssertEqual(hassState, entity !);
        }
Beispiel #6
0
        public async Task EntityOnStateCallFunction()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            string?actualToState   = "";
            string?actualFromState = "";
            var    actualEntity    = "";

            DefaultDaemonHost
            .Entity("binary_sensor.pir")
            .WhenStateChange("on").Call((entity, to, from) =>
            {
                actualToState   = to?.State;
                actualFromState = from?.State;
                actualEntity    = entity;
                return(Task.CompletedTask);
            }).Execute();

            await RunDefauldDaemonUntilCanceled();

            Assert.Equal("on", actualToState);
            Assert.Equal("off", actualFromState);
            Assert.Equal("binary_sensor.pir", actualEntity);
        }
Beispiel #7
0
        public async Task SpeakCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultDaemonHost.InternalDelayTimeForTts = 0; // For testing

            // ACT
            await DefaultDaemonHost
            .MediaPlayer("media_player.correct_player")
            .Speak("a message")
            .ExecuteAsync();

            var(daemonTask, _) = ReturnRunningDefauldDaemonHostTask();

            await Task.Delay(20);

            var     expObject           = new ExpandoObject();
            dynamic expectedAttruibutes = expObject;

            expectedAttruibutes.entity_id = "media_player.correct_player";
            expectedAttruibutes.message   = "a message";

            // ASSERT
            DefaultHassClientMock.Verify(n => n.CallService("tts", "google_cloud_say", expObject, true));

            await WaitUntilCanceled(daemonTask);
        }
Beispiel #8
0
        public async Task EntityOnStateChangedForTimeTurnOffLightCallsCorrectServiceCall()
        {
            // ARRANGE

            var lastChanged = new DateTime(2020, 1, 1, 1, 1, 1, 10);
            var lastUpdated = new DateTime(2020, 1, 1, 1, 1, 1, 50);

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout(100);

            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "on", "off",
                                                  lastUpdated, lastChanged);

            DefaultDaemonHost
            .Entity("binary_sensor.pir")
            .WhenStateChange("off")
            .AndNotChangeFor(TimeSpan.FromMilliseconds(20))
            .UseEntity("light.correct_entity")
            .TurnOff()
            .Execute();

            var(daemonTask, _) = ReturnRunningDefauldDaemonHostTask();
            // ASSERT
            await Task.Delay(10); // After 10ms we should not have call

            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Never());
            await Task.Delay(30); // After 10ms we should not have call

            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Once());

            await WaitUntilCanceled(daemonTask);
        }
Beispiel #9
0
        public async Task EntityOnStateChangedTurnOnLightCallsCorrectServiceCallButNoTurnOff()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            DefaultDaemonHost
            .Entity("binary_sensor.pir")
            .WhenStateChange("on")
            .UseEntity("light.correct_entity")
            .TurnOn()
            .Execute();

            DefaultDaemonHost
            .Entity("binary_sensor.pir")
            .WhenStateChange("off")
            .UseEntity("light.correct_entity")
            .TurnOff()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "turn_on", ("entity_id", "light.correct_entity"));

            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Never());
        }
Beispiel #10
0
        public async Task EntityOnStateChangedEntitiesLambdaTurnOnLightCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout(); //new CancellationTokenSource(1000);

            // Fake the
            DefaultDaemonHost.InternalState.Add("light.correct_entity", new EntityState
            {
                EntityId = "light.correct_entity"
            });

            DefaultDaemonHost
            .Entity("binary_sensor.pir")
            .WhenStateChange("on")
            .UseEntities(n => n.EntityId == "light.correct_entity")
            .TurnOn()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "turn_on", ("entity_id", "light.correct_entity"));
        }
        public async Task CallServiceEventShouldCallCorrectFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var dynObject = DaemonHostTestBase.GetDynamicDataObject(HelloWorldData);

            var    isCalled = false;
            string?message  = "";

            // ACT
            DefaultDaemonHost.ListenServiceCall("custom_domain", "any_service", data =>
            {
                isCalled = true;
                message  = data?.Test;
                return(Task.CompletedTask);
            });

            DefaultHassClientMock.AddCallServiceEvent("custom_domain", "any_service", dynObject);

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.True(isCalled);
            Assert.Equal(HelloWorldData, message);
        }
        public async Task SpeakShouldCallCorrectService()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            // ARRANGE
            SetEntityState(new()
            {
                EntityId = "media_player.fakeplayer"
            });

            DefaultDaemonHost.TextToSpeechService.InternalDelayTimeForTts = 100; // For testing

            // ACT
            DefaultDaemonHost.Speak("media_player.fakeplayer", "Hello test!");

            var(_, expObject) = GetDynamicObject(
                ("entity_id", "media_player.fakeplayer"),
                ("message", "Hello test!")
                );

            await DefaultDaemonHost.WaitForTasksAsync().ConfigureAwait(false);

            // ASSERT
            VerifyCallService("tts", "google_cloud_say", expObject, true);
        }
        public async Task StopCallsCloseClient()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            await DefaultDaemonHost.Stop().ConfigureAwait(false);

            DefaultHassClientMock.Verify(n => n.CloseAsync(), Times.Once);
        }
Beispiel #14
0
        public async Task SendEventWithNullDataShouldCallCorrectMethod()
        {
            // ACT
            await DefaultDaemonHost.SendEvent("test_event");

            // ASSERT
            DefaultHassClientMock.Verify(n => n.SendEvent("test_event", null));
        }
Beispiel #15
0
        public void SetStateWithAreaShouldReturnCorrectArea()
        {
            // ACT
            SetEntityState("sensor.any_sensor", "SomeState", "LivingRoom");

            // ASSERT
            Assert.Equal("LivingRoom", DefaultDaemonHost.GetState("sensor.any_sensor")?.Area);
        }
        public async Task GetServicesShouldCallCorrectFunctions()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            var services = await DefaultDaemonHost.GetAllServices().ConfigureAwait(false);

            DefaultHassClientMock.Verify(n => n.GetServices(), Times.Once);
        }
Beispiel #17
0
        public async Task SetStateShouldReturnCorrectData()
        {
            await DefaultDaemonHost.SetState("sensor.any_sensor", "on", ("attr", "value"));

            var(dynObj, expObj) = GetDynamicObject(
                ("attr", "value")
                );
            DefaultHassClientMock.Verify(n => n.SetState("sensor.any_sensor", "on", expObj));
        }
        public async Task SetDaemonStateAsyncShouldCallCorrectFunctions()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            await DefaultDaemonHost.SetDaemonStateAsync(5, 2).ConfigureAwait(false);

            DefaultHassClientMock.Verify(n => n.SetState("sensor.netdaemon_status", "Connected", It.IsAny <object>()),
                                         Times.Once);
        }
Beispiel #19
0
 public FaultyRxAppTests() : base()
 {
     App = new DaemonRxAppTestApp()
     {
         Id = "id"
     };
     DefaultDaemonHost.AddRunningApp(App);
     App.StartUpAsync(DefaultDaemonHost).Wait();
 }
Beispiel #20
0
        public void GetAppOnMissingAppShouldReturnNull()
        {
            // ARRANGE
            // ACT
            var theApp = DefaultDaemonHost.GetApp("noexist");

            // ASSERT
            Assert.Null(theApp);
        }
Beispiel #21
0
        public void RegisterAppShouldReturnCorrectAppWhenUsingGetApp()
        {
            // ARRANGE
            DefaultDaemonHost.RegisterAppInstance("appx", new HostTestApp());
            // ACT
            var theApp = DefaultDaemonHost.GetApp("appx");

            // ASSERT
            Assert.NotNull(theApp);
        }
Beispiel #22
0
        public void RunEveryMinuteShouldCallCreateObservableIntervall()
        {
            // ARRANGE
            // ACT
            DefaultMockedRxApp.Object.RunEveryMinute(1, () => Console.WriteLine("Test"));
            DefaultDaemonHost.WaitForTasksAsync();

            // ASSERT
            DefaultMockedRxApp.Verify(n => n.CreateObservableTimer(It.IsAny <DateTime>(), It.IsAny <TimeSpan>(), It.IsAny <Action>()), Times.Once());
        }
Beispiel #23
0
        public void GetStateMissingEntityReturnsNull()
        {
            // ARRANGE

            // ACT
            var entity = DefaultDaemonHost.GetState("light.missing_entity");

            // ASSERT
            Assert.Null(entity);
        }
Beispiel #24
0
        public void RegisterAppShouldReturnCorrectAppWhenUsingGetApp()
        {
            // ARRANGE
            DefaultDaemonHost.RegisterAppInstance("appx", new JoySoftware.HomeAssistant.NetDaemon.Common.NetDaemonApp());
            // ACT
            var theApp = DefaultDaemonHost.GetApp("appx");

            // ASSERT
            Assert.NotNull(theApp);
        }
        public async Task SetStateShouldReturnCorrectData()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            await DefaultDaemonHost.SetStateAsync("sensor.any_sensor", "on", ("attr", "value")).ConfigureAwait(false);

            var(dynObj, expObj) = GetDynamicObject(
                ("attr", "value")
                );
            DefaultHassClientMock.Verify(n => n.SetState("sensor.any_sensor", "on", expObj));
        }
Beispiel #26
0
        public async Task SetStateShouldKeepSameArea()
        {
            // ARRANGE
            await RunDefauldDaemonUntilCanceled();

            // ACT
            var state = await DefaultDaemonHost.SetState("light.ligth_in_area", "on", ("attr", "value"));

            /// ASSERT
            Assert.Equal("Area", state?.Area);
        }
Beispiel #27
0
        public async Task SendEventWithNullDataShouldCallCorrectMethod()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            // ACT
            await DefaultDaemonHost.SendEvent("test_event");

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            VerifyEventSent("test_event");
        }
        public async Task SetStateWaitForResultShouldCallCorrectFunction()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

            var(dynObj, expObj) = GetDynamicObject(
                ("attr", "value")
                );
            var entity = await DefaultDaemonHost
                         .SetStateAndWaitForResponseAsync("sensor.any_sensor", "on", dynObj, true)
                         .ConfigureAwait(false);

            DefaultHassClientMock.Verify(n => n.SetState("sensor.any_sensor", "on", expObj));
        }
        public async Task ConnectToHAIntegrationShouldCallCorrectFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            await DefaultDaemonHost.ConnectToHAIntegration().ConfigureAwait(false);

            // ACT
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            DefaultHassClientMock.Verify(n => n.GetApiCall <object>("netdaemon/info"));
        }
Beispiel #30
0
        public async Task SendEventShouldCallCorrectMethod()
        {
            // ARRANGE
            var eventData = GetDynamicDataObject();

            // ACT
            await DefaultDaemonHost.SendEvent("test_event", eventData);

            // ASSERT
            var expandoObject = (ExpandoObject)eventData;

            DefaultHassClientMock.Verify(n => n.SendEvent("test_event", expandoObject));
        }