Exemplo n.º 1
0
        public async Task SetStateWithAttributesEntityCallsCorrectServiceCall()
        {
            // ARRANGE
            // ACT
            await DefaultDaemonHost
            .Entity("light.correct_entity")
            .SetState(50)
            .WithAttribute("attr1", "str_value")
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifySetStateTimes("light.correct_entity", Times.Once());
            DefaultHassClientMock.VerifySetState("light.correct_entity", "50", ("attr1", "str_value"));
        }
Exemplo n.º 2
0
        public async Task TurnOffEntityCallsCorrectServiceCall()
        {
            // ARRANGE

            // ACT
            await DefaultDaemonHost
            .Entity("light.correct_entity")
            .TurnOff()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity"));
        }
Exemplo n.º 3
0
        public async Task ToggleAsyncWithLightCallsSendMessageWithCorrectEntityId()
        {
            // ARRANGE
            var excpectedAttributes = new ExpandoObject();

            ((IDictionary <string, object>)excpectedAttributes)["entity_id"] = "light.correct_entity";

            // ACT
            await DefaultDaemonHost.ToggleAsync("light.correct_entity");

            // ASSERT

            DefaultHassClientMock.Verify(n => n.CallService("light", "toggle", excpectedAttributes, It.IsAny <bool>()));
        }
Exemplo n.º 4
0
        public async Task TurnOnAsyncWithLightCallsSendMessageWithCorrectEntityId()
        {
            // ARRANGE
            var(_, expectedAttributes) = GetDynamicObject(
                ("entity_id", "light.correct_entity")
                );

            // ACT
            await DefaultDaemonHost.TurnOnAsync("light.correct_entity");

            // ASSERT

            DefaultHassClientMock.Verify(n => n.CallService("light", "turn_on", expectedAttributes, It.IsAny <bool>()));
        }
Exemplo n.º 5
0
        public async Task RunScriptShouldCallCorrectFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            // ACT
            DefaultDaemonRxApp.RunScript("myscript");

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT

            DefaultHassClientMock.VerifyCallServiceTimes("myscript", Times.Once());
        }
Exemplo n.º 6
0
        public async Task DelayStateLambdaChangeShouldNotComplete()
        {
            // ARRANGE

            // ACT
            using var delayResult = DefaultDaemonHost.DelayUntilStateChange(new string[] { "binary_sensor.pir" }, (n, o) => n?.State == "on");

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

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            Assert.False(delayResult.Task.IsCompleted);
            Assert.Single(DefaultDaemonHost.InternalStateActions);
        }
Exemplo n.º 7
0
        public async Task RunScriptWithDomainShouldCallCorrectFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var(dynObj, expObj) = GetDynamicObject(
                ("attr", "value"));

            // ACT
            DefaultDaemonRxApp.RunScript("script.myscript");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("myscript", Times.Once());
        }
Exemplo n.º 8
0
        public async Task DelayStateChangeWithToAndFromShouldReturnTrue()
        {
            // ARRANGE

            // ACT
            using var delayResult = DefaultDaemonHost.DelayUntilStateChange(new string[] { "binary_sensor.pir" }, to: "on", from: "off");

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

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            Assert.True(delayResult.Task.Result);
            Assert.Empty(DefaultDaemonHost.InternalStateActions);
        }
Exemplo n.º 9
0
        public async Task InputSelectSetOptionIEnumerableShouldCallCorrectCallService()
        {
            // ARRANGE
            // ACT
            await DefaultDaemonApp
            .InputSelects(new string[] { "input_select.myselect" })
            .SetOption("option1")
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("select_option", Times.Once());
            DefaultHassClientMock.VerifyCallService("input_select", "select_option",
                                                    ("entity_id", "input_select.myselect"),
                                                    ("option", "option1"));
        }
Exemplo n.º 10
0
        public async Task SetStateShouldReturnCorrectData()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var(dynObj, expObj) = GetDynamicObject(
                ("attr", "value"));

            // ACT
            DefaultDaemonRxApp.SetState("sensor.any_sensor", "on", dynObj);
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            DefaultHassClientMock.Verify(n => n.SetState("sensor.any_sensor", "on", expObj));
        }
Exemplo n.º 11
0
        public async Task MissingEntityShouldNotLogError()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(_ => App.Entity("light.do_not_exist").TurnOn());

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            LoggerMock.AssertLogged(LogLevel.Error, Times.Never());
        }
Exemplo n.º 12
0
        public async Task EntityDelayUntilStateChangeLamdaShouldReturnTrue()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            var delayResult = DefaultDaemonApp
                              .Entity("binary_sensor.pir")
                              .DelayUntilStateChange((to, _) => to?.State == "on");

            await RunDefauldDaemonUntilCanceled();

            Assert.True(delayResult.Task.IsCompletedSuccessfully);
            Assert.True(delayResult.Task.Result);
        }
Exemplo n.º 13
0
        public async Task TimerToggleShouldCallCorrectServiceCall()
        {
            // ARRANGE
            DefaultDaemonHost
            .Timer()
            .Every(TimeSpan.FromMilliseconds(20))
            .Entity("light.correct_light")
            .Toggle()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("toggle", Times.AtLeast(2)); // Less cause of slow cloud CI builds
            DefaultHassClientMock.VerifyCallService("light", "toggle", ("entity_id", "light.correct_light"));
        }
Exemplo n.º 14
0
        public async Task TimerTurnOffShouldCallCorrectServiceCall()
        {
            // ARRANGE
            DefaultDaemonHost
            .Timer()
            .Every(TimeSpan.FromMilliseconds(20))
            .Entity("light.correct_light")
            .TurnOff()
            .Execute();

            await RunDefauldDaemonUntilCanceled(200);

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.AtLeast(2));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_light"));
        }
Exemplo n.º 15
0
        public async Task TurnOnEntityWithAttributeCallsCorrectServiceCall()
        {
            // ARRANGE
            // ACT
            await DefaultDaemonApp
            .Entity("light.correct_entity")
            .TurnOn()
            .WithAttribute("brightness", 100)
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "turn_on",
                                                    ("brightness", 100),
                                                    ("entity_id", "light.correct_entity"));
        }
Exemplo n.º 16
0
        public async Task CameraEnableMotionDetectionCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "enable_motion_detection";

            // ACT
            await DefaultDaemonApp
            .Camera(entityId)
            .EnableMotionDetection()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes(service_call, Times.Once());
            DefaultHassClientMock.VerifyCallServiceTuple("camera", service_call, ("entity_id", entityId));
        }
Exemplo n.º 17
0
        public async Task CameraTurnOffCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "turn_off";

            // ACT
            await DefaultDaemonApp
            .Camera(entityId)
            .TurnOff()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes(service_call, Times.Once());
            DefaultHassClientMock.VerifyCallServiceTuple("camera", service_call, ("entity_id", entityId));
        }
Exemplo n.º 18
0
        public async Task CamerasDisableMotionDetectionCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "disable_motion_detection";

            // ACT
            await DefaultDaemonHost
            .Cameras(new string[] { entityId })
            .DisableMotionDetection()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes(service_call, Times.Once());
            DefaultHassClientMock.VerifyCallService("camera", service_call, ("entity_id", entityId));
        }
Exemplo n.º 19
0
        public async Task SpeakShouldWaitUntilMediaPlays()
        {
            // ARRANGE

            // Get a running default Daemon
            var(daemonTask, _) = ReturnRunningDefauldDaemonHostTask(500);

            DefaultDaemonHost.InternalDelayTimeForTts = 0; // Allow now extra waittime

            // Expected data call service
            var(expectedAttruibutes, expectedAttributesExpObject) = GetDynamicObject(
                ("entity_id", "media_player.fakeplayer"),
                ("message", "Hello test!")
                );

            // Add the player that we want to fake having with the fake playing 0.1 second media duration
            dynamic currentStateAttributes = new ExpandoObject();

            currentStateAttributes.media_duration = 0.1;

            DefaultDaemonHost.InternalState["media_player.fakeplayer"] = new EntityState
            {
                EntityId  = "media_player.fakeplayer",
                Attribute = currentStateAttributes
            };

            await Task.Delay(100);

            // ACT
            DefaultDaemonHost.Speak("media_player.fakeplayer", "Hello test!");
            await Task.Delay(80);

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

            // ASSERT

            // Called once after 150 ms
            DefaultHassClientMock.Verify(n => n.CallService("tts", "google_cloud_say", expectedAttributesExpObject, true), Times.Once);

            await Task.Delay(200);

            // Called twice after 250ms
            DefaultHassClientMock.Verify(n => n.CallService("tts", "google_cloud_say", expectedAttributesExpObject, true), Times.Exactly(2));

            await WaitUntilCanceled(daemonTask);
        }
Exemplo n.º 20
0
        public async Task WhenStateStaysSameForTimeItShouldCallFunction()
        {
            await InitializeFakeDaemon(100).ConfigureAwait(false);

            bool isRun = false;

            using var ctx = DefaultDaemonRxApp.StateChanges
                            .Where(t => t.New.EntityId == "binary_sensor.pir")
                            .NDSameStateFor(TimeSpan.FromMilliseconds(50))
                            .Subscribe(_ => isRun = true);

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            Assert.True(isRun);
        }
Exemplo n.º 21
0
        public async Task TurnOffEntityLamdaSelectionCallsCorrectServiceCall()
        {
            // ARRANGE
            await RunDefauldDaemonUntilCanceled();

            // ACT
            await DefaultDaemonApp
            .Entities(n => n.EntityId.StartsWith("light.correct_entity"))
            .TurnOff()
            .ExecuteAsync();

            // ASSERT

            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Exactly(2));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity"));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity2"));
        }
Exemplo n.º 22
0
        public async Task TurnOffLightLambdaAttributeSelectionCallsCorrectServiceCall()
        {
            // ARRANGE
            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();
            await DefaultDaemonHost.Run("host", 8123, false, "token", cancelSource.Token);

            // ACT
            await DefaultDaemonHost
            .Lights(n => n?.Attribute?.test >= 100)
            .TurnOff()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Exactly(2));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity"));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity2"));
        }
Exemplo n.º 23
0
        public async Task TimerEveryWithAttributeShouldCallServiceCorrectNumberOfTimes()
        {
            // ARRANGE
            DefaultDaemonHost
            .Timer()
            .Every(TimeSpan.FromMilliseconds(20))
            .Entity("light.correct_light")
            .TurnOn()
            .UsingAttribute("attr", "on")
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.AtLeast(4));
            DefaultHassClientMock.VerifyCallService("light", "turn_on", ("attr", "on"), ("entity_id", "light.correct_light"));
        }
Exemplo n.º 24
0
        public async Task NewEventShouldCallFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var called = false;

            // ACT
            DefaultDaemonRxApp.EventChanges
            .Subscribe(_ => called = true);

            DefaultHassClientMock.AddCustomEvent("AN_EVENT", new { somedata = "hello" });

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.True(called);
        }
Exemplo n.º 25
0
        public async Task SetStateDynamicWithNoWaitShouldCallCorrectFunction()
        {
            await InitializeFakeDaemon().ConfigureAwait(false);

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

            DefaultHassClientMock.Verify(n => n.CallService("netdaemon", "entity_create",
                                                            It.IsAny <object>(), null, false), Times.Once);

            DefaultHassClientMock.Verify(n => n.GetState("sensor.any_sensor"), Times.Never);
            Assert.Null(entity);
        }
Exemplo n.º 26
0
        public async Task NewAllEventDataShouldCallFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var called = false;

            // ACT
            DefaultDaemonRxApp.StateAllChanges
            .Subscribe(_ => called = true);

            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "on", "on");

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.True(called);
        }
Exemplo n.º 27
0
        public async Task MediaPlayersFuncExceptionLogsError()
        {
            // ARRANGE
            DefaultDaemonHost.InternalState["id"] = new EntityState {
                EntityId = "id"
            };

            // ACT
            var x = await Assert.ThrowsAsync <Exception>(() => DefaultDaemonApp
                                                         .MediaPlayers(n => throw new Exception("Some error"))
                                                         .Play()
                                                         .ExecuteAsync());

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Never());
            LoggerMock.AssertLogged(LogLevel.Error, Times.AtLeastOnce());
            Assert.Equal("Some error", x.Message);
        }
Exemplo n.º 28
0
        public async Task EntityOnStateTriggerScript()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "on", "off");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            // ACT
            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange(to: "off")
            .RunScript("thescript")
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.Verify(n => n.CallService("script", "thescript", It.IsAny <object>(), false));
        }
Exemplo n.º 29
0
        public async Task InputSelectSetOptionFuncShouldCallCorrectCallService()
        {
            // ARRANGE
            DefaultDaemonHost.InternalState["input_select.myselect"] = new EntityState {
                EntityId = "input_select.myselect"
            };
            // ACT
            await DefaultDaemonApp
            .InputSelects(n => n.EntityId == "input_select.myselect")
            .SetOption("option1")
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("select_option", Times.Once());
            DefaultHassClientMock.VerifyCallService("input_select", "select_option",
                                                    ("entity_id", "input_select.myselect"),
                                                    ("option", "option1"));
        }
Exemplo n.º 30
0
        public async Task UsingEntityNewEventShouldNotCallFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var called = false;

            // ACT
            DefaultDaemonRxApp.Entity("binary_sensor.other_pir")
            .StateChanges
            .Subscribe(_ => called = true);

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.False(called);
        }