no message

This commit is contained in:
dh
2025-11-14 10:56:00 +01:00
parent 25225734f3
commit 10ccfede64
+53 -46
View File
@@ -71,9 +71,9 @@ class Shelly_Parser_MQTT extends IPSModule
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RequestAction → Schalten von Outputs * RequestAction → Schalten von Outputs
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
IPS_LogMessage("ShellyDebug", "RequestAction($Ident → $Value)"); IPS_LogMessage('ShellyDebug', "RequestAction($Ident → " . var_export($Value, true) . ')');
// Prüfen ob es eine Output-Variable ist // Prüfen ob es eine Output-Variable ist
if (str_contains($Ident, '_output_')) { if (str_contains($Ident, '_output_')) {
@@ -85,33 +85,33 @@ public function RequestAction($Ident, $Value)
} }
// device + index extrahieren // device + index extrahieren
list($deviceID, $suffix) = explode('_output_', $Ident); [$deviceID, $suffix] = explode('_output_', $Ident, 2);
$index = intval($suffix); $index = intval($suffix);
// Shelly-RPC Topic (wie im IPS-Shelly Modul) // Shelly-RPC Topic
$topic = $deviceID . '/rpc'; $topic = $deviceID . '/rpc';
// Shelly-RPC-kompatibles JSON // Shelly-RPC-kompatibles JSON
$payload = json_encode([ $payloadArr = [
"id" => 1, 'id' => 1,
"src" => "ips", 'src' => 'ips',
"method" => "Switch.Set", 'method' => 'Switch.Set',
"params" => [ 'params' => [
"id" => $index, 'id' => $index,
"on" => (bool)$Value 'on' => (bool) $Value
] ]
]); ];
$payload = json_encode($payloadArr);
IPS_LogMessage("ShellyDebug", "SEND → $topic : $payload"); IPS_LogMessage('ShellyDebug', "SEND → $topic : $payload");
// absenden // absenden
$this->Publish($topic, $payload); $this->Publish($topic, $payload);
return; return;
} }
throw new Exception("Unknown Ident: " . $Ident); throw new Exception('Unknown Ident: ' . $Ident);
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RECEIVE MQTT DATA * RECEIVE MQTT DATA
@@ -193,54 +193,59 @@ public function RequestAction($Ident, $Value)
// ----------------------------------- // -----------------------------------
// DYNAMISCHE OUTPUTS // DYNAMISCHE OUTPUTS
// ----------------------------------- // -----------------------------------
foreach ($mapped['outputs'] as $index => $value) { if (isset($mapped['outputs']) && is_array($mapped['outputs'])) {
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . "_output_" . $index; $ident = $deviceID . '_output_' . $index;
$name = "Output $index"; $name = 'Output ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
// damit RequestAction überhaupt ausgeführt wird
IPS_SetVariableCustomAction($varID, $this->InstanceID);
SetValue($varID, $value); SetValue($varID, $value);
} }
}
// ----------------------------------- // -----------------------------------
// DYNAMISCHE INPUTS // DYNAMISCHE INPUTS
// ----------------------------------- // -----------------------------------
foreach ($mapped['inputs'] as $index => $value) { if (isset($mapped['inputs']) && is_array($mapped['inputs'])) {
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . "_input_" . $index; $ident = $deviceID . '_input_' . $index;
$name = "Input $index"; $name = 'Input ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name); $varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
SetValue($varID, $value); SetValue($varID, $value);
} }
}
// ----------------------------------- // -----------------------------------
// TEMPERATUR // TEMPERATUR
// ----------------------------------- // -----------------------------------
if ($mapped['temperature'] !== null) { if (array_key_exists('temperature', $mapped) && $mapped['temperature'] !== null) {
$tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'); $tempID = $this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur');
SetValue($tempID, $mapped['temperature']); SetValue($tempID, $mapped['temperature']);
} }
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* Helper für Variablen * Helper für Variablen
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{ {
$folderID = $this->GetDeviceFolder($deviceID); $folderID = $this->GetDeviceFolder($deviceID);
// vorhandene Variable finden // vorhandene Variable finden
foreach (IPS_GetChildrenIDs($folderID) as $cid) { foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident) { if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
// *** geändert: für Outputs immer CustomAction + Profil setzen ***
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($cid, $this->InstanceID);
$varInfo = IPS_GetVariable($cid);
if ($varInfo['VariableProfile'] === '' && $varInfo['VariableCustomProfile'] === '') {
IPS_SetVariableCustomProfile($cid, '~Switch');
}
}
return $cid; return $cid;
} }
} }
@@ -251,10 +256,14 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
IPS_SetName($varID, $name); IPS_SetName($varID, $name);
IPS_SetParent($varID, $folderID); IPS_SetParent($varID, $folderID);
// *** geändert: für Outputs CustomAction + Profil setzen ***
if (str_contains($ident, '_output_')) {
IPS_SetVariableCustomAction($varID, $this->InstanceID);
IPS_SetVariableCustomProfile($varID, '~Switch');
}
return $varID; return $varID;
} }
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
{ {
@@ -324,8 +333,8 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
return 0; return 0;
} }
private function FindVariableByIdent(string $Ident) private function FindVariableByIdent(string $Ident)
{ {
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) { foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) { foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
@@ -334,8 +343,6 @@ private function FindVariableByIdent(string $Ident)
} }
} }
return 0; return 0;
} }
} }
?> ?>