diff options
| author | Jake Mannens <jake@asger.xyz> | 2024-09-20 16:21:09 +1000 |
|---|---|---|
| committer | Jake Mannens <jake@asger.xyz> | 2024-10-29 11:42:54 +1100 |
| commit | e8e3c4cba8ffa0056e984c113cfbb75319e00022 (patch) | |
| tree | 8336315e61f9e77207276d478b25fe5dc7c3d06c /Handlers/HomeAssistantHandler.cs | |
| parent | 0e21907c76dbefed11f382bcf949143f0716567f (diff) | |
v0.4-rc1v0.4-rc1
Diffstat (limited to 'Handlers/HomeAssistantHandler.cs')
| -rw-r--r-- | Handlers/HomeAssistantHandler.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/Handlers/HomeAssistantHandler.cs b/Handlers/HomeAssistantHandler.cs new file mode 100644 index 0000000..bf914c3 --- /dev/null +++ b/Handlers/HomeAssistantHandler.cs @@ -0,0 +1,54 @@ +using System.Net.Http.Headers; + +namespace PagerParser.Handlers; + +[PagerHandler] +public class HomeAssistantHandler : IPagerHandler { + private List<HomeAssistantConfig> haConfigs = new(); + + private ILogger logger; + + private HttpClient httpClient; + + public async Task HandleMessageAsync(PagerMessage message, ParsedPagerMessage? pm) { + // Generate an event via the API on each configured Home Assistant server + foreach(var ha in haConfigs) { + var url = $"https://{ha.Host}/api/events/{ha.EventType}"; + httpClient.DefaultRequestHeaders.Authorization = + new AuthenticationHeaderValue("Bearer", ha.ApiKey); + await httpClient.PostAsJsonAsync( + url, + message); + } + } + + public void OnConfiguring( + ILogger logger, + IConfiguration config, + IServiceProvider serviceProvider) { + + this.logger = logger; + + config.Bind("PagerParser:HomeAssistant:Servers", haConfigs); + if(haConfigs is null) + return; + + logger.LogInformation($"{haConfigs.Count()} Home Assistant servers configured"); + } + + public Task StartAsync(CancellationToken ct) { + httpClient = new(); + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken ct) { + httpClient?.Dispose(); + return Task.CompletedTask; + } + + public class HomeAssistantConfig { + public string Host { get; set; } + public string ApiKey { get; set; } + public string EventType { get; set; } = "cfa_pager_message"; + } +} |
