コード例 #1
0
        public void ShouldShowStateChangesFromContext()
        {
            var haContextMock = new HaContextMock();

            var target = new NumericEntity(haContextMock.Object, "domain.testEntity");
            var stateChangeObserverMock    = new Mock <IObserver <NumericStateChange> >();
            var stateAllChangeObserverMock = new Mock <IObserver <NumericStateChange> >();

            target.StateAllChanges().Subscribe(stateAllChangeObserverMock.Object);
            target.StateChanges().Subscribe(stateChangeObserverMock.Object);

            haContextMock.StateAllChangeSubject.OnNext(new StateChange(target,
                                                                       old:  new EntityState {
                State = "1"
            },
                                                                       @new: new EntityState {
                State = "1"
            }));

            haContextMock.StateAllChangeSubject.OnNext(new StateChange(target,
                                                                       old:  new EntityState {
                State = "1"
            },
                                                                       @new: new EntityState {
                State = "2"
            }));

            stateChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>(e => e.New !.State !.Equals(1.0))), Times.Never);
            stateChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>(e => e.New !.State !.Equals(2.0))), Times.Once);

            stateAllChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>(e => e.Old !.State !.Equals(1.0))), Times.Exactly(2));
            stateAllChangeObserverMock.Verify(o => o.OnNext(It.IsAny <NumericStateChange>()), Times.Exactly(2));
        }
コード例 #2
0
    public void ShouldShowStateChangesFromContext()
    {
        var haContextMock = new HaContextMock();

        var entity = new Entity(haContextMock.Object, "domain.testEntity");
        var target = new NumericEntity(entity);

        haContextMock.Setup(m => m.GetState(entity.EntityId)).Returns(new EntityState()
        {
            State = "3.14"
        });

        var stateAllChangeObserverMock = target.StateAllChanges().SubscribeMock();
        var stateChangeObserverMock    = target.StateChanges().SubscribeMock();

        haContextMock.StateAllChangeSubject.OnNext(new StateChange(entity,
                                                                   old: new EntityState {
            State = "1"
        },
                                                                   @new: new EntityState {
            State = "1"
        }));

        haContextMock.StateAllChangeSubject.OnNext(new StateChange(entity,
                                                                   old: new EntityState {
            State = "1"
        },
                                                                   @new: new EntityState {
            State = "2"
        }));

        // Assert
        stateChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>
                                                         (e => e.Entity.State.Equals(3.14) &&
                                                         e.Old !.State.Equals(1.0) &&
                                                         e.New !.State.Equals(2.0))), Times.Once);
        stateChangeObserverMock.VerifyNoOtherCalls();

        stateAllChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>
                                                            (e => e.Entity.State.Equals(3.14) &&
                                                            e.Old !.State.Equals(1.0) &&
                                                            e.New !.State.Equals(2.0))), Times.Once);

        stateAllChangeObserverMock.Verify(o => o.OnNext(It.Is <NumericStateChange>
                                                            (e => e.Entity.State.Equals(3.14) &&
                                                            e.Old !.State.Equals(1.0) &&
                                                            e.New !.State.Equals(1.0))), Times.Once);
        stateAllChangeObserverMock.VerifyNoOtherCalls();
    }
コード例 #3
0
    public void WithAttributesAs_Than_AsNumeric()
    {
        var entityId = "sensor.temperature";

        var haContextMock = new HaContextMock();

        haContextMock.Setup(m => m.GetState(entityId)).Returns(
            new EntityState
        {
            EntityId       = entityId,
            State          = "12.3",
            AttributesJson = JsonSerializer.Deserialize <JsonElement>(@"{""setPoint"": 21.5, ""units"": ""Celcius""}")
        });

        var entity = new Entity(haContextMock.Object, entityId);

        // Act: WithAttributesAs
        Entity <TestSensorAttributes>        withAttributes = entity.WithAttributesAs <TestSensorAttributes>();
        NumericEntity <TestSensorAttributes> numericEntity  = withAttributes.AsNumeric();

        // Assert
        withAttributes.State.Should().Be("12.3", because: "State  is still a string");

        withAttributes.Attributes !.units.Should().Be("Celcius");
        withAttributes.Attributes !.setPoint.Should().Be(21.5);
        withAttributes.EntityState !.Attributes !.units.Should().Be("Celcius");
        withAttributes.EntityState !.Attributes !.setPoint.Should().Be(21.5);

        // Act: AsNumeric()
        var numericWithAttributes = withAttributes.AsNumeric();

        numericWithAttributes.State !.Value !.Should().Be(12.3d);
        numericWithAttributes.EntityState !.State !.Value !.Should().Be(12.3d);

        numericWithAttributes.Attributes !.units.Should().Be("Celcius");
        numericWithAttributes.Attributes !.setPoint.Should().Be(21.5);
        numericWithAttributes.EntityState !.Attributes !.units.Should().Be("Celcius");
        numericWithAttributes.EntityState !.Attributes !.setPoint.Should().Be(21.5);

        haContextMock.StateAllChangeSubject.OnNext(new StateChange(entity, new EntityState(), new EntityState()));
        numericWithAttributes.StateAllChanges().Where(e => e.New?.State > 1.2 && e.Entity != null).Subscribe();
    }