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

[WIP] A basic proposal setup of entity representations as c# classes #19

Closed
wants to merge 11 commits into from

Conversation

Devqon
Copy link

@Devqon Devqon commented Feb 2, 2021

Please review and comment for using such a setup. I want to have specific classes that represent the HA entities in my (to-be developed) project.

Addresses #12

Features:

  • Extends HADotNet.Core (separate project, probably separate NuGet?)
  • Exposes an EntitiesService with methods to retrieve entities in the form of strongly typed C# classes. Currently implemented:
    • Base Entity (State, LastChanged, LastUpdated, Attributes)
    • Light (TurnOn, TurnOff, Toggle)
    • BinarySensor
    • Sensor
    • Switch (TurnOn, TurnOff, Toggle)
    • Climate (TurnOn, TurnOff, SetTemperature, SetHvacMode)
  • State updates

Proposed (extra) features/nice-to-haves:

  • Extensions per domain for specific actions, for example TurnOn(this IEnumerable<Light> lights) to turn on a collection of lights (maybe in a separate project/NuGet package?)
  • DI friendly
    • Currently the EntitiesService only. Probably also make the Clients DI friendly?
  • Predicates/filters in GetEntities method

Example usage:

var entityClient = ClientFactory.GetClient<EntityClient>();
var statesClient = ClientFactory.GetClient<StatesClient>();

// Initialize an instance of the EntitiesService
var entitiesService = new EntitiesService(entityClient, statesClient);

// Get all light entities
var lights = await entitiesService.GetEntities<Light>();

// Turn on all lights
await Task.WhenAll(lights.Select(light => light.TurnOn()));

@qJake qJake self-requested a review February 2, 2021 16:49
Copy link
Author

@Devqon Devqon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Handle state updates

[SetUp]
public void Setup()
{
_entityClientMock = new Mock<EntityClient>();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: This doesn't work. Sealed classes cannot be mocked. Either delete unittests, use another constructor, or add interfaces.

@qJake
Copy link
Owner

qJake commented Feb 13, 2021

So, I want to take a step back here before anyone adds more code to this...

Please see my comment here: #11

Ultimately, my goal for this library specifically is to provide a C# layer on top of the REST API that Home Assistant provides out of the box. Basically a 1:1 command mapping with this: https://developers.home-assistant.io/docs/api/rest

Now, I completely understand the value here - I totally agree that you should be able to do something like:

Entities.Lights["my_light"].TurnOn();

However, I'd envision that as actually a completely separate project/repo/NuGet package from this one. Reason being, there are folks that just want exactly what this library provides - a nice REST wrapper - and nothing more.

And then there are a different set of folks who actually want the super-friendly, easy-to-use C# interface that may give you some ease of use while sacrificing some performance.

The way I see it, there are really three layers here:

Home Assistant
-------------------
HADotNet (Core)
-------------------
HADotNet (???) Unnamed Library

If there is enough interest in this, I'd be happy to start up a repo and a codebase and start accepting PRs. I think if all of these libraries are under the same name, it may lessen some of the confusion - and I can even add a page to https://hacc.dev that explains these, since they're ultimately the same "family" of products/libraries.

Thoughts?

@qJake
Copy link
Owner

qJake commented Feb 13, 2021

CC @Devqon @PockyBum522

@qJake qJake mentioned this pull request Feb 13, 2021
@Devqon
Copy link
Author

Devqon commented Feb 15, 2021

@qJake I understand your thoughts completely, which is also a reason why I've made everything in a separate project apart from yours (except for the constants, but they could be in the extra project too). The idea was also to put it in a separate NuGet package.

It is totally valid to say this does not/should not belong here. My attempt was only for fullfilling mine and maybe other's extra needs. Because the base was already made by you, I thought it was suitable to extend it from here. If you would rather have this to be a separate github project, no problem at all! Then I'll just move it to my own github (or a new project under yours).

@Devqon
Copy link
Author

Devqon commented Feb 16, 2021

@qJake @PockyBum522 I've moved the code to a separate project on my github:
https://github.com/Devqon/HADotNet.Entities

This PR can be closed

@Devqon Devqon closed this Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants