Beispiel #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));
        }
Beispiel #2
0
        public void AsNumeric_Than_WithAttributesAs()
        {
            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);

            entity.State.Should().Be("12.3");

            // Act: AsNumeric
            var numericEntity = entity.AsNumeric();

            // Assert
            numericEntity.State !.Value !.Should().Be(12.3d);
            numericEntity.EntityState !.State !.Value !.Should().Be(12.3d);
            numericEntity.StateAllChanges().Where(e => e.New?.State > 1.2);
            // Act: WithAttributesAs
            var withAttributes = numericEntity.WithAttributesAs <TestSensorAttributes>();

            // Assert
            withAttributes.State !.Value !.Should().Be(12.3d);
            withAttributes.EntityState !.State !.Value !.Should().Be(12.3d);

            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);
        }