From ef53cc591acd61b42455012a66b20bfe179a5aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=A4fliger?= Date: Fri, 14 Nov 2025 08:59:21 +0100 Subject: [PATCH] no message --- Shelly_Parser_MQTT/module.php | 185 ++++++++++++++++++---------------- 1 file changed, 99 insertions(+), 86 deletions(-) diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index 3d1b293..5d13574 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -73,14 +73,15 @@ class Shelly_Parser_MQTT extends IPSModule * ---------------------------------------------------------*/ public function RequestAction($Ident, $Value) { - // Wert lokal setzen $this->SetValue($Ident, $Value); - // Wir erwarten Ident im Format: "_output" + // Output: _output if (str_ends_with($Ident, '_output')) { + $deviceID = substr($Ident, 0, -strlen('_output')); $topic = $deviceID . '/rpc/Switch.Set'; + $payload = json_encode([ 'id' => 0, 'on' => (bool)$Value @@ -90,6 +91,7 @@ class Shelly_Parser_MQTT extends IPSModule } } + /* --------------------------------------------------------- * RECEIVE MQTT DATA * ---------------------------------------------------------*/ @@ -138,111 +140,122 @@ class Shelly_Parser_MQTT extends IPSModule private function HandleOnline(string $deviceID, string $payload): void { $ident = $deviceID . '_online'; - $name = $deviceID . ' Online'; + $name = "Online"; - $this->EnsureBooleanVariable($ident, $name); - - $value = ($payload === 'true' || $payload === '1'); - $this->SetValue($ident, $value); - - if ($this->ReadPropertyBoolean('Debug')) { - IPS_LogMessage('Shelly_Parser_MQTT', sprintf( - 'Online-Status %s -> %s', - $deviceID, - $value ? 'true' : 'false' - )); - } + $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); + $this->SetValue($ident, $payload === 'true' || $payload === '1'); } + /* --------------------------------------------------------- * RPC-EVENTS * ---------------------------------------------------------*/ - 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')) { - // kein Shelly-Gerät → ignorieren - return; - } - - // Typ/Modell aus src extrahieren - $type = ShellyParser::ExtractType($src); - - // Typ-Variable - $typeIdent = $deviceID . '_type'; - $this->EnsureStringVariable($typeIdent, $deviceID . ' Typ'); - $this->SetValue($typeIdent, $type); - - // params parsen - $params = $json['params'] ?? []; - if (!is_array($params)) { - return; - } - - $mapped = ShellyParser::MapParams($params); - - // input - if (array_key_exists('input', $mapped)) { - $ident = $deviceID . '_input'; - $this->EnsureBooleanVariable($ident, $deviceID . ' Input'); - $this->SetValue($ident, (bool)$mapped['input']); - } - - // output - if (array_key_exists('output', $mapped)) { - $ident = $deviceID . '_output'; - $this->EnsureBooleanVariable($ident, $deviceID . ' Output'); - $this->EnableAction($ident); - $this->SetValue($ident, (bool)$mapped['output']); - } - - // temperature - if (array_key_exists('temperature', $mapped)) { - $ident = $deviceID . '_temperature'; - $this->EnsureFloatVariable($ident, $deviceID . ' Temperatur'); - $this->SetValue($ident, (float)$mapped['temperature']); - } - - if ($this->ReadPropertyBoolean('Debug')) { - IPS_LogMessage('Shelly_Parser_MQTT', sprintf( - 'RPC von %s (Typ %s) → %s', - $deviceID, - $type, - json_encode($mapped) - )); - } +private function HandleRPC(string $deviceID, string $payload): void +{ + $json = json_decode($payload, true); + if (!is_array($json)) { + return; } + $src = $json['src'] ?? ''; + if (!str_starts_with($src, 'shelly')) { + return; // kein Shelly-Gerät + } + + // Typ extrahieren + $type = ShellyParser::ExtractType($src); + $typeIdent = $deviceID . '_type'; + $this->EnsureStringVariable($deviceID, $typeIdent, 'Typ'); + $this->SetValue($typeIdent, $type); + + $params = $json['params'] ?? []; + if (!is_array($params)) { + return; + } + + $mapped = ShellyParser::MapParams($params); + + // INPUT + if (array_key_exists('input', $mapped)) { + $ident = $deviceID . '_input'; + $this->EnsureBooleanVariable($deviceID, $ident, 'Input'); + $this->SetValue($ident, (bool)$mapped['input']); + } + + // OUTPUT + if (array_key_exists('output', $mapped)) { + $ident = $deviceID . '_output'; + $varID = $this->EnsureBooleanVariable($deviceID, $ident, 'Output'); + $this->EnableAction($ident); + $this->SetValue($ident, (bool)$mapped['output']); + } + + // TEMPERATUR + if (array_key_exists('temperature', $mapped)) { + $ident = $deviceID . '_temperature'; + $this->EnsureFloatVariable($deviceID, $ident, 'Temperatur'); + $this->SetValue($ident, (float)$mapped['temperature']); + } +} + + /* --------------------------------------------------------- * Helper für Variablen * ---------------------------------------------------------*/ - private function EnsureBooleanVariable(string $ident, string $name): void + private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int { - $id = @$this->GetIDForIdent($ident); - if ($id === false) { - $this->RegisterVariableBoolean($ident, $name); + $folderID = $this->GetDeviceFolder($deviceID); + + $varID = @$this->GetIDForIdent($ident); + if ($varID === false) { + $varID = $this->RegisterVariableBoolean($ident, $name); + IPS_SetParent($varID, $folderID); } + return $varID; } - private function EnsureFloatVariable(string $ident, string $name): void + private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { - $id = @$this->GetIDForIdent($ident); - if ($id === false) { - $this->RegisterVariableFloat($ident, $name); + $folderID = $this->GetDeviceFolder($deviceID); + + $varID = @$this->GetIDForIdent($ident); + if ($varID === false) { + $varID = $this->RegisterVariableFloat($ident, $name); + IPS_SetParent($varID, $folderID); } + return $varID; } - private function EnsureStringVariable(string $ident, string $name): void + private function EnsureStringVariable(string $deviceID, string $ident, string $name): int { - $id = @$this->GetIDForIdent($ident); - if ($id === false) { - $this->RegisterVariableString($ident, $name); + $folderID = $this->GetDeviceFolder($deviceID); + + $varID = @$this->GetIDForIdent($ident); + if ($varID === false) { + $varID = $this->RegisterVariableString($ident, $name); + IPS_SetParent($varID, $folderID); } + return $varID; } + + private function GetDeviceFolder(string $deviceID): int + { + $folderIdent = 'folder_' . $deviceID; + + $folderID = @$this->GetIDForIdent($folderIdent); + if ($folderID !== false) { + return $folderID; + } + + // Ordner erzeugen + $folderID = IPS_CreateCategory(); + IPS_SetParent($folderID, $this->InstanceID); + IPS_SetIdent($folderID, $folderIdent); + IPS_SetName($folderID, $deviceID); + + return $folderID; + } + + } ?>