A wrapper around the excellent HADotNet.Core REST api by @qJake with strongly typed Entity classes
- .NET Standard 2.0 cross-platform library
- DI-friendly initialization
- Home Assistant entities represented by strongly typed C# classes
- Strongly typed methods (TurnOn, TurnOff etc.)
- Strongly typed attributes (climate.CurrentTemperature, mediaPlayer.IsMuted etc.)
- Websockets or any real-time update strategy
- binary_sensor
- climate (TurnOn, TurnOff, SetTemperature, SetHvacMode)
- light (TurnOn, TurnOff, Toggle)
- switch
- media_player (TurnOn, TurnOff, Toggle, SelectSource, Play, Pause, Stop, Mute, VolumeUp, VolumeDown, VolumeSet)
- sensor
- switch (TurnOn, TurnOff, Toggle)
The package is available on nuget.org:
Install-Package HADotNet.Entities
// Initialize the HADotNet.Core clients
ClientFactory.Initialize("https://my-ha-instance.duckdns.org", "long-lived-access-token");
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()));
// Get a single climate entity
var climate = await entitiesService.GetEntity<Climate>("living_room"); // climate.living_room
Console.WriteLine($"Current target temperature: {climate.Temperature}");
await climate.SetTemperature(20);
Console.WriteLine($"New target temperature: {climate.Temperature}");
var light = await entitiesService.GetEntity<Light>("living_room"); // light.living_room
Thread.Sleep(10000);
await light.Update();