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

V0.4 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Loupedeck.HomeAssistantPlugin
{
using System;
using System.Text.Json.Nodes;

class HomeAssistantImageCommand : PluginDynamicCommand
{
protected class Template
{
public String template { get; set; }
}

public HomeAssistantImageCommand()
{
this.AddParameter("3zeilig", "Text in drei zeilen", "Multiline");
this.AddParameter("2zeilig", "Text in zwei Zeilen", "Multiline");
this.AddParameter("1big", "Eine große Zahl", "Singleline");
this.AddParameter("1norm", "Text in einer zeilen", "Singleline");
this.AddParameter("gauge", "Zeige Gauge an der Seite", "Graphic");
this.AddParameter("circle", "virteilkreis unten rechts", "Graphic");
this.AddParameter("dreieck", "Dreieck oben rechts", "Graphic");
}

protected override void RunCommand(String actionParameter)
{
}

protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
{
if (actionParameter == null)
{
return null;
}

using (var bitmapBuilder = new BitmapBuilder(imageSize))
{
var fn = EmbeddedResources.FindFile("ButtonBaseHomeAssistant.png");
bitmapBuilder.SetBackgroundImage(EmbeddedResources.ReadImage(fn));

switch (actionParameter)
{
case "3zeilig":
bitmapBuilder.DrawText("Line 2xxx", 3, 30, 75, 15);
bitmapBuilder.DrawText("Line 1xx", 3, 13, 75, 15);
bitmapBuilder.DrawText("Line 3xxxx", 3, 47, 75, 15);
break;
case "2zeilig":
bitmapBuilder.DrawText("Line 1big", 3, 25, 75, 15, new BitmapColor(230, 230, 230), 20);
bitmapBuilder.DrawText("Line 2norm", 3, 47, 75, 15);
break;
case "1big":
bitmapBuilder.DrawText("BIG Line", 3, 31, 75, 15, new BitmapColor(220, 220, 220), 22);
break;
case "1norm":
bitmapBuilder.DrawText("Line 1norm", 3, 30, 75, 15);
break;
case "gauge":
bitmapBuilder.FillRectangle(70, 30, 15, 50, new BitmapColor(20, 240, 20));
break;
case "circle":
bitmapBuilder.FillCircle(75, 75, 15, new BitmapColor(240, 20, 20));
break;
case "dreieck":
bitmapBuilder.DrawLine(55, 0, 90, 25, new BitmapColor(240, 240, 20), 15);
break;
default:
break;
};

return bitmapBuilder.ToImage();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text.Json.Nodes;
using Newtonsoft.Json;
using System.Timers;
using System.Collections;

class HomeAssistantStateCommand : PluginDynamicCommand
{
Expand All @@ -17,10 +17,14 @@ class HomeAssistantStateCommand : PluginDynamicCommand
protected class StateData
{
public String state;
public String entity_id;
public Hashtable attributes;
public Hashtable context;
public DateTime last_changed;
public DateTime last_updated;
public Boolean IsValid = false;
public Boolean IsLoading = false;
}

public HomeAssistantStateCommand() : base("Get a state", "Get the state value of an entity.", "")
{
this.MakeProfileAction("text;Enter entity");
Expand All @@ -41,14 +45,17 @@ public HomeAssistantStateCommand() : base("Get a state", "Get the state value of
protected override void RunCommand(String actionParameter)
{
this.LoadData(actionParameter);
this.ActionImageChanged(actionParameter);
}

protected override BitmapImage GetCommandImage(String actionParameter, PluginImageSize imageSize)
{
if (actionParameter == null)
/*
* if (actionParameter == null)
{
return null;
}
*/

StateData s = this.GetStateData(actionParameter);

Expand Down Expand Up @@ -116,9 +123,15 @@ protected async void LoadData(String actionParameter)
try
{
var body = await resp.Content.ReadAsStringAsync();
var json = JsonNode.Parse(body);
d.state = json["state"].GetValue<String>();
d.IsValid = true;
StateData json = JsonConvert.DeserializeObject<StateData>(body);

if (json.state != null)
{
d.state = json.state;
d.IsValid = true;
}


}
catch (HttpRequestException e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Loupedeck.HomeAssistantPlugin
{
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -12,6 +13,10 @@ class HomeAssistantTemplateCommand : PluginDynamicCommand
protected IDictionary<string, TemplateData> templateData = new Dictionary<string, TemplateData>();
protected Timer timer;

protected class TemplateObject
{
public String template;
}
protected class TemplateData
{
public String template;
Expand Down Expand Up @@ -115,7 +120,12 @@ protected async void LoadData(String actionParameter)
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 t = new TemplateObject () { template = actionParameter};
var strJson = JsonConvert.SerializeObject(t);

var content = new StringContent(strJson, 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(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\..\..\..\Program Files (x86)\Loupedeck\Loupedeck2\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PluginApi, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath Condition="$(OS) == 'Windows_NT'">C:\Program Files (x86)\Loupedeck\Loupedeck2\PluginApi.dll</HintPath>
Expand Down Expand Up @@ -85,6 +89,7 @@
<Compile Include="Actions\HomeAssistantTemplateCommand.cs" />
<None Include="Actions\_HomeAssistantStateCommand.cs" />
<Compile Include="Actions\HomeAssistantServiceComand.cs" />
<None Include="Actions\HomeAssistantImageCommand.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="HomeAssistantApplication.cs" />
<Compile Include="HomeAssistantPlugin.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ displayName: HomeAssistant
pluginFileName: HomeAssistantPlugin.dll

# Plugin version.
version: 0.3
version: 0.4

# Author of the plugin. The author can be a company or an individual developer.
author: lubeda
Expand Down