Skip to content

Instantly share code, notes, and snippets.

@rille111
Last active February 25, 2019 11:33
Show Gist options
  • Save rille111/8f797c6ac174641e916ccd5495764347 to your computer and use it in GitHub Desktop.
Save rille111/8f797c6ac174641e916ccd5495764347 to your computer and use it in GitHub Desktop.
Example for a C# .NET Home-Assistant consuming app
using System.Threading.Tasks;
using NLog;
using Rille.Hass.ApiProxy;
using Rille.Hass.AppStarter;
using Rille.Hass.Models.StateChangeEventGraph;
namespace HassLab.Console.HassApps
{
public class WakeUpApp : IHassApp
{
public string EntityIdentifierFilter { get; set; } = "automation.wakeup_*"; // <-- yes, wildcards are supported!
// Dependencies (IoC or Factories not supported right now)
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
private readonly HassApiProxy _hassApiProxy = new HassApiProxy(new ApiConfigForHass { BaseUrl = "http://192.168.0.201:8123/api" });
public async Task ExecuteAsync(EntityStateChangeData e, string dataUntouched)
{
if (e.OldState != "on" || e.NewState != "on")
return; // A trigger for an automation has both these states set to "on" by some reason.
_logger.Info($"Executing {nameof(WakeUpApp)} for [{e.Entity}]");
// Turn on all lamps
await TurnOnLightFor("light.dimmer_vardagsrum_level", 155);
await TurnOnLightFor("light.dimmer_minihall_level", 155);
await TurnOnLightFor("light.dimmer_hall_level", 155);
await TurnOnLightFor("light.led_sovrum_tak_level", 255);
}
private async Task TurnOnLightFor(string entityId, int brightness)
{
dynamic jsonObj = new
{
entity_id = entityId,
brightness
};
await _hassApiProxy.CallHassService("light", "turn_on", jsonObj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment