From 25225734f345378c6cf44c2bdd837d3cdadbe4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Fri, 14 Nov 2025 10:45:23 +0100 Subject: [PATCH] no message --- Shelly_Parser_MQTT/libs/ShellyParser.php | 14 +- Shelly_Parser_MQTT/module.php | 155 ++++++++++++----------- 2 files changed, 83 insertions(+), 86 deletions(-) diff --git a/Shelly_Parser_MQTT/libs/ShellyParser.php b/Shelly_Parser_MQTT/libs/ShellyParser.php index fef133e..955f582 100644 --- a/Shelly_Parser_MQTT/libs/ShellyParser.php +++ b/Shelly_Parser_MQTT/libs/ShellyParser.php @@ -30,7 +30,7 @@ class ShellyParser * - output (bool) * - temperature (float, inkl. tC) */ - public static function MapParams(array $params): array + public static function MapParams(array $params): array { $mapped = [ 'outputs' => [], // switch:x → output @@ -40,9 +40,7 @@ class ShellyParser foreach ($params as $key => $value) { - // ----------------------------- - // OUTPUTS (switch:0 → output) - // ----------------------------- + // OUTPUTS (switch:0.output) if (str_starts_with($key, 'switch:') && is_array($value)) { $index = (int)substr($key, 7); @@ -51,15 +49,13 @@ class ShellyParser $mapped['outputs'][$index] = (bool)$value['output']; } - // Gen4 Input in switch + // Gen4 / Pro input in switch if (isset($value['input'])) { $mapped['inputs'][$index] = (bool)$value['input']; } } - // ----------------------------- - // INPUTS (input:0 → state) - // ----------------------------- + // INPUTS (input:0.state) if (str_starts_with($key, 'input:') && is_array($value)) { $index = (int)substr($key, 6); @@ -70,7 +66,7 @@ class ShellyParser } } - // Temperatur (verschachtelt) + // Temperatur tief suchen self::ExtractRecursive($params, $mapped); return $mapped; diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 339bc49..0357593 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -70,50 +70,48 @@ 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 → $Value)"); - // Dynamischer Output? z.B. BE_1_3_14_output_0 - if (str_contains($Ident, '_output_')) { + // Prüfen ob es eine Output-Variable ist + if (str_contains($Ident, '_output_')) { - // 1) Variable im Geräte-Ordner finden & setzen - $varID = $this->FindVariableByIdent($Ident); - if ($varID) { - SetValue($varID, $Value); - } - - // 2) Device + Switch-Index extrahieren - $parts = explode('_output_', $Ident); - $deviceID = $parts[0]; - $index = intval($parts[1]); - - // 3) Shelly-konformes Topic - $topic = $deviceID . '/rpc'; - - // 4) Shelly-konformes Payload gemäß API-Dokumentation - $payload = json_encode([ - "id" => 1, - "src" => "ips", - "method" => "Switch.Set", - "params" => [ - "id" => $index, - "on" => (bool)$Value - ] - ]); - - IPS_LogMessage("ShellyDebug", "SEND → $topic : $payload"); - - // 5) Absenden - $this->Publish($topic, $payload); - - return; + // lokale Variable setzen + $varID = $this->FindVariableByIdent($Ident); + if ($varID) { + SetValue($varID, $Value); } - throw new Exception("Unknown Ident: " . $Ident); + // 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); +} + /* --------------------------------------------------------- * RECEIVE MQTT DATA @@ -195,31 +193,32 @@ class Shelly_Parser_MQTT extends IPSModule // ----------------------------------- // DYNAMISCHE OUTPUTS // ----------------------------------- - foreach ($mapped['outputs'] as $index => $value) { +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); + $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); - // Für Schalten in RequestAction - IPS_SetVariableCustomAction($varID, $this->InstanceID); + // damit RequestAction überhaupt ausgeführt wird + IPS_SetVariableCustomAction($varID, $this->InstanceID); - SetValue($varID, $value); - } + SetValue($varID, $value); +} // ----------------------------------- // DYNAMISCHE INPUTS // ----------------------------------- - foreach ($mapped['inputs'] as $index => $value) { +foreach ($mapped['inputs'] as $index => $value) { - $ident = $deviceID . "_input_" . $index; - $name = "Input $index"; + $ident = $deviceID . "_input_" . $index; + $name = "Input $index"; - $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); + $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); + + SetValue($varID, $value); +} - SetValue($varID, $value); - } // ----------------------------------- // TEMPERATUR @@ -234,27 +233,28 @@ class Shelly_Parser_MQTT extends IPSModule /* --------------------------------------------------------- * 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); - // Suche richtige Variable im Ordner - foreach (IPS_GetChildrenIDs($folderID) as $cid) { - $obj = IPS_GetObject($cid); - if ($obj['ObjectIdent'] === $ident) { - return $cid; // bestehende Variable gefunden - } + // vorhandene Variable finden + foreach (IPS_GetChildrenIDs($folderID) as $cid) { + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $ident) { + return $cid; } - - // Neue Variable anlegen - $varID = IPS_CreateVariable(0); // 0 = Boolean - IPS_SetName($varID, $name); - IPS_SetIdent($varID, $ident); - IPS_SetParent($varID, $folderID); - - 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 { @@ -324,17 +324,18 @@ class Shelly_Parser_MQTT extends IPSModule 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; +} + } ?>