diff --git a/Shelly_Parser_MQTT/module.php b/Shelly_Parser_MQTT/module.php index ce1a109..c1584de 100644 --- a/Shelly_Parser_MQTT/module.php +++ b/Shelly_Parser_MQTT/module.php @@ -99,17 +99,20 @@ class Shelly_Parser_MQTT extends IPSModule * ---------------------------------------------------------*/ public function RequestAction($Ident, $Value) { - $this->Log('RequestAction', "$Ident → $Value"); + $this->Log('RequestAction', "$Ident → " . var_export($Value, true)); + // Nur Outputs sind schaltbar if (!str_contains($Ident, '_output_')) { throw new Exception("Unknown Ident: $Ident"); } + // Lokale Variable aktualisieren $varID = $this->FindVariableByIdent($Ident); if ($varID > 0) { SetValue($varID, $Value); } + // deviceID und Index extrahieren [$deviceID, $suffix] = explode('_output_', $Ident); $index = intval($suffix); @@ -138,19 +141,25 @@ class Shelly_Parser_MQTT extends IPSModule $this->EnsureSubscribe(); $data = json_decode($JSONString, true); - if (!is_array($data)) return; + if (!is_array($data)) { + return; + } - $topic = $data['Topic'] ?? ''; + $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; - if ($topic === '') return; + if ($topic === '') { + return; + } $this->Log('ReceiveTopic', "$topic → $payload"); - $parts = explode('/', $topic); + $parts = explode('/', $topic); $deviceID = $parts[0] ?? ''; - if ($deviceID === '') return; + if ($deviceID === '') { + return; + } // Online if (($parts[1] ?? '') === 'online') { @@ -182,13 +191,17 @@ class Shelly_Parser_MQTT extends IPSModule private function HandleRPC(string $deviceID, string $payload) { $json = json_decode($payload, true); - if (!is_array($json)) return; + if (!is_array($json)) { + return; + } $src = $json['src'] ?? ''; - if (!str_starts_with($src, 'shelly')) return; + if (!str_starts_with($src, 'shelly')) { + return; + } // Typ - $type = ShellyParser::ExtractType($src); + $type = ShellyParser::ExtractType($src); $typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'); SetValue($typeID, $type); @@ -220,34 +233,36 @@ class Shelly_Parser_MQTT extends IPSModule private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int { $folder = $this->GetDeviceFolder($deviceID); + $vid = 0; + // Gibt es die Variable schon? foreach (IPS_GetChildrenIDs($folder) as $cid) { - if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { - - // ---- Action wieder setzen, falls sie fehlt ---- - if (str_contains($ident, '_output_')) { - IPS_SetVariableCustomAction($cid, $this->InstanceID); - } - - return $cid; + $obj = IPS_GetObject($cid); + if ($obj['ObjectIdent'] === $ident) { + $vid = $cid; + break; } } - // Variable neu anlegen - $vid = IPS_CreateVariable(0); - IPS_SetName($vid, $name); - IPS_SetIdent($vid, $ident); - IPS_SetParent($vid, $folder); + // Neu anlegen, falls nicht vorhanden + if ($vid === 0) { + $vid = IPS_CreateVariable(0); // 0 = Boolean + IPS_SetName($vid, $name); + IPS_SetIdent($vid, $ident); + IPS_SetParent($vid, $folder); + } - // ---- HIER: Action für Output-Variablen setzen ---- + // ---- HIER: nur für Outputs Action + Profil setzen ---- if (str_contains($ident, '_output_')) { + // Schaltprofil + IPS_SetVariableCustomProfile($vid, '~Switch'); + // Aktion geht direkt an dieses Modul (RequestAction) IPS_SetVariableCustomAction($vid, $this->InstanceID); } return $vid; } - private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folder = $this->GetDeviceFolder($deviceID);