This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lubeda/getstate
Merge Getstate into main
- Loading branch information
Showing
9 changed files
with
428 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
HomeAssistantPlugin/src/HomeAssistantPlugin/Actions/HomeAssistantStateCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
namespace Loupedeck.HomeAssistantPlugin.Actions | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json.Nodes; | ||
using System.Timers; | ||
using System.Web; | ||
|
||
|
||
class HomeAssistantStateCommand : PluginDynamicCommand | ||
{ | ||
protected HttpClient httpClient = new HttpClient(); | ||
protected IDictionary<string, StateData> stateData = new Dictionary<string, StateData>(); | ||
protected Timer timer; | ||
|
||
protected class StateData | ||
{ | ||
public String state; | ||
public Boolean IsValid = false; | ||
public Boolean IsLoading = false; | ||
} | ||
|
||
public HomeAssistantStateCommand() : base("Get State", "Get the state of an entity", "") | ||
{ | ||
this.MakeProfileAction("text;Enter entity"); | ||
|
||
// Reload the data periodically every 1 minutes | ||
this.timer = new Timer(60 * 1 * 1000); | ||
this.timer.Elapsed += (Object, ElapsedEventArgs) => | ||
{ | ||
foreach (var actionParameter in new List<String>(this.stateData.Keys)) | ||
{ | ||
this.LoadData(actionParameter); | ||
} | ||
}; | ||
this.timer.AutoReset = true; | ||
this.timer.Enabled = true; | ||
} | ||
|
||
protected override void RunCommand(String actionParameter) | ||
{ | ||
this.LoadData(actionParameter); | ||
} | ||
|
||
protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize) | ||
{ | ||
if (actionParameter == null) | ||
{ | ||
return null; | ||
} | ||
|
||
StateData s = this.GetStateData(actionParameter); | ||
if (!s.IsValid) | ||
{ | ||
return null; | ||
} | ||
|
||
var img = new BitmapBuilder(imageSize); | ||
using (var bitmapBuilder = new BitmapBuilder(imageSize)) | ||
{ | ||
var fn = EmbeddedResources.FindFile("ButtonBaseHomeAssistant.png"); | ||
bitmapBuilder.SetBackgroundImage(EmbeddedResources.ReadImage(fn)); | ||
if (this.stateData[actionParameter].IsValid) | ||
{ | ||
bitmapBuilder.DrawText(this.stateData[actionParameter].state); | ||
} | ||
else | ||
{ | ||
bitmapBuilder.DrawText(actionParameter); | ||
} | ||
|
||
|
||
return bitmapBuilder.ToImage(); | ||
} | ||
} | ||
|
||
protected StateData GetStateData(string actionParameter) | ||
{ | ||
StateData d; | ||
if (this.stateData.TryGetValue(actionParameter, out d)) | ||
return d; | ||
|
||
d = new StateData(); | ||
stateData[actionParameter] = d; | ||
|
||
LoadData(actionParameter); | ||
|
||
return d; | ||
} | ||
|
||
protected StateData _GetStateData(String actionParameter) | ||
{ | ||
|
||
this.LoadData(actionParameter); | ||
|
||
return this.stateData[actionParameter]; | ||
} | ||
|
||
protected async void LoadData(String actionParameter) | ||
{ | ||
if (actionParameter == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (this.stateData[actionParameter] == null) | ||
{ | ||
this.stateData[actionParameter] = new StateData(); | ||
} | ||
|
||
StateData d = this.GetStateData(actionParameter); | ||
if (d.IsLoading) | ||
{ | ||
return; | ||
} | ||
|
||
d.IsLoading = true; | ||
|
||
try | ||
{ | ||
var _client = new HttpClient(); | ||
var url = HomeAssistantPlugin.Config.Url + "states/" + actionParameter; | ||
_client.DefaultRequestHeaders.Authorization = | ||
new AuthenticationHeaderValue("Bearer", HomeAssistantPlugin.Config.Token); | ||
HttpResponseMessage resp = await _client.GetAsync(url); | ||
var json = JsonNode.Parse(await resp.Content.ReadAsStringAsync()); | ||
d.state = json["state"].GetValue<String>(); | ||
} | ||
catch (Exception e) | ||
{ | ||
d.state = "error"; | ||
} | ||
finally | ||
{ | ||
d.IsLoading = false; | ||
d.IsValid = true; | ||
this.ActionImageChanged(actionParameter); | ||
} | ||
} | ||
} | ||
|
||
} |
140 changes: 140 additions & 0 deletions
140
HomeAssistantPlugin/src/HomeAssistantPlugin/Actions/HomeAssistantTemplateCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
namespace Loupedeck.HomeAssistantPlugin | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json.Nodes; | ||
using System.Timers; | ||
|
||
class HomeAssistantTemplateCommand : PluginDynamicCommand | ||
{ | ||
protected HttpClient httpClient = new HttpClient(); | ||
protected IDictionary<string, TemplateData> templateData = new Dictionary<string, TemplateData>(); | ||
protected Timer timer; | ||
|
||
protected class TemplateData | ||
{ | ||
public String template; | ||
public Boolean IsValid = false; | ||
public Boolean IsLoading = false; | ||
} | ||
|
||
public HomeAssistantTemplateCommand() : base("Eval a template", "Evaluate a template to show the result.", "") | ||
{ | ||
this.MakeProfileAction("text;Enter template"); | ||
|
||
// Reload the data periodically every 1 minutes | ||
this.timer = new Timer(60 * 1 * 1000); | ||
this.timer.Elapsed += (Object, ElapsedEventArgs) => | ||
{ | ||
foreach (var actionParameter in new List<String>(this.templateData.Keys)) | ||
{ | ||
this.LoadData(actionParameter); | ||
} | ||
}; | ||
this.timer.AutoReset = true; | ||
this.timer.Enabled = true; | ||
} | ||
|
||
protected override void RunCommand(String actionParameter) | ||
{ | ||
this.LoadData(actionParameter); | ||
} | ||
|
||
protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize) | ||
{ | ||
if (actionParameter == null) | ||
{ | ||
return null; | ||
} | ||
|
||
TemplateData s = this.GetTemplateData(actionParameter); | ||
if (!s.IsValid) | ||
{ | ||
return null; | ||
} | ||
|
||
var img = new BitmapBuilder(imageSize); | ||
using (var bitmapBuilder = new BitmapBuilder(imageSize)) | ||
{ | ||
var fn = EmbeddedResources.FindFile("ButtonBaseHomeAssistant.png"); | ||
bitmapBuilder.SetBackgroundImage(EmbeddedResources.ReadImage(fn)); | ||
if (this.templateData[actionParameter].IsValid) | ||
{ | ||
bitmapBuilder.DrawText(this.templateData[actionParameter].template); | ||
} | ||
else | ||
{ | ||
bitmapBuilder.DrawText("Error"); | ||
} | ||
|
||
|
||
return bitmapBuilder.ToImage(); | ||
} | ||
} | ||
|
||
protected TemplateData GetTemplateData(String actionParameter) | ||
{ | ||
TemplateData d; | ||
if (this.templateData.TryGetValue(actionParameter, out d)) | ||
return d; | ||
|
||
d = new TemplateData(); | ||
templateData[actionParameter] = d; | ||
|
||
LoadData(actionParameter); | ||
|
||
return d; | ||
} | ||
|
||
protected async void LoadData(String actionParameter) | ||
{ | ||
if (actionParameter == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (this.templateData[actionParameter] == null) | ||
{ | ||
this.templateData[actionParameter] = new TemplateData(); | ||
} | ||
|
||
TemplateData d = this.GetTemplateData(actionParameter); | ||
if (d.IsLoading) | ||
{ | ||
return; | ||
} | ||
|
||
d.IsLoading = true; | ||
|
||
try | ||
{ | ||
var _client = new HttpClient(); | ||
|
||
var url = HomeAssistantPlugin.Config.Url + "template"; | ||
var body = @"{""template"": """ + actionParameter + @"""}"; | ||
_client.DefaultRequestHeaders.Authorization = | ||
new AuthenticationHeaderValue("Bearer", HomeAssistantPlugin.Config.Token); | ||
var content = new StringContent(body, System.Text.Encoding.UTF8, "application/json"); //https://developers.home-assistant.io/docs/api/rest/ | ||
var resp = await _client.PostAsync(url, content); | ||
if (resp.IsSuccessStatusCode) | ||
{ d.template = await resp.Content.ReadAsStringAsync(); } | ||
else | ||
{ d.template = "Error"; } | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
d.template = "error"; | ||
} | ||
finally | ||
{ | ||
d.IsLoading = false; | ||
d.IsValid = true; | ||
this.ActionImageChanged(actionParameter); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.