From 10ccfede64a598b328627d94b0c39828e1a0ba23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Fri, 14 Nov 2025 10:56:00 +0100 Subject: [PATCH] no message --- Shelly_Parser_MQTT/module.php | 209 ++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 101 deletions(-) diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 0357593..8da04c2 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -70,49 +70,49 @@ class Shelly_Parser_MQTT extends IPSModule /* --------------------------------------------------------- * RequestAction → Schalten von Outputs - * ---------------------------------------------------------*/ -public function RequestAction($Ident, $Value) -{ - IPS_LogMessage("ShellyDebug", "RequestAction($Ident → $Value)"); + * ---------------------------------------------------------*/ + public function RequestAction($Ident, $Value) + { + IPS_LogMessage('ShellyDebug', "RequestAction($Ident → " . var_export($Value, true) . ')'); - // Prüfen ob es eine Output-Variable ist - if (str_contains($Ident, '_output_')) { + // Prüfen ob es eine Output-Variable ist + if (str_contains($Ident, '_output_')) { - // lokale Variable setzen - $varID = $this->FindVariableByIdent($Ident); - if ($varID) { - SetValue($varID, $Value); + // lokale Variable setzen + $varID = $this->FindVariableByIdent($Ident); + if ($varID) { + SetValue($varID, $Value); + } + + // device + index extrahieren + [$deviceID, $suffix] = explode('_output_', $Ident, 2); + $index = intval($suffix); + + // Shelly-RPC Topic + $topic = $deviceID . '/rpc'; + + // Shelly-RPC-kompatibles JSON + $payloadArr = [ + 'id' => 1, + 'src' => 'ips', + 'method' => 'Switch.Set', + 'params' => [ + 'id' => $index, + 'on' => (bool) $Value + ] + ]; + $payload = json_encode($payloadArr); + + IPS_LogMessage('ShellyDebug', "SEND → $topic : $payload"); + + // absenden + $this->Publish($topic, $payload); + return; } - // device + index extrahieren - list($deviceID, $suffix) = explode('_output_', $Ident); - $index = intval($suffix); - - // Shelly-RPC Topic (wie im IPS-Shelly Modul) - $topic = $deviceID . '/rpc'; - - // Shelly-RPC-kompatibles JSON - $payload = json_encode([ - "id" => 1, - "src" => "ips", - "method" => "Switch.Set", - "params" => [ - "id" => $index, - "on" => (bool)$Value - ] - ]); - - IPS_LogMessage("ShellyDebug", "SEND → $topic : $payload"); - - // absenden - $this->Publish($topic, $payload); - return; + throw new Exception('Unknown Ident: ' . $Ident); } - throw new Exception("Unknown Ident: " . $Ident); -} - - /* --------------------------------------------------------- * RECEIVE MQTT DATA * ---------------------------------------------------------*/ @@ -134,7 +134,7 @@ public function RequestAction($Ident, $Value) return; } - $parts = explode('/', $topic); + $parts = explode('/', $topic); $deviceID = $parts[0] ?? ''; if ($deviceID === '') { @@ -158,18 +158,18 @@ public function RequestAction($Ident, $Value) /* --------------------------------------------------------- * ONLINE-STATUS * ---------------------------------------------------------*/ - private function HandleOnline(string $deviceID, string $payload): void - { - $value = ($payload === 'true' || $payload === '1'); + private function HandleOnline(string $deviceID, string $payload): void + { + $value = ($payload === 'true' || $payload === '1'); - $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'); - SetValue($varID, $value); - } + $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'); + SetValue($varID, $value); + } - /* --------------------------------------------------------- - * RPC-EVENTS - * ---------------------------------------------------------*/ - private function HandleRPC(string $deviceID, string $payload): void + /* --------------------------------------------------------- + * RPC-EVENTS + * ---------------------------------------------------------*/ + private function HandleRPC(string $deviceID, string $payload): void { $json = json_decode($payload, true); if (!is_array($json)) { @@ -182,7 +182,7 @@ public function RequestAction($Ident, $Value) return; } - $type = ShellyParser::ExtractType($src); + $type = ShellyParser::ExtractType($src); $typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'); SetValue($typeID, $type); @@ -193,69 +193,78 @@ public function RequestAction($Ident, $Value) // ----------------------------------- // DYNAMISCHE OUTPUTS // ----------------------------------- -foreach ($mapped['outputs'] as $index => $value) { + if (isset($mapped['outputs']) && is_array($mapped['outputs'])) { + foreach ($mapped['outputs'] as $index => $value) { + $ident = $deviceID . '_output_' . $index; + $name = 'Output ' . $index; - $ident = $deviceID . "_output_" . $index; - $name = "Output $index"; - - $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); - - // damit RequestAction überhaupt ausgeführt wird - IPS_SetVariableCustomAction($varID, $this->InstanceID); - - SetValue($varID, $value); -} + $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); + SetValue($varID, $value); + } + } // ----------------------------------- // DYNAMISCHE INPUTS // ----------------------------------- -foreach ($mapped['inputs'] as $index => $value) { - - $ident = $deviceID . "_input_" . $index; - $name = "Input $index"; - - $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); - - SetValue($varID, $value); -} + if (isset($mapped['inputs']) && is_array($mapped['inputs'])) { + foreach ($mapped['inputs'] as $index => $value) { + $ident = $deviceID . '_input_' . $index; + $name = 'Input ' . $index; + $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); + SetValue($varID, $value); + } + } // ----------------------------------- // TEMPERATUR // ----------------------------------- - if ($mapped['temperature'] !== null) { + if (array_key_exists('temperature', $mapped) && $mapped['temperature'] !== null) { $tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'); SetValue($tempID, $mapped['temperature']); } } - /* --------------------------------------------------------- * Helper für Variablen * ---------------------------------------------------------*/ -private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int -{ - $folderID = $this->GetDeviceFolder($deviceID); + private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int + { + $folderID = $this->GetDeviceFolder($deviceID); - // vorhandene Variable finden - foreach (IPS_GetChildrenIDs($folderID) as $cid) { - $obj = IPS_GetObject($cid); - if ($obj['ObjectIdent'] === $ident) { - return $cid; + // vorhandene Variable finden + foreach (IPS_GetChildrenIDs($folderID) as $cid) { + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { + + // *** geändert: für Outputs immer CustomAction + Profil setzen *** + if (str_contains($ident, '_output_')) { + IPS_SetVariableCustomAction($cid, $this->InstanceID); + $varInfo = IPS_GetVariable($cid); + if ($varInfo['VariableProfile'] === '' && $varInfo['VariableCustomProfile'] === '') { + IPS_SetVariableCustomProfile($cid, '~Switch'); + } + } + + return $cid; + } } + + // neue Variable erzeugen + $varID = IPS_CreateVariable(0); // Boolean + IPS_SetIdent($varID, $ident); + IPS_SetName($varID, $name); + IPS_SetParent($varID, $folderID); + + // *** geändert: für Outputs CustomAction + Profil setzen *** + if (str_contains($ident, '_output_')) { + IPS_SetVariableCustomAction($varID, $this->InstanceID); + IPS_SetVariableCustomProfile($varID, '~Switch'); + } + + return $varID; } - // neue Variable erzeugen - $varID = IPS_CreateVariable(0); // Boolean - IPS_SetIdent($varID, $ident); - IPS_SetName($varID, $name); - IPS_SetParent($varID, $folderID); - - return $varID; -} - - - private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); @@ -312,7 +321,7 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $ return $folderID; } - private function FindDeviceFolder(string $Ident): int + private function FindDeviceFolder(string $Ident): int { foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { foreach (IPS_GetChildrenIDs($folder) as $cid) { @@ -324,18 +333,16 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $ return 0; } -private function FindVariableByIdent(string $Ident) -{ - foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { - foreach (IPS_GetChildrenIDs($folder) as $cid) { - if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { - return $cid; + private function FindVariableByIdent(string $Ident) + { + foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { + foreach (IPS_GetChildrenIDs($folder) as $cid) { + if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { + return $cid; + } } } + return 0; } - return 0; -} - - } ?>