Esempio n. 1
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);
        }
Esempio n. 2
0
        public async Task EntityDelayUntilStateChangeLamdaShouldReturnTrue()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);


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

            AddChangedEvent("binary_sensor.pir", "off", "on");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            Assert.True(delayResult.Task.IsCompletedSuccessfully);
            Assert.True(delayResult.Task.Result);
        }
Esempio n. 3
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"));
        }
Esempio n. 4
0
        public async Task TurnOffMultipleEntityCallsCorrectServiceCall()
        {
            // ARRANGE

            // ACT
            await DefaultDaemonApp
            .Entity("light.correct_entity", "light.correct_entity2")
            .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"));
        }
Esempio n. 5
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));
        }
Esempio n. 6
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));
        }
Esempio n. 7
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"));
        }
Esempio n. 8
0
        public async Task EntityOnStateTriggerScript()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

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

            AddChangedEvent("binary_sensor.pir", "on", "off");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            VerifyCallService("script", "thescript", false);
            // Verify(n => n.CallService("script", "thescript", It.IsAny<object>(), false));
        }
Esempio n. 9
0
        public async Task EntityOnStateChangedLamdaTurnOnLightCallsCorrectServiceCall()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange((n, _) => n?.State == "on")
            .UseEntity("light.correct_entity")
            .TurnOn()
            .Execute();

            AddChangedEvent("binary_sensor.pir", "off", "on");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            VerifyCallServiceTimes("turn_on", Times.Once());
            VerifyCallServiceTuple("light", "turn_on", ("entity_id", "light.correct_entity"));
        }
Esempio n. 10
0
        public async Task TurnOnEntityWithMultipleAttributeCallsCorrectServiceCall()
        {
            // ARRANGE
            // ACT
            await DefaultDaemonApp
            .Entity("light.correct_entity")
            .TurnOn()
            .WithAttribute("brightness", 100)
            .WithAttribute("color_temp", 230)
            .ExecuteAsync();

            // ASSERT
            VerifyCallServiceTimes("turn_on", Times.Once());
            VerifyCallServiceTuple("light", "turn_on",
                                   ("brightness", 100),
                                   ("color_temp", 230),
                                   ("entity_id", "light.correct_entity"));
        }
Esempio n. 11
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"));
        }
Esempio n. 12
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));
        }
Esempio n. 13
0
        public async Task MediaPlayersFuncPlayCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultDaemonHost.InternalState["media_player.player"] = new EntityState
            {
                EntityId = "media_player.player",
                State    = "off"
            };

            // ACT
            await DefaultDaemonApp
            .MediaPlayers(n => n.EntityId == "media_player.player")
            .Play()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes("media_play", Times.Once());
            DefaultHassClientMock.VerifyCallService("media_player", "media_play", ("entity_id", "media_player.player"));
        }
Esempio n. 14
0
        public async Task TurnOffEntityLamdaSelectionCallsCorrectServiceCall()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

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

            // ASSERT

            VerifyCallServiceTimes("turn_off", Times.Exactly(2));
            VerifyCallServiceTuple("light", "turn_off", ("entity_id", "light.correct_entity"));
            VerifyCallServiceTuple("light", "turn_off", ("entity_id", "light.correct_entity2"));

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);
        }
Esempio n. 15
0
        public async Task EntityOnStateChangedEntitiesTurnOnLightCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange("on")
            .UseEntities(new string[] { "light.correct_entity" })
            .TurnOn()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "turn_on", ("entity_id", "light.correct_entity"));
        }
Esempio n. 16
0
        public async Task TurnOffEntityLambdaAttributeSelectionCallsCorrectServiceCall()
        {
            // ARRANGE
            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();
            await DefaultDaemonHost.Run("host", 8123, false, "token", cancelSource.Token);

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

            // ASSERT

            DefaultHassClientMock.VerifyCallServiceTimes("turn_off", Times.Exactly(3));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity"));
            DefaultHassClientMock.VerifyCallService("light", "turn_off", ("entity_id", "light.correct_entity2"));
            DefaultHassClientMock.VerifyCallService("switch", "turn_off", ("entity_id", "switch.correct_entity"));
        }
Esempio n. 17
0
        public async Task OneEntityWithSeveralShouldCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange((n, _) => n?.State == "on")
            .UseEntity("light.correct_entity")
            .Toggle()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("toggle", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "toggle", ("entity_id", "light.correct_entity"));
        }
Esempio n. 18
0
        public async Task CameraSnapshotCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "snapshot";

            // ACT
            await DefaultDaemonApp
            .Camera(entityId)
            .Snapshot("filename")
            .ExecuteAsync();

            // ASSERT
            VerifyCallServiceTimes(service_call, Times.Once());
            VerifyCallServiceTuple("camera", service_call,
                                   ("filename", "filename"),
                                   ("entity_id", entityId)
                                   );
        }
Esempio n. 19
0
        public async Task EntityOnStateChangedLamdaWithMultipleEntitiesCallsCorrectServiceCall()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var MotionEnabled = true;

            DefaultDaemonApp
            .Entities(new string[] { "binary_sensor.pir", "binary_sensor-pir2" })
            .WhenStateChange((to, from) => @from?.State == "off" && to?.State == "on" && MotionEnabled)
            .UseEntity("light.correct_entity")
            .Toggle()
            .Execute();

            AddChangedEvent("binary_sensor.pir", "off", "on");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            VerifyCallServiceTimes("toggle", Times.Once());
            VerifyCallServiceTuple("light", "toggle", ("entity_id", "light.correct_entity"));
        }
Esempio n. 20
0
        public async Task CameraPlayStreamCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "play_stream";

            // ACT
            await DefaultDaemonApp
            .Camera(entityId)
            .PlayStream("media_player.player", "anyformat")
            .ExecuteAsync();

            // ASSERT
            VerifyCallServiceTimes(service_call, Times.Once());
            VerifyCallServiceTuple("camera", service_call,
                                   ("media_player", "media_player.player"),
                                   ("format", "anyformat"),
                                   ("entity_id", entityId)
                                   );
        }
Esempio n. 21
0
        public async Task TurnOffEntityLambdaAttributeSelectionCallsCorrectServiceCall()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

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

            // ASSERT

            VerifyCallServiceTimes("turn_off", Times.Exactly(3));
            VerifyCallServiceTuple("light", "turn_off", ("entity_id", "light.correct_entity"));
            VerifyCallServiceTuple("light", "turn_off", ("entity_id", "light.correct_entity2"));
            VerifyCallServiceTuple("switch", "turn_off", ("entity_id", "switch.correct_entity"));

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);
        }
Esempio n. 22
0
        public async Task EntityOnStateChangedLamdaWithMultipleEntitiesCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource  = DefaultHassClientMock.GetSourceWithTimeout();
            var MotionEnabled = true;

            DefaultDaemonApp
            .Entities(new string[] { "binary_sensor.pir", "binary_sensor-pir2" })
            .WhenStateChange((to, from) => @from?.State == "off" && to?.State == "on" && MotionEnabled)
            .UseEntity("light.correct_entity")
            .Toggle()
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("toggle", Times.Once());
            DefaultHassClientMock.VerifyCallService("light", "toggle", ("entity_id", "light.correct_entity"));
        }
Esempio n. 23
0
        public async Task CameraRecordCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "record";

            // ACT
            await DefaultDaemonApp
            .Camera(entityId)
            .Record("filename", 1, 2)
            .ExecuteAsync();

            // ASSERT
            VerifyCallServiceTimes(service_call, Times.Once());
            VerifyCallServiceTuple("camera", service_call,
                                   ("filename", "filename"),
                                   ("duration", 1),
                                   ("lookback", 2),
                                   ("entity_id", entityId)
                                   );
        }
Esempio n. 24
0
        public async Task CamerasFuncDisableMotionDetectionCallsCorrectServiceCall()
        {
            // ARRANGE
            var entityId     = "camera.camera1";
            var service_call = "disable_motion_detection";

            DefaultDaemonHost.InternalState["camera.camera1"] = new EntityState
            {
                EntityId = entityId,
                State    = "on"
            };

            // ACT
            await DefaultDaemonApp
            .Cameras(n => n.EntityId == entityId)
            .DisableMotionDetection()
            .ExecuteAsync();

            // ASSERT
            DefaultHassClientMock.VerifyCallServiceTimes(service_call, Times.Once());
            DefaultHassClientMock.VerifyCallServiceTuple("camera", service_call, ("entity_id", entityId));
        }
Esempio n. 25
0
        public async Task OtherEventShouldNotCallCorrectFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            dynamic dataObject = GetDynamicDataObject();

            DefaultHassClientMock.AddCustomEvent("CUSTOM_EVENT", dataObject);

            var isCalled = false;

            // ACT
            DefaultDaemonApp.ListenEvent("OTHER_EVENT", (ev, data) =>
            {
                isCalled = true;
                return(Task.CompletedTask);
            });

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.False(isCalled);
        }
Esempio n. 26
0
        public async Task EntityOnStateIncludeAttributesTriggerOnSameState()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var triggered = false;

            // ACT
            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange(allChanges: true)
            .Call((e, n, o) =>
            {
                triggered = true;
                return(Task.CompletedTask);
            })
            .Execute();

            AddChangedEvent("binary_sensor.pir", "off", "off");
            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.True(triggered);
        }
Esempio n. 27
0
        public async Task EntityOnStateIncludeAttributesTriggerOnSameState()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "off");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();
            var triggered    = false;

            // ACT
            DefaultDaemonApp
            .Entity("binary_sensor.pir")
            .WhenStateChange(allChanges: true)
            .Call((e, n, o) =>
            {
                triggered = true;
                return(Task.CompletedTask);
            })
            .Execute();

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            Assert.True(triggered);
        }