no message

This commit is contained in:
dh
2025-11-14 10:01:17 +01:00
parent 1e23cf94c6
commit 7de43a4240
+40 -17
View File
@@ -73,25 +73,45 @@ class Shelly_Parser_MQTT extends IPSModule
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
public function RequestAction($Ident, $Value) public function RequestAction($Ident, $Value)
{ {
// Ident ist z.B. "BE_1_3_14_output" // Lokale Variable aktualisieren
$varID = @IPS_GetObjectIDByIdent($Ident, $this->InstanceID);
if (!$varID) {
// Falls Variable im Ordner liegt:
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
$varID = $cid;
break 2;
}
}
}
}
if ($varID) {
SetValue($varID, $Value);
}
// Prüfen ob das ein Output ist
if (str_ends_with($Ident, '_output')) { if (str_ends_with($Ident, '_output')) {
$deviceID = substr($Ident, 0, -strlen('_output')); $deviceID = substr($Ident, 0, -strlen('_output'));
// lokale Variable aktualisieren
$varID = $this->EnsureBooleanVariable($deviceID, $Ident, 'Output');
SetValue($varID, (bool)$Value);
// Shelly schalten
$topic = $deviceID . '/rpc/Switch.Set'; $topic = $deviceID . '/rpc/Switch.Set';
$payload = json_encode([ $payload = json_encode([
'id' => 0, 'id' => 0,
'on' => (bool)$Value 'on' => (bool)$Value
]); ]);
$this->Publish($topic, $payload); $this->Publish($topic, $payload);
if ($this->ReadPropertyBoolean('Debug')) {
IPS_LogMessage("Shelly_Parser_MQTT", "Switching $deviceID -> $Value");
}
} }
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RECEIVE MQTT DATA * RECEIVE MQTT DATA
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
@@ -176,15 +196,12 @@ class Shelly_Parser_MQTT extends IPSModule
// Output // Output
if (array_key_exists('output', $mapped)) { if (array_key_exists('output', $mapped)) {
$outID = $this->EnsureBooleanVariable( $outID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output');
$deviceID, IPS_SetVariableCustomAction($outID, $this->InstanceID);
$deviceID . '_output',
'Output'
);
$this->EnableAction($deviceID . '_output');
SetValue($outID, (bool)$mapped['output']); SetValue($outID, (bool)$mapped['output']);
} }
// Input (GEN4 / PRO) // Input (GEN4 / PRO)
if (array_key_exists('input', $mapped)) { if (array_key_exists('input', $mapped)) {
$inID = $this->EnsureBooleanVariable( $inID = $this->EnsureBooleanVariable(
@@ -213,18 +230,24 @@ class Shelly_Parser_MQTT extends IPSModule
{ {
$folderID = $this->GetDeviceFolder($deviceID); $folderID = $this->GetDeviceFolder($deviceID);
// Suche richtige Variable im Ordner
foreach (IPS_GetChildrenIDs($folderID) as $cid) { foreach (IPS_GetChildrenIDs($folderID) as $cid) {
$obj = IPS_GetObject($cid); $obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) { if ($obj['ObjectIdent'] === $ident) {
return $cid; return $cid; // bestehende Variable gefunden
} }
} }
$id = $this->RegisterVariableBoolean($ident, $name); // Neue Variable anlegen
IPS_SetParent($id, $folderID); $varID = IPS_CreateVariable(0); // 0 = Boolean
return $id; IPS_SetName($varID, $name);
IPS_SetIdent($varID, $ident);
IPS_SetParent($varID, $folderID);
return $varID;
} }
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
{ {
$folderID = $this->GetDeviceFolder($deviceID); $folderID = $this->GetDeviceFolder($deviceID);