-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
2 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
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; | ||
} | ||
} | ||
} |
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,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}"; | ||
} | ||
} | ||
} |
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