diff --git a/Shelly_Parser_MQTT/libs/ShellyParser.php b/Shelly_Parser_MQTT/libs/ShellyParser.php index aed068e..074df79 100644 --- a/Shelly_Parser_MQTT/libs/ShellyParser.php +++ b/Shelly_Parser_MQTT/libs/ShellyParser.php @@ -38,14 +38,29 @@ class ShellyParser 'temperature' => null ]; + // GEN3 / GEN4 Geräte → switch:0, switch:1, ... + foreach ($params as $key => $value) { + if (str_starts_with($key, 'switch:') && is_array($value)) { + + // Output + if (isset($value['output'])) { + $mapped['output'] = (bool)$value['output']; + } + + // Input (für Pro / Gen4 Geräte) + if (isset($value['input'])) { + $mapped['input'] = (bool)$value['input']; + } + } + } + + // Temperatur suchen (beliebig tief) self::ExtractRecursive($params, $mapped); - // null-Werte rauswerfen - return array_filter($mapped, static function ($v) { - return $v !== null; - }); + return array_filter($mapped, fn($v) => $v !== null); } + private static function ExtractRecursive(array $data, array &$mapped): void { foreach ($data as $key => $value) { diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index cdfa4e6..d4986b7 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -152,54 +152,66 @@ class Shelly_Parser_MQTT extends IPSModule /* --------------------------------------------------------- * RPC-EVENTS * ---------------------------------------------------------*/ -private function HandleRPC(string $deviceID, string $payload): void -{ - $json = json_decode($payload, true); - if (!is_array($json)) { - return; - } + private function HandleRPC(string $deviceID, string $payload): void + { + $json = json_decode($payload, true); + if (!is_array($json)) { + return; + } - $src = $json['src'] ?? ''; - if (!is_string($src) || !str_starts_with($src, 'shelly')) { - return; - } + // Shelly prüfen + $src = $json['src'] ?? ''; + if (!str_starts_with($src, 'shelly')) { + return; + } - // Modell Typ extrahieren - $type = ShellyParser::ExtractType($src); - - // Typ Variable - $this->SetValue( - $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'), - $type - ); - - // PARAMETER MAPPEN - $params = $json['params'] ?? []; - $mapped = ShellyParser::MapParams($params); - - // INPUT - if (isset($mapped['input'])) { + // Typ extrahieren + $type = ShellyParser::ExtractType($src); $this->SetValue( - $this->EnsureBooleanVariable($deviceID, $deviceID . '_input', 'Input'), - (bool)$mapped['input'] + $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'), + $type ); + + // Parameter extrahieren + $params = $json['params'] ?? []; + $mapped = ShellyParser::MapParams($params); + + // Output + if (array_key_exists('output', $mapped)) { + $varID = $this->EnsureBooleanVariable( + $deviceID, + $deviceID . '_output', + 'Output' + ); + $this->EnableAction($deviceID . '_output'); + SetValue($varID, $mapped['output']); + } + + // Input (GEN4 / PRO) + if (array_key_exists('input', $mapped)) { + SetValue( + $this->EnsureBooleanVariable( + $deviceID, + $deviceID . '_input', + 'Input' + ), + $mapped['input'] + ); + } + + // Temperatur + if (array_key_exists('temperature', $mapped)) { + SetValue( + $this->EnsureFloatVariable( + $deviceID, + $deviceID . '_temperature', + 'Temperatur' + ), + $mapped['temperature'] + ); + } } - // OUTPUT - if (isset($mapped['output'])) { - $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output'); - $this->EnableAction($deviceID . '_output'); - $this->SetValue($varID, (bool)$mapped['output']); - } - - // TEMPERATURE - if (isset($mapped['temperature'])) { - $this->SetValue( - $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'), - (float)$mapped['temperature'] - ); - } -} /* --------------------------------------------------------- @@ -207,20 +219,17 @@ private function HandleRPC(string $deviceID, string $payload): void * ---------------------------------------------------------*/ private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int { - $folderID = @IPS_GetObjectIDByIdent($folderIdent, $this->InstanceID); + $folderID = $this->GetDeviceFolder($deviceID); - // Immer im Ordner suchen foreach (IPS_GetChildrenIDs($folderID) as $cid) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { return $cid; } } - // Neue Variable anlegen - $varID = $this->RegisterVariableBoolean($ident, $name); - IPS_SetParent($varID, $folderID); - - return $varID; + $id = $this->RegisterVariableBoolean($ident, $name); + IPS_SetParent($id, $folderID); + return $id; } private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int @@ -233,10 +242,9 @@ private function HandleRPC(string $deviceID, string $payload): void } } - $varID = $this->RegisterVariableFloat($ident, $name); - IPS_SetParent($varID, $folderID); - - return $varID; + $id = $this->RegisterVariableFloat($ident, $name); + IPS_SetParent($id, $folderID); + return $id; } private function EnsureStringVariable(string $deviceID, string $ident, string $name): int @@ -249,13 +257,13 @@ private function HandleRPC(string $deviceID, string $payload): void } } - $varID = $this->RegisterVariableString($ident, $name); - IPS_SetParent($varID, $folderID); - - return $varID; + $id = $this->RegisterVariableString($ident, $name); + IPS_SetParent($id, $folderID); + return $id; } + private function GetDeviceFolder(string $deviceID): int { $folderIdent = 'folder_' . $deviceID;