Skip to content

Commit

Permalink
add script support, closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
schmic committed Feb 1, 2024
1 parent f0c54d6 commit 8f79228
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
69 changes: 69 additions & 0 deletions HaPlugin/Commands/ScriptAdvancedCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace Loupedeck.HomeAssistant.Commands
{
using System;

using Newtonsoft.Json.Linq;

class ScriptAdvancedCommand : ActionEditorCommand
{
public ScriptAdvancedCommand()
{
this.Name = "ScriptAdvancedCommand";
this.DisplayName = "Execute Script with parameters";

this.ActionEditor.AddControlEx(
new ActionEditorTextbox("script", "Script").SetPlaceholder("Name of script").SetRequired()
);
this.ActionEditor.AddControlEx(
new ActionEditorTextbox("data", "Data", "Must be valid JSON").SetPlaceholder("{ \"message\": \"foobar\" }")
);
}

protected HaPlugin GetPlugin() => (HaPlugin)base.Plugin;

protected override String GetCommandDisplayName(ActionEditorActionParameters actionParameters)
{
var scriptName = actionParameters.GetString("script");

PluginLog.Info(scriptName);
return base.GetCommandDisplayName(actionParameters);
}

//protected override BitmapImage GetCommandImage(ActionEditorActionParameters actionParameters, Int32 imageWidth, Int32 imageHeight) => base.GetCommandImage(actionParameters, imageWidth, imageHeight);

protected override Boolean RunCommand(ActionEditorActionParameters actionParameters)
{
var scriptName = actionParameters.GetString("script");

if (scriptName.IsNullOrEmpty())
{
return false;
}

var scriptData = actionParameters.GetString("data");
PluginLog.Info($"Executing script: {scriptName} with \"{scriptData}\"");

JObject scriptJson = null;
if (!scriptData.IsNullOrEmpty())
{
scriptJson = JObject.Parse(scriptData);
}


var data = new JObject {
{ "domain", "script" },
{ "service", "turn_on" },
{ "target", new JObject { { "entity_id", $"script.{scriptName}"} } }
};

if (scriptJson != null)
{
data["service_data"] = new JObject { { "variables", scriptJson} };
}

this.GetPlugin().CallService(data);

return true;
}
}
}
36 changes: 36 additions & 0 deletions HaPlugin/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Loupedeck.HomeAssistant.Commands
{
using System;
using Newtonsoft.Json.Linq;

public class ScriptCommand : BaseCommand
{
public ScriptCommand() : base("Script") { }

protected override Boolean EntitiyFilter(String entity_id)
=> entity_id.StartsWith("script.");

protected override void RunCommand(String entity_id)
{
var data = new JObject {
{ "domain", "script" },
{ "service", "turn_on" },
{ "target", new JObject { { "entity_id", entity_id } } }
};

this.GetPlugin().CallService(data);
}

protected override String GetCommandDisplayName(String entity_id, PluginImageSize imageSize)
{
if (entity_id.IsNullOrEmpty())
{ return base.GetCommandDisplayName(entity_id, imageSize); }

var states = this.GetStates();

var FriendlyName = states[entity_id].FriendlyName;

return $"{FriendlyName}";
}
}
}
7 changes: 5 additions & 2 deletions HaPlugin/HaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ private void OnMessageHdl(Object sender, MessageEventArgs e)

public void CallService(JObject data)
{
data.Add("type", "call_service");
PluginLog.Verbose($"CallService: [domain: {data["domain"]}] [service: {data["service"]}] [entity_id: {data["target"]["entity_id"]}] [service_data: {data["service_data"]}]");
if (!data.ContainsKey("type"))
{
data.Add("type", "call_service");
}
PluginLog.Verbose(data.ToString());
this.Send(data);
}

Expand Down
2 changes: 2 additions & 0 deletions HaPlugin/HaPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
<Compile Include="Adjustments\CoverAdjustment.cs" />
<Compile Include="Adjustments\ClimateAdjustment.cs" />
<Compile Include="Adjustments\DimmerAdjustment.cs" />
<Compile Include="Commands\ScriptCommand.cs" />
<Compile Include="Commands\ScriptAdvancedCommand.cs" />
<Compile Include="Commands\ButtonCommand.cs" />
<Compile Include="Commands\LockCommand.cs" />
<Compile Include="Commands\SceneCommand.cs" />
Expand Down

0 comments on commit 8f79228

Please sign in to comment.