ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); // Auf alle Topics lauschen, Filter machen wir selbst $this->Subscribe('#'); // Debug-Property $this->RegisterPropertyBoolean('Debug', false); } public function ApplyChanges() { parent::ApplyChanges(); $this->ConnectParent('{C6D2AEB3-6E1F-4B2E-8E69-3A1A00246850}'); $this->Subscribe('#'); } /* --------------------------------------------------------- * MQTT SUBSCRIBE * ---------------------------------------------------------*/ private function Subscribe(string $topic): void { $packet = [ 'PacketType' => 8, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => '' ]; $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); } /* --------------------------------------------------------- * MQTT PUBLISH * ---------------------------------------------------------*/ private function Publish(string $topic, string $payload): void { $packet = [ 'PacketType' => 3, 'QualityOfService' => 0, 'Retain' => false, 'Topic' => $topic, 'Payload' => $payload ]; $this->SendDataToParent(json_encode([ 'DataID' => '{043EA491-0325-4ADD-8FC2-A30C8EEB4D3F}' ] + $packet)); if ($this->ReadPropertyBoolean('Debug')) { IPS_LogMessage('Shelly_Parser_MQTT', 'Publish: ' . $topic . ' -> ' . $payload); } } /* --------------------------------------------------------- * RequestAction → Schalten von Outputs * ---------------------------------------------------------*/ 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_')) { // 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; } throw new Exception("Unknown Ident: " . $Ident); } /* --------------------------------------------------------- * RECEIVE MQTT DATA * ---------------------------------------------------------*/ public function ReceiveData($JSONString) { if ($this->ReadPropertyBoolean('Debug')) { IPS_LogMessage('Shelly_Parser_MQTT', 'ReceiveData: ' . $JSONString); } $data = json_decode($JSONString, true); if (!is_array($data)) { return; } $topic = $data['Topic'] ?? ''; $payload = $data['Payload'] ?? ''; if ($topic === '') { return; } $parts = explode('/', $topic); $deviceID = $parts[0] ?? ''; if ($deviceID === '') { return; } // /online if ((isset($parts[1])) && ($parts[1] === 'online')) { $this->HandleOnline($deviceID, $payload); return; } // /events/rpc if ((isset($parts[1]) && $parts[1] === 'events') && (isset($parts[2]) && $parts[2] === 'rpc')) { $this->HandleRPC($deviceID, $payload); return; } } /* --------------------------------------------------------- * ONLINE-STATUS * ---------------------------------------------------------*/ private function HandleOnline(string $deviceID, string $payload): void { $value = ($payload === 'true' || $payload === '1'); $varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'); SetValue($varID, $value); } /* --------------------------------------------------------- * RPC-EVENTS * ---------------------------------------------------------*/ private function HandleRPC(string $deviceID, string $payload): void { $json = json_decode($payload, true); if (!is_array($json)) { return; } // Gerätetyp setzen $src = $json['src'] ?? ''; if (!str_starts_with($src, 'shelly')) { return; } $type = ShellyParser::ExtractType($src); $typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'); SetValue($typeID, $type); // Mappen $params = $json['params'] ?? []; $mapped = ShellyParser::MapParams($params); // ----------------------------------- // DYNAMISCHE OUTPUTS // ----------------------------------- foreach ($mapped['outputs'] as $index => $value) { $ident = $deviceID . "_output_" . $index; $name = "Output $index"; $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); // Für Schalten in RequestAction IPS_SetVariableCustomAction($varID, $this->InstanceID); 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); } // ----------------------------------- // TEMPERATUR // ----------------------------------- if ($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); // Suche richtige Variable im Ordner foreach (IPS_GetChildrenIDs($folderID) as $cid) { $obj = IPS_GetObject($cid); if ($obj['ObjectIdent'] === $ident) { return $cid; // bestehende Variable gefunden } } // Neue Variable anlegen $varID = IPS_CreateVariable(0); // 0 = Boolean IPS_SetName($varID, $name); IPS_SetIdent($varID, $ident); IPS_SetParent($varID, $folderID); return $varID; } private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { $obj = IPS_GetObject($cid); if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { return $cid; } } $id = $this->RegisterVariableFloat($ident, $name); IPS_SetParent($id, $folderID); return $id; } private function EnsureStringVariable(string $deviceID, string $ident, string $name): int { $folderID = $this->GetDeviceFolder($deviceID); foreach (IPS_GetChildrenIDs($folderID) as $cid) { $obj = IPS_GetObject($cid); if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { return $cid; } } $id = $this->RegisterVariableString($ident, $name); IPS_SetParent($id, $folderID); return $id; } /* --------------------------------------------------------- * Geräte-Ordner * ---------------------------------------------------------*/ private function GetDeviceFolder(string $deviceID): int { $folderIdent = 'folder_' . $deviceID; // nur unter dieser Instanz suchen foreach (IPS_GetChildrenIDs($this->InstanceID) as $cid) { $obj = IPS_GetObject($cid); if ($obj['ObjectIdent'] === $folderIdent && $obj['ObjectType'] === OBJECTTYPE_CATEGORY) { return $cid; } } // neu anlegen $folderID = IPS_CreateCategory(); IPS_SetParent($folderID, $this->InstanceID); IPS_SetName($folderID, $deviceID); IPS_SetIdent($folderID, $folderIdent); return $folderID; } private function FindDeviceFolder(string $Ident): int { foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { foreach (IPS_GetChildrenIDs($folder) as $cid) { if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { return $folder; } } } 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; } } } return 0; } } ?>