Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Test with getstate and get template
Browse files Browse the repository at this point in the history
  • Loading branch information
lubeda committed Nov 11, 2022
1 parent 5e743e8 commit 5fe09ea
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

public class HomeAssistandCommand : PluginDynamicCommand
{

public HomeAssistandCommand() : base("Home assistant", "Controll your Home", "Entity")
{
public HomeAssistandCommand() : base("Call Service", "Controll your Home", "Entity")
{
this.MakeProfileAction("tree");
}

protected override PluginProfileActionData GetProfileActionData()
{
var tree = new PluginProfileActionTree("Select Service and entity");
Expand All @@ -27,7 +25,7 @@ protected override PluginProfileActionData GetProfileActionData()
foreach (String s in e.Entities)
{
pluginProfileActionTreeNode.SetPropertyValue("Service", e.Service);
pluginProfileActionTreeNode.AddItem(e.Service + "|" + s,s,e.Service);
pluginProfileActionTreeNode.AddItem(e.Service + "|" + s, s, e.Service);
}
}
}
Expand All @@ -37,7 +35,7 @@ protected override PluginProfileActionData GetProfileActionData()

protected override String GetCommandDisplayName(String actionParameter, PluginImageSize imageSize) => String.IsNullOrEmpty(actionParameter) ? "HomeAssistant" : actionParameter;


protected override void RunCommand(String actionParameter)
{
if (actionParameter.Contains("|"))
Expand All @@ -50,25 +48,9 @@ protected override void RunCommand(String actionParameter)
var body = @"{""entity_id"": """ + entity + @"""}";
_client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", HomeAssistantPlugin.HomeAssistantPlugin.Config.Token);
var content = new StringContent(body,System.Text.Encoding.UTF8, "application/json"); //https://developers.home-assistant.io/docs/api/rest/
var content = new StringContent(body, System.Text.Encoding.UTF8, "application/json"); //https://developers.home-assistant.io/docs/api/rest/
_client.PostAsync(url, content);
}
}

/*
protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
{
using (var bitmapBuilder = new BitmapBuilder(imageSize))
{
bitmapBuilder.Clear(BitmapColor.Black);
if (actionParameter.Contains("|"))
{
bitmapBuilder.DrawText(actionParameter.Split("|")[1].Replace(".",".\n"), BitmapColor.White, 15, 13, 3);
}
return bitmapBuilder.ToImage();
}
} */

}
}
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);
}
}
}

}
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);
}
}
}

}
Loading

0 comments on commit 5fe09ea

Please sign in to comment.