diff --git a/src/HassModel/NetDaemon.HassModel.Tests/Entities/EntityTest.cs b/src/HassModel/NetDaemon.HassModel.Tests/Entities/EntityTest.cs index a59e7fdde..8a2721eb1 100644 --- a/src/HassModel/NetDaemon.HassModel.Tests/Entities/EntityTest.cs +++ b/src/HassModel/NetDaemon.HassModel.Tests/Entities/EntityTest.cs @@ -102,4 +102,18 @@ public void ShouldWrapAreaFromContext() // Assert Assert.Equal("Area Name", target.Area); } + + [Fact] + public void StateChange_WithTestConstructor_PropertiesShouldNotThrow() + { + // Arrange + var haContextMock = new Mock(); + var target = new TestEntity(haContextMock.Object, "domain.testEntity"); + var stateChange = new StateChange(target, null, null); + + // Act & Assert + Assert.NotNull(stateChange.Entity); + Assert.Null(stateChange.Old); + Assert.Null(stateChange.New); + } } diff --git a/src/HassModel/NetDeamon.HassModel/Entities/StateChange.cs b/src/HassModel/NetDeamon.HassModel/Entities/StateChange.cs index 96876cc98..97a624d3c 100644 --- a/src/HassModel/NetDeamon.HassModel/Entities/StateChange.cs +++ b/src/HassModel/NetDeamon.HassModel/Entities/StateChange.cs @@ -5,11 +5,9 @@ /// public record StateChange { - private readonly JsonElement _jsonElement; - private readonly IHaContext _haContext; - private Entity? _entity; - private EntityState? _old; - private EntityState? _new; + private readonly Lazy _entity; + private readonly Lazy _old; + private readonly Lazy _new; /// /// Creates a StateChange from a jsonElement and lazy load the states @@ -18,8 +16,10 @@ public record StateChange /// internal StateChange(JsonElement jsonElement, IHaContext haContext) { - _jsonElement = jsonElement; - _haContext = haContext; + _entity = new Lazy(() => + new Entity(haContext, jsonElement.GetProperty("entity_id").GetString() ?? throw new InvalidOperationException("No Entity_id in state_change event"))); + _new = new Lazy(() => jsonElement.GetProperty("new_state").Deserialize()); + _old = new Lazy(() => jsonElement.GetProperty("old_state").Deserialize()); } /// @@ -30,20 +30,19 @@ internal StateChange(JsonElement jsonElement, IHaContext haContext) /// public StateChange(Entity entity, EntityState? old, EntityState? @new) { - _entity = entity; - _new = @new; - _old = old; - _haContext = null!; // haContext is not used when _entity is already initialized + _entity = new Lazy(() => entity); + _new = new Lazy(() => @new); + _old = new Lazy(() => old); } /// The Entity that changed - public virtual Entity Entity => _entity ??= new Entity(_haContext, _jsonElement.GetProperty("entity_id").GetString() ?? throw new InvalidOperationException("No Entity_id in state_change event")); + public virtual Entity Entity => _entity.Value; /// The old state of the entity - public virtual EntityState? Old => _old ??= _jsonElement.GetProperty("old_state").Deserialize(); + public virtual EntityState? Old => _old.Value; /// The new state of the entity - public virtual EntityState? New => _new ??= _jsonElement.GetProperty("new_state").Deserialize(); + public virtual EntityState? New => _new.Value; } ///