-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
Hass model areas #542
Hass model areas #542
Conversation
var areas = await _hassClient.GetAreas().ConfigureAwait(false); | ||
var areaDict = areas?.ToDictionary(k => k.Id!, v => v); | ||
|
||
_latestAreas.Clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of Clearing the dictionary and re-loading you could create a whole new dictionary, load it and then re-assign the private field to the new dictionary when it is fully loaded. This will prevent any possible concurrency issue between clearing and re-filling it. You then don't even need a ConcurrentDictionary because it is only ever written to initially by one thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added - Commit
return _latestAreas.TryGetValue(entityId, out var result) ? result : null; | ||
} | ||
|
||
public void Dispose() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I like to put Dispose() last
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
/// </summary> | ||
/// <param name="entityId"></param> | ||
/// <returns></returns> | ||
HassArea? GetArea(string entityId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the HassArea class from the HassClient? I prefer not to expose that in the HassModel public API. HassModel currenty only uses HassClient internally.
I was thinking of a design where IHaContext returns some more metadata about an Entity in the form of an EntityMetaData
object that has Device, Area etc. That will however be a bit more complicated as we need to design such a structure which is basically why this Area was not implemented yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed on discord. Refactored to use simple class and not expose the HassClient models.
I really like this It is almost exactly what I had in mind for this. |
Cheers for the feedback @FrankBakkerNl any other suggestions let me know 👍 |
Thank you for the PR! |
Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com>
Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com>
Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com>
* Fix return area id instead of area name. * Fix UT for area id vs name. * Create new area cache * Add area cache and expose get area to HaContext implementation and interface * Update entity to get area name from context * Update DI * Add tests for area cache and entity wrap * tidy and refactor init call to cache * rebuild list locally then reasign * move dispose to last * add area class * Add mapper from HassArea -> Area * Refactor removing HassClient objects * update entity GetAreaFromEntityId * update UT GetAreaFromEntityId * Update src/HassModel/NetDeamon.HassModel/Entities/Area.cs Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com> * Update src/HassModel/NetDeamon.HassModel/Internal/HassObjectMapper.cs Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com> * Update src/HassModel/NetDeamon.HassModel/Internal/HassObjectMapper.cs Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com> * remove area from m3app Co-authored-by: Frank Bakker <FrankBakkerNl@users.noreply.github.com>
Breaking change
Proposed change
Fixes an issue in APIv2 where area is populated with the area id instead of the name (which I think is incorrect)
Adds area name to the new hass model api.
Type of change
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed: