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

Commit 4e9a55b

Browse files
authored
Merge pull request #2 from lubeda/v0.3
V0.3
2 parents 3252152 + 27aa8d5 commit 4e9a55b

File tree

7 files changed

+175
-131
lines changed

7 files changed

+175
-131
lines changed
Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
namespace Loupedeck.HomeAssistantPlugin.Actions
1+
namespace Loupedeck.HomeAssistantPlugin
22
{
33
using System;
44
using System.Collections.Generic;
55
using System.Net.Http;
66
using System.Net.Http.Headers;
7+
using System.Reflection;
78
using System.Text.Json.Nodes;
89
using System.Timers;
9-
using System.Web;
10-
1110

1211
class HomeAssistantStateCommand : PluginDynamicCommand
1312
{
1413
protected HttpClient httpClient = new HttpClient();
15-
protected IDictionary<string, StateData> stateData = new Dictionary<string, StateData>();
14+
protected IDictionary<String, StateData> stateData = new Dictionary<String, StateData>();
1615
protected Timer timer;
1716

1817
protected class StateData
@@ -22,7 +21,7 @@ protected class StateData
2221
public Boolean IsLoading = false;
2322
}
2423

25-
public HomeAssistantStateCommand() : base("Get State", "Get the state of an entity", "")
24+
public HomeAssistantStateCommand() : base("Get a state", "Get the state value of an entity.", "")
2625
{
2726
this.MakeProfileAction("text;Enter entity");
2827

@@ -52,11 +51,7 @@ protected override BitmapImage GetCommandImage(String actionParameter, PluginIma
5251
}
5352

5453
StateData s = this.GetStateData(actionParameter);
55-
if (!s.IsValid)
56-
{
57-
return null;
58-
}
59-
54+
6055
var img = new BitmapBuilder(imageSize);
6156
using (var bitmapBuilder = new BitmapBuilder(imageSize))
6257
{
@@ -70,32 +65,25 @@ protected override BitmapImage GetCommandImage(String actionParameter, PluginIma
7065
{
7166
bitmapBuilder.DrawText(actionParameter);
7267
}
73-
74-
7568
return bitmapBuilder.ToImage();
7669
}
7770
}
7871

79-
protected StateData GetStateData(string actionParameter)
72+
protected StateData GetStateData(String actionParameter)
8073
{
8174
StateData d;
75+
8276
if (this.stateData.TryGetValue(actionParameter, out d))
77+
{
8378
return d;
79+
}
8480

8581
d = new StateData();
86-
stateData[actionParameter] = d;
87-
88-
LoadData(actionParameter);
89-
90-
return d;
91-
}
92-
93-
protected StateData _GetStateData(String actionParameter)
94-
{
82+
this.stateData[actionParameter] = d;
9583

9684
this.LoadData(actionParameter);
9785

98-
return this.stateData[actionParameter];
86+
return d;
9987
}
10088

10189
protected async void LoadData(String actionParameter)
@@ -105,14 +93,11 @@ protected async void LoadData(String actionParameter)
10593
return;
10694
}
10795

108-
if (this.stateData[actionParameter] == null)
109-
{
110-
this.stateData[actionParameter] = new StateData();
111-
}
112-
11396
StateData d = this.GetStateData(actionParameter);
97+
11498
if (d.IsLoading)
11599
{
100+
d.IsValid = false;
116101
return;
117102
}
118103

@@ -121,24 +106,41 @@ protected async void LoadData(String actionParameter)
121106
try
122107
{
123108
var _client = new HttpClient();
109+
124110
var url = HomeAssistantPlugin.Config.Url + "states/" + actionParameter;
125111
_client.DefaultRequestHeaders.Authorization =
126112
new AuthenticationHeaderValue("Bearer", HomeAssistantPlugin.Config.Token);
127-
HttpResponseMessage resp = await _client.GetAsync(url);
128-
var json = JsonNode.Parse(await resp.Content.ReadAsStringAsync());
129-
d.state = json["state"].GetValue<String>();
113+
var resp = await _client.GetAsync(url);
114+
if (resp.IsSuccessStatusCode)
115+
{
116+
try
117+
{
118+
var body = await resp.Content.ReadAsStringAsync();
119+
var json = JsonNode.Parse(body);
120+
d.state = json["state"].GetValue<String>();
121+
d.IsValid = true;
122+
}
123+
catch (HttpRequestException e)
124+
{
125+
d.state = e.Message;
126+
d.IsValid = true;
127+
}
128+
}
129+
else
130+
{
131+
d.state = "Error1";
132+
}
133+
130134
}
131135
catch (Exception e)
132136
{
133-
d.state = "error";
137+
d.state = "Error2";
134138
}
135139
finally
136140
{
137141
d.IsLoading = false;
138-
d.IsValid = true;
139142
this.ActionImageChanged(actionParameter);
140143
}
141144
}
142145
}
143-
144146
}

HomeAssistantPlugin/src/HomeAssistantPlugin/Actions/HomeAssistantTemplateCommand.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Net.Http;
66
using System.Net.Http.Headers;
7-
using System.Text.Json.Nodes;
87
using System.Timers;
98

109
class HomeAssistantTemplateCommand : PluginDynamicCommand
@@ -81,9 +80,9 @@ protected TemplateData GetTemplateData(String actionParameter)
8180
return d;
8281

8382
d = new TemplateData();
84-
templateData[actionParameter] = d;
83+
this.templateData[actionParameter] = d;
8584

86-
LoadData(actionParameter);
85+
this.LoadData(actionParameter);
8786

8887
return d;
8988
}
@@ -126,7 +125,7 @@ protected async void LoadData(String actionParameter)
126125
}
127126
catch (Exception e)
128127
{
129-
d.template = "error";
128+
d.template = "Error";
130129
}
131130
finally
132131
{

HomeAssistantPlugin/src/HomeAssistantPlugin/Actions/HomeAssistantTest.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
namespace Loupedeck.HomeAssistantPlugin.Actions
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net.Http;
6+
using System.Net.Http.Headers;
7+
using System.Text.Json.Nodes;
8+
using System.Timers;
9+
class HomeAssistantStateCommand : PluginDynamicCommand
10+
{
11+
protected HttpClient httpClient = new HttpClient();
12+
protected IDictionary<string, StateData> stateData = new Dictionary<string, StateData>();
13+
protected Timer timer;
14+
15+
protected class StateData
16+
{
17+
public String state;
18+
public Boolean IsValid = false;
19+
public Boolean IsLoading = false;
20+
}
21+
22+
public HomeAssistantStateCommand() : base("Get State", "Get the state of an entity", "")
23+
{
24+
this.MakeProfileAction("text;Enter entity");
25+
26+
// Reload the data periodically every 1 minutes
27+
this.timer = new Timer(60 * 1 * 1000);
28+
this.timer.Elapsed += (Object, ElapsedEventArgs) =>
29+
{
30+
foreach (var actionParameter in new List<String>(this.stateData.Keys))
31+
{
32+
this.LoadData(actionParameter);
33+
}
34+
};
35+
this.timer.AutoReset = true;
36+
this.timer.Enabled = true;
37+
}
38+
39+
protected override void RunCommand(String actionParameter)
40+
{
41+
this.LoadData(actionParameter);
42+
}
43+
44+
protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
45+
{
46+
if (actionParameter == null)
47+
{
48+
return null;
49+
}
50+
51+
StateData s = this.GetStateData(actionParameter);
52+
53+
using (var bitmapBuilder = new BitmapBuilder(imageSize))
54+
{
55+
var fn = EmbeddedResources.FindFile("ButtonBaseHomeAssistant.png");
56+
bitmapBuilder.SetBackgroundImage(EmbeddedResources.ReadImage(fn));
57+
if (this.stateData[actionParameter].IsValid)
58+
{
59+
bitmapBuilder.DrawText(this.stateData[actionParameter].state);
60+
}
61+
else
62+
{
63+
bitmapBuilder.DrawText("Error");
64+
}
65+
return bitmapBuilder.ToImage();
66+
}
67+
}
68+
protected StateData GetStateData(String actionParameter)
69+
{
70+
StateData d;
71+
72+
if (!this.stateData.ContainsKey(actionParameter))
73+
{
74+
d = new StateData();
75+
this.stateData[actionParameter] = d;
76+
} else
77+
{
78+
d = this.stateData[actionParameter];
79+
}
80+
81+
this.LoadData(actionParameter);
82+
83+
return d;
84+
}
85+
protected async void LoadData(String actionParameter)
86+
{
87+
if (actionParameter == null)
88+
{
89+
return;
90+
}
91+
92+
if (this.stateData[actionParameter] == null)
93+
{
94+
this.stateData[actionParameter] = new StateData();
95+
}
96+
97+
StateData d = this.GetStateData(actionParameter);
98+
99+
if (d.IsLoading)
100+
{
101+
return;
102+
}
103+
104+
d.IsLoading = true;
105+
106+
try
107+
{
108+
var _client = new HttpClient();
109+
var url = HomeAssistantPlugin.Config.Url + "states/" + actionParameter;
110+
_client.DefaultRequestHeaders.Authorization =
111+
new AuthenticationHeaderValue("Bearer", HomeAssistantPlugin.Config.Token);
112+
HttpResponseMessage resp = await _client.GetAsync(url);
113+
var json = JsonNode.Parse(await resp.Content.ReadAsStringAsync());
114+
d.state = json["state"].GetValue<String>();
115+
}
116+
catch (Exception e)
117+
{
118+
d.state = "Error";
119+
}
120+
finally
121+
{
122+
d.IsLoading = false;
123+
d.IsValid = true;
124+
this.ActionImageChanged(actionParameter);
125+
}
126+
}
127+
}
128+
129+
}

0 commit comments

Comments
 (0)