Skip to content

Commit d37b84b

Browse files
333fredhelto4real
andauthored
Strongly-type EntityState.Attributes (#1251)
For those working with base entities (such as the IHaRegistry API), the virtual property/covariant return override approach isn't helpful. The base property will always return a `Dictionary<string, object>`, which can be useful to iterate through. Instead, we should have strongly-typed derived class _hide_ the base property, so users who are just working with pure entities get a useful key/value pair, and derived users get strongly-typed info. Co-authored-by: Tomas Hellström <[email protected]>
1 parent 214ee3d commit d37b84b

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/HassModel/NetDeamon.HassModel/Entities/Entity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Entity(IEntityCore entity)
4343
/// <summary>
4444
/// The current Attributes of this Entity
4545
/// </summary>
46-
public virtual object? Attributes => EntityState?.Attributes;
46+
public Dictionary<string, object>? Attributes => EntityState?.Attributes;
4747

4848
/// <summary>
4949
/// The full state of this Entity
@@ -110,7 +110,7 @@ protected Entity(IHaContext haContext, string entityId) : base(haContext, entity
110110
{ }
111111

112112
/// <inheritdoc />
113-
public override TAttributes? Attributes => EntityState?.Attributes;
113+
public new virtual TAttributes? Attributes => EntityState?.Attributes;
114114

115115
/// <inheritdoc />
116116
public override TEntityState? EntityState => MapState(base.EntityState);

src/HassModel/NetDeamon.HassModel/Entities/EntityState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public record EntityState
1717
/// <summary>
1818
/// The attributes
1919
/// </summary>
20-
public virtual object? Attributes => AttributesJson?.Deserialize<Dictionary<string, object>>() ?? [];
20+
public Dictionary<string, object>? Attributes => AttributesJson?.Deserialize<Dictionary<string, object>>() ?? [];
2121

2222
/// <summary>Last changed, when state changed from and to different values</summary>
2323
[JsonPropertyName("last_changed")] public DateTime? LastChanged { get; init; }
@@ -52,5 +52,5 @@ public EntityState(EntityState source) : base(source)
5252
}
5353

5454
/// <inheritdoc/>
55-
public override TAttributes? Attributes => _attributesLazy.Value;
55+
public new virtual TAttributes? Attributes => _attributesLazy.Value;
5656
}

0 commit comments

Comments
 (0)