HADotNet - A .NET Standard Library for Home Assistant

Overview

I’ve started an open-source code library to wrap the official external Home Assistant API into an easy-to-use, strongly-typed C# library.

You can install the initial beta release from NuGet: Install-Package HADotNet.Core -Version 0.1.0-beta

The project’s homepage is located here: https://github.com/qJake/HADotNet/

Code Samples

First, perform one-time initialization:

ClientFactory.Initialize("https://my-home-assistant-url/", "AbCdEf0123456789...");

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! :smiley:

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!

13 Likes

This looks great! I’m new to the HA api so I’d love to see a few more examples around GetStates and GetServices.

I assume you are using RestSharp rather than using a typed HttpClient because the latter requires 2.1?

Are you building the dashboard with razor pages?

@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! :tada:). 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! :slight_smile:

1 Like

Version 0.2.x

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).

Get this version via NuGet: Install-Package HADotNet.Core -Version 0.2.0

View this release on GitHub.

Version 1.0.0

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! :tada: Hooray!

Get this version via NuGet: Install-Package HADotNet.Core -Version 1.0.0

View this release on GitHub.

2 Likes