Skip to content

Commit

Permalink
Properly deserialize context when deserializing state in the entity s…
Browse files Browse the repository at this point in the history
…tate cache (#1157)

* Properly deserialize context when deserializing state in the state cache

* Update tests to pass state change events as raw JSON

* Fix missed assert

---------

Co-authored-by: Tomas Hellström <tomas.hellstrom@yahoo.se>
Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 1, 2024
1 parent c9bc201 commit 897c9e4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NetDaemon.Client.HomeAssistant.Model;
using NetDaemon.Client.Internal.HomeAssistant.Commands;
using NetDaemon.HassModel.Tests.TestHelpers;
using Microsoft.Extensions.DependencyInjection;
using NetDaemon.Client.Internal.Extensions;
using NetDaemon.HassModel.Internal;

Expand Down Expand Up @@ -43,13 +42,18 @@ public async Task StateChangeEventIsFirstStoredInCacheThanForwarded()
cache.GetState(entityId)!.State.Should().Be("InitialState", "The initial value should be available");

// Act 2: now fire a state change event
var changedEventData = new HassStateChangedEventData
var changedEventData = new
{
EntityId = entityId,
OldState = new HassState(),
NewState = new HassState
entity_id = entityId,
old_state = new { },
new_state = new
{
State = "newState"
state = "newState",
context = new
{
id = "contextId",
parent_id = "parentId"
}
}
};

Expand All @@ -71,6 +75,8 @@ public async Task StateChangeEventIsFirstStoredInCacheThanForwarded()
// Assert
eventObserverMock.Verify(m => m.OnNext(It.IsAny<HassEvent>()));
cache.GetState(entityId)!.State.Should().Be("newState");
cache.GetState(entityId)!.Context!.Id.Should().Be("contextId");
cache.GetState(entityId)!.Context!.ParentId.Should().Be("parentId");
}

[Fact]
Expand Down Expand Up @@ -112,36 +118,46 @@ public async Task AllEntityIds_returnsInitialPlusChangedEntities()
testSubject.OnNext(new HassEvent
{
EventType = "state_changed",
DataElement = new HassStateChangedEventData
DataElement = new
{
EntityId = "sensor.sensor1",
OldState = new HassState(),
NewState = new HassState
entity_id = "sensor.sensor1",
old_state = new { },
new_state = new
{
State = "newState",
AttributesJson = new {brightness = 200}.ToJsonElement()
state = "newState",
context = new
{
id = "contextId1"
},
attributes = new {brightness = 200}
}
}.AsJsonElement()
});

testSubject.OnNext(new HassEvent
{
EventType = "state_changed",
DataElement = new HassStateChangedEventData
DataElement = new
{
EntityId = "sensor.sensor2",
OldState = new HassState(),
NewState = new HassState
entity_id = "sensor.sensor2",
old_state = new { },
new_state = new
{
State = "newState",
AttributesJson = new {brightness = 300}.ToJsonElement()
state = "newState",
context = new
{
id = "contextId2"
},
attributes = new {brightness = 300}
}
}.AsJsonElement()
});

// Assert
cache.AllEntityIds.Should().BeEquivalentTo("sensor.sensor1", "sensor.sensor2");
cache.GetState("sensor.sensor1")!.AttributesJson.GetValueOrDefault().GetProperty("brightness").GetInt32().Should().Be(200);
cache.GetState("sensor.sensor1")!.Context!.Id.Should().Be("contextId1");
cache.GetState("sensor.sensor2")!.AttributesJson.GetValueOrDefault().GetProperty("brightness").GetInt32().Should().Be(300);
cache.GetState("sensor.sensor2")!.Context!.Id.Should().Be("contextId2");
}
}
10 changes: 5 additions & 5 deletions src/HassModel/NetDeamon.HassModel/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public class Context
/// <summary>
/// Id
/// </summary>
public string Id { get; set; } = "";
[JsonPropertyName("id")] public string Id { get; set; } = "";

/// <summary>
/// ParentId
/// </summary>
public string? ParentId { get; set; }
[JsonPropertyName("parent_id")] public string? ParentId { get; set; }
/// <summary>
/// The id of the user who is responsible for the connected item.
/// </summary>
public string? UserId { get; set; }
}
[JsonPropertyName("user_id")] public string? UserId { get; set; }
}

0 comments on commit 897c9e4

Please sign in to comment.