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

Commit

Permalink
Merge pull request #2 from lubeda/v0.3
Browse files Browse the repository at this point in the history
V0.3
  • Loading branch information
lubeda authored Nov 11, 2022
2 parents 3252152 + 27aa8d5 commit 4e9a55b
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
namespace Loupedeck.HomeAssistantPlugin.Actions
namespace Loupedeck.HomeAssistantPlugin
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
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 IDictionary<String, StateData> stateData = new Dictionary<String, StateData>();
protected Timer timer;

protected class StateData
Expand All @@ -22,7 +21,7 @@ protected class StateData
public Boolean IsLoading = false;
}

public HomeAssistantStateCommand() : base("Get State", "Get the state of an entity", "")
public HomeAssistantStateCommand() : base("Get a state", "Get the state value of an entity.", "")
{
this.MakeProfileAction("text;Enter entity");

Expand Down Expand Up @@ -52,11 +51,7 @@ protected override BitmapImage GetCommandImage(String actionParameter, PluginIma
}

StateData s = this.GetStateData(actionParameter);
if (!s.IsValid)
{
return null;
}


var img = new BitmapBuilder(imageSize);
using (var bitmapBuilder = new BitmapBuilder(imageSize))
{
Expand All @@ -70,32 +65,25 @@ protected override BitmapImage GetCommandImage(String actionParameter, PluginIma
{
bitmapBuilder.DrawText(actionParameter);
}


return bitmapBuilder.ToImage();
}
}

protected StateData GetStateData(string actionParameter)
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.stateData[actionParameter] = d;

this.LoadData(actionParameter);

return this.stateData[actionParameter];
return d;
}

protected async void LoadData(String actionParameter)
Expand All @@ -105,14 +93,11 @@ protected async void LoadData(String actionParameter)
return;
}

if (this.stateData[actionParameter] == null)
{
this.stateData[actionParameter] = new StateData();
}

StateData d = this.GetStateData(actionParameter);

if (d.IsLoading)
{
d.IsValid = false;
return;
}

Expand All @@ -121,24 +106,41 @@ protected async void LoadData(String actionParameter)
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>();
var resp = await _client.GetAsync(url);
if (resp.IsSuccessStatusCode)
{
try
{
var body = await resp.Content.ReadAsStringAsync();
var json = JsonNode.Parse(body);
d.state = json["state"].GetValue<String>();
d.IsValid = true;
}
catch (HttpRequestException e)
{
d.state = e.Message;
d.IsValid = true;
}
}
else
{
d.state = "Error1";
}

}
catch (Exception e)
{
d.state = "error";
d.state = "Error2";
}
finally
{
d.IsLoading = false;
d.IsValid = true;
this.ActionImageChanged(actionParameter);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json.Nodes;
using System.Timers;

class HomeAssistantTemplateCommand : PluginDynamicCommand
Expand Down Expand Up @@ -81,9 +80,9 @@ protected TemplateData GetTemplateData(String actionParameter)
return d;

d = new TemplateData();
templateData[actionParameter] = d;
this.templateData[actionParameter] = d;

LoadData(actionParameter);
this.LoadData(actionParameter);

return d;
}
Expand Down Expand Up @@ -126,7 +125,7 @@ protected async void LoadData(String actionParameter)
}
catch (Exception e)
{
d.template = "error";
d.template = "Error";
}
finally
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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;
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);

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("Error");
}
return bitmapBuilder.ToImage();
}
}
protected StateData GetStateData(String actionParameter)
{
StateData d;

if (!this.stateData.ContainsKey(actionParameter))
{
d = new StateData();
this.stateData[actionParameter] = d;
} else
{
d = this.stateData[actionParameter];
}

this.LoadData(actionParameter);

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

}
Loading

0 comments on commit 4e9a55b

Please sign in to comment.