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 #2 from lubeda/v0.3
V0.3
- Loading branch information
Showing
7 changed files
with
175 additions
and
131 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
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
86 changes: 0 additions & 86 deletions
86
HomeAssistantPlugin/src/HomeAssistantPlugin/Actions/HomeAssistantTest.cs
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
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,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); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.