Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lazy for state value in StateChange #1159

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/HassModel/NetDaemon.HassModel.Tests/Entities/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHaContext>();
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);
}
}
27 changes: 13 additions & 14 deletions src/HassModel/NetDeamon.HassModel/Entities/StateChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
/// </summary>
public record StateChange
{
private readonly JsonElement _jsonElement;
private readonly IHaContext _haContext;
private Entity? _entity;
private EntityState? _old;
private EntityState? _new;
private readonly Lazy<Entity> _entity;
private readonly Lazy<EntityState?> _old;
private readonly Lazy<EntityState?> _new;

/// <summary>
/// Creates a StateChange from a jsonElement and lazy load the states
Expand All @@ -18,8 +16,10 @@ public record StateChange
/// <param name="haContext"></param>
internal StateChange(JsonElement jsonElement, IHaContext haContext)
{
_jsonElement = jsonElement;
_haContext = haContext;
_entity = new Lazy<Entity>(() =>
new Entity(haContext, jsonElement.GetProperty("entity_id").GetString() ?? throw new InvalidOperationException("No Entity_id in state_change event")));
_new = new Lazy<EntityState?>(() => jsonElement.GetProperty("new_state").Deserialize<EntityState>());
_old = new Lazy<EntityState?>(() => jsonElement.GetProperty("old_state").Deserialize<EntityState>());
}

/// <summary>
Expand All @@ -30,20 +30,19 @@ internal StateChange(JsonElement jsonElement, IHaContext haContext)
/// <param name="new"></param>
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>(() => entity);
_new = new Lazy<EntityState?>(() => @new);
_old = new Lazy<EntityState?>(() => old);
}

/// <summary>The Entity that changed</summary>
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;

/// <summary>The old state of the entity</summary>
public virtual EntityState? Old => _old ??= _jsonElement.GetProperty("old_state").Deserialize<EntityState>();
public virtual EntityState? Old => _old.Value;

/// <summary>The new state of the entity</summary>
public virtual EntityState? New => _new ??= _jsonElement.GetProperty("new_state").Deserialize<EntityState>();
public virtual EntityState? New => _new.Value;
}

/// <summary>
Expand Down
Loading