Note: Auto-discovery and some semi-automated sign-in is planned for the future!
Get State
var statesClient = ClientFactory.GetClient<StatesClient>();
var state = await statesClient.GetState("sun.sun");
// state.EntityId: "sun.sun"
// state.State: "below_horizon"
Call a Service
var serviceClient = ClientFactory.GetClient<ServiceClient>();
var resultingState = await serviceClient.CallService("homeassistant", "restart");
// Or
var resultingState = await serviceClient.CallService("light", "turn_on", new { entity_id = "light.my_light" });
// Or
var resultingState = await serviceClient.CallService("light.turn_on", new { entity_id = "light.my_light" });
// Or
var resultingState = await serviceClient.CallService("light.turn_on", @"{""entity_id"":""light.my_light""}");
If you are a Microsoft/.NET developer and fan like I am, I would love your feedback on this project!
N.B. I am also aspiring to eventually create a web/tablet dashboard based off of this project that uses ASP.NET Core and is installable via the Hass.io Add-on interface, but wanted to get this core API client library out there first for anyone else that wants to use it!
@jarrettv I’m actually targeting .NET Standard 2.0 which does have HttpClientFactory in it - and to be honest, I just used RestSharp because I’m most familiar with it, so I could get this prototype up fairly quickly (and it’s just a nice wrapper around HttpWebRequest).
I’m definitely not opposed to researching using HttpClientFactory instead, though (one less dependency! ). I’ll see how hard it would be to swap out - especially since it’s early on, I’m not risking breaking anything right now.
And the dashboard will probably be Razor MVC with the dashboard(s) actually having an admin/configuration UI so you don’t have to do a lot of configuration file stuff manually - it’ll all be point and click - at least that’s my goal!
Version 0.2.0 has been released with support for rendering HA templates, and a wrapper for the States client called EntityClient which will retrieve a list of entities. (Home Assistant doesn’t have an “entity API” endpoint, and going to the StatesClient to get a list of entities seemed unintuitive).
Version 1.0.0 has been released with a small enhancement to the EntityClient where you can call GetEntities and pass a domain filter, e.g. entityClient.GetEntities("light"); retrieves a list of only light entities.
I have been using this library via another upcoming project regularly, and I feel as of right now, it’s stable enough to release as version 1.0! Hooray!