Skip to content

Instantly share code, notes, and snippets.

@minhhungit
Forked from rille111/wakeupapp-homeassistant.cs
Created February 25, 2019 11:33
Show Gist options
  • Save minhhungit/4210bec3d7c41004ae59712fca975c2b to your computer and use it in GitHub Desktop.
Save minhhungit/4210bec3d7c41004ae59712fca975c2b 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