This commit is contained in:
mh
2025-05-26 09:48:28 +02:00
parent c9b6584e86
commit 819f5f9587
+37 -29
View File
@@ -2,7 +2,7 @@
class Symcon_Publish_to_Shelly_MQTT extends IPSModule class Symcon_Publish_to_Shelly_MQTT extends IPSModule
{ {
public function Create() public function Create()
{ {
parent::Create(); parent::Create();
@@ -14,12 +14,10 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
$this->RegisterPropertyString("Topic", ""); $this->RegisterPropertyString("Topic", "");
// Nachricht-Payload-Parameter // Nachricht-Payload-Parameter
$this->RegisterPropertyInteger("msg_id", 1); $this->RegisterPropertyInteger("msg_id", 100);
$this->RegisterPropertyString("src", "user1"); $this->RegisterPropertyString("src", "user1");
$this->RegisterPropertyString("method", "Switch.Set"); $this->RegisterPropertyString("method", "Switch.Set");
$this->RegisterPropertyInteger("switch_bool", 0); $this->RegisterPropertyInteger("switch_bool", 0); // Variable-ID einer Bool-Variable
} }
public function ApplyChanges() public function ApplyChanges()
@@ -29,34 +27,43 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
IPS_LogMessage("ShellySwitchSender", " RequestAction: $Ident = $Value"); IPS_LogMessage("ShellySwitchSender", "⚙️ RequestAction: $Ident = $Value");
switch ($Ident) { switch ($Ident) {
case "GetAction": case "GetAction":
$this->GetAction(); $this->GetAction();
break; break;
default: default:
throw new Exception("Invalid action"); throw new Exception("Invalid action: $Ident");
} }
} }
private function GetAction() private function GetAction()
{ {
IPS_LogMessage("ShellySwitchSender", " Starte GetAction()..."); IPS_LogMessage("ShellySwitchSender", " Starte GetAction()...");
// Eigenschaften lesen // Eigenschaften lesen
$broker = GetValue($this->ReadPropertyString("broker_address")); $broker = $this->ReadPropertyString("broker_address");
$port = GetValue($this->ReadPropertyInteger("broker_port")); $port = $this->ReadPropertyInteger("broker_port");
$user = GetValue($this->ReadPropertyString("username")); $user = $this->ReadPropertyString("username");
$pass = GetValue($this->ReadPropertyString("password")); $pass = $this->ReadPropertyString("password");
$topic = GetValue($this->ReadPropertyString("Topic")); $topic = $this->ReadPropertyString("Topic");
$msg_id = GetValue($this->ReadPropertyInteger("msg_id")); $msg_id = $this->ReadPropertyInteger("msg_id");
$src = GetValue($this->ReadPropertyString("src")); $src = $this->ReadPropertyString("src");
$method = GetValue($this->ReadPropertyString("method")); $method = $this->ReadPropertyString("method");
$switch_bool = GetValueBoolean($this->ReadPropertyInteger("switch_bool")); $boolVarID = $this->ReadPropertyInteger("switch_bool");
if (!IPS_VariableExists($boolVarID)) {
IPS_LogMessage("ShellySwitchSender", " Fehler: Bool-Variable mit ID $boolVarID existiert nicht.");
return;
}
$onValue = GetValueBoolean($boolVarID);
IPS_LogMessage("ShellySwitchSender", " MQTT-Ziel: $broker:$port");
IPS_LogMessage("ShellySwitchSender", " topic: $topic, src: $src, method: $method, msg_id: $msg_id");
IPS_LogMessage("ShellySwitchSender", " Bool-Wert: " . ($onValue ? "true" : "false") . " (aus Variable $boolVarID)");
IPS_LogMessage("ShellySwitchSender", "Broker: $broker:$port, Topic: $topic");
IPS_LogMessage("ShellySwitchSender", " src: $src, method: $method, msg_id: $msg_id");
IPS_LogMessage("ShellySwitchSender", " boolVarID: $switch_bool");
// JSON-Payload erstellen // JSON-Payload erstellen
$payload = [ $payload = [
"id" => 0, "id" => 0,
@@ -64,30 +71,31 @@ class Symcon_Publish_to_Shelly_MQTT extends IPSModule
"method" => $method, "method" => $method,
"params" => [ "params" => [
"id" => $msg_id, "id" => $msg_id,
"on" => $switch_bool "on" => $onValue
] ]
]; ];
$json = json_encode($payload); $json = json_encode($payload);
IPS_LogMessage("ShellySwitchSender", "📦 MQTT Payload: $json");
IPS_LogMessage("ShellySwitchSender", " MQTT Payload: $json");
// Sende an MQTTClient // Sende an MQTTClient
$clientID = @IPS_GetInstanceIDByName("MQTTClient", 0); $clientID = @IPS_GetInstanceIDByName("MQTTClient", 0);
if ($clientID && IPS_InstanceExists($clientID)) { if ($clientID && IPS_InstanceExists($clientID)) {
$moduleID = IPS_GetInstance($clientID)['ModuleInfo']['ModuleID']; $moduleID = IPS_GetInstance($clientID)['ModuleInfo']['ModuleID'];
IPS_LogMessage("ShellySwitchSender", "📡 Gefundener MQTTClient mit ModuleID: $moduleID"); IPS_LogMessage("ShellySwitchSender", "MQTTClient gefunden (ID: $clientID, ModuleID: $moduleID)");
if ($moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}") { // Unterstützte Module
if (
$moduleID == "{C03937E3-5F86-EACA-B4A2-39A24ACF945A}" || // alter MQTTClient
$moduleID == "{C8792760-65CF-4C53-B5C7-A30FCC84FEFE}" // offizieller Symcon MQTTClient
) {
MQTTClient_Publish($clientID, $topic, $json, 0, false); MQTTClient_Publish($clientID, $topic, $json, 0, false);
IPS_LogMessage("ShellySwitchSender", " Nachricht erfolgreich an '$topic' gesendet."); IPS_LogMessage("ShellySwitchSender", " Nachricht an Topic '$topic' gesendet.");
} else { } else {
IPS_LogMessage("ShellySwitchSender", " Fehler: Instanz 'MQTTClient' hat falsches Modul."); IPS_LogMessage("ShellySwitchSender", " Fehler: 'MQTTClient' Instanz ist kein gültiges MQTT-Modul.");
} }
} else { } else {
IPS_LogMessage("ShellySwitchSender", " Kein MQTTClient gefunden oder benannt als 'MQTTClient'."); IPS_LogMessage("ShellySwitchSender", " Fehler: MQTTClient nicht gefunden. Bitte sicherstellen, dass eine Instanz mit Namen 'MQTTClient' existiert.");
} }
} }
} }
?>