Beispiel #1
0
        public async Task SameStateEventShouldNotCallFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var called = false;

            // ACT
            DefaultDaemonRxApp.StateChanges
            .Subscribe(s =>
            {
                called = true;
            });

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.False(called);
        }
Beispiel #2
0
        public async Task EntityOnStateChangedMultipleTimesCallsCorrectServiceCall()
        {
            // ARRANGE
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");
            DefaultHassClientMock.AddChangedEvent("binary_sensor.pir", "off", "on");

            var cancelSource = DefaultHassClientMock.GetSourceWithTimeout();

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

            await RunDefauldDaemonUntilCanceled();

            DefaultHassClientMock.VerifyCallServiceTimes("turn_on", Times.Exactly(2));
            DefaultHassClientMock.VerifyCallService("light", "turn_on",
                                                    ("entity_id", "light.correct_entity"));
        }
Beispiel #3
0
        public async Task CallbackObserverAttributeMissingShouldReturnNull()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            string?missingString = "has initial value";

            // ACT
            DefaultDaemonRxApp.Entities(n => n.EntityId.StartsWith("binary_sensor.pir"))
            .StateChanges
            .Subscribe(s =>
            {
                missingString = s.New.Attribute?.missing_attribute;
            });

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.Null(missingString);
        }
Beispiel #4
0
        public async Task UsingEntitiesLambdaNewEventShouldCallFunction()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            var called = false;

            // ACT
            DefaultDaemonRxApp.Entities(n => n.EntityId.StartsWith("binary_sensor.pir"))
            .StateChanges
            .Subscribe(s =>
            {
                called = true;
            });

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            // ASSERT
            Assert.True(called);
        }
Beispiel #5
0
        public async Task MissingEntityShouldNotLogErrorAndNotBreakOtherApps()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            bool eventRun = false, event2Run = false;

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(s =>
            {
                eventRun = true;
            });

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

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(s =>
            {
                event2Run = true;
            });

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            LoggerMock.AssertLogged(LogLevel.Error, Times.Never());
            Assert.True(eventRun);
            Assert.True(event2Run);
        }
Beispiel #6
0
        public async Task ARunTimeErrorShouldNotBreakOtherApps()
        {
            // ARRANGE
            await InitializeFakeDaemon().ConfigureAwait(false);

            bool eventRun = false, event2Run = false;

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(s =>
            {
                eventRun = true;
            });

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(s =>
            {
                int x = int.Parse("ss");
            });

            App
            .Entity("binary_sensor.pir")
            .StateChanges
            .Subscribe(s =>
            {
                event2Run = true;
            });

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

            LoggerMock.AssertLogged(LogLevel.Error, Times.Once());
            Assert.True(eventRun);
            Assert.True(event2Run);
        }
Beispiel #7
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(10))
                            .Subscribe(_ => isRun = true);

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

            await RunFakeDaemonUntilTimeout().ConfigureAwait(false);

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

            await Task.Delay(100).ConfigureAwait(false);

            Assert.True(isRun);
        }
Beispiel #8
0
        public async Task CancelChangedStateForSubscriptionWillNotMakeCallback()
        {
            // ARRANGE
            bool isCalled = false;

            // ACT
            var id = DefaultDaemonHost.ListenState("binary_sensor.pir", (entityId, newState, oldState) =>
            {
                isCalled = true;

                return(Task.CompletedTask);
            });

            DefaultDaemonHost.CancelListenState(id !);

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

            await RunDefauldDaemonUntilCanceled();

            // ASSERT
            Assert.False(isCalled);
            Assert.Empty(DefaultDaemonHost.InternalStateActions);
        }
Beispiel #9
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);
        }