no message

This commit is contained in:
2025-11-14 09:40:47 +01:00
parent 8c7b860b2a
commit 807cc007bc
2 changed files with 84 additions and 61 deletions

View File

@@ -38,14 +38,29 @@ class ShellyParser
'temperature' => null 'temperature' => null
]; ];
// GEN3 / GEN4 Geräte → switch:0, switch:1, ...
foreach ($params as $key => $value) {
if (str_starts_with($key, 'switch:') && is_array($value)) {
// Output
if (isset($value['output'])) {
$mapped['output'] = (bool)$value['output'];
}
// Input (für Pro / Gen4 Geräte)
if (isset($value['input'])) {
$mapped['input'] = (bool)$value['input'];
}
}
}
// Temperatur suchen (beliebig tief)
self::ExtractRecursive($params, $mapped); self::ExtractRecursive($params, $mapped);
// null-Werte rauswerfen return array_filter($mapped, fn($v) => $v !== null);
return array_filter($mapped, static function ($v) {
return $v !== null;
});
} }
private static function ExtractRecursive(array $data, array &$mapped): void private static function ExtractRecursive(array $data, array &$mapped): void
{ {
foreach ($data as $key => $value) { foreach ($data as $key => $value) {

View File

@@ -152,54 +152,66 @@ class Shelly_Parser_MQTT extends IPSModule
/* --------------------------------------------------------- /* ---------------------------------------------------------
* RPC-EVENTS * RPC-EVENTS
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function HandleRPC(string $deviceID, string $payload): void private function HandleRPC(string $deviceID, string $payload): void
{ {
$json = json_decode($payload, true); $json = json_decode($payload, true);
if (!is_array($json)) { if (!is_array($json)) {
return; return;
} }
$src = $json['src'] ?? ''; // Shelly prüfen
if (!is_string($src) || !str_starts_with($src, 'shelly')) { $src = $json['src'] ?? '';
return; if (!str_starts_with($src, 'shelly')) {
} return;
}
// Modell Typ extrahieren // Typ extrahieren
$type = ShellyParser::ExtractType($src); $type = ShellyParser::ExtractType($src);
// Typ Variable
$this->SetValue(
$this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'),
$type
);
// PARAMETER MAPPEN
$params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params);
// INPUT
if (isset($mapped['input'])) {
$this->SetValue( $this->SetValue(
$this->EnsureBooleanVariable($deviceID, $deviceID . '_input', 'Input'), $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'),
(bool)$mapped['input'] $type
); );
// Parameter extrahieren
$params = $json['params'] ?? [];
$mapped = ShellyParser::MapParams($params);
// Output
if (array_key_exists('output', $mapped)) {
$varID = $this->EnsureBooleanVariable(
$deviceID,
$deviceID . '_output',
'Output'
);
$this->EnableAction($deviceID . '_output');
SetValue($varID, $mapped['output']);
}
// Input (GEN4 / PRO)
if (array_key_exists('input', $mapped)) {
SetValue(
$this->EnsureBooleanVariable(
$deviceID,
$deviceID . '_input',
'Input'
),
$mapped['input']
);
}
// Temperatur
if (array_key_exists('temperature', $mapped)) {
SetValue(
$this->EnsureFloatVariable(
$deviceID,
$deviceID . '_temperature',
'Temperatur'
),
$mapped['temperature']
);
}
} }
// OUTPUT
if (isset($mapped['output'])) {
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_output', 'Output');
$this->EnableAction($deviceID . '_output');
$this->SetValue($varID, (bool)$mapped['output']);
}
// TEMPERATURE
if (isset($mapped['temperature'])) {
$this->SetValue(
$this->EnsureFloatVariable($deviceID, $deviceID . '_temperature', 'Temperatur'),
(float)$mapped['temperature']
);
}
}
/* --------------------------------------------------------- /* ---------------------------------------------------------
@@ -207,20 +219,17 @@ private function HandleRPC(string $deviceID, string $payload): void
* ---------------------------------------------------------*/ * ---------------------------------------------------------*/
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{ {
$folderID = @IPS_GetObjectIDByIdent($folderIdent, $this->InstanceID); $folderID = $this->GetDeviceFolder($deviceID);
// Immer im Ordner suchen
foreach (IPS_GetChildrenIDs($folderID) as $cid) { foreach (IPS_GetChildrenIDs($folderID) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) { if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
return $cid; return $cid;
} }
} }
// Neue Variable anlegen $id = $this->RegisterVariableBoolean($ident, $name);
$varID = $this->RegisterVariableBoolean($ident, $name); IPS_SetParent($id, $folderID);
IPS_SetParent($varID, $folderID); return $id;
return $varID;
} }
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
@@ -233,10 +242,9 @@ private function HandleRPC(string $deviceID, string $payload): void
} }
} }
$varID = $this->RegisterVariableFloat($ident, $name); $id = $this->RegisterVariableFloat($ident, $name);
IPS_SetParent($varID, $folderID); IPS_SetParent($id, $folderID);
return $id;
return $varID;
} }
private function EnsureStringVariable(string $deviceID, string $ident, string $name): int private function EnsureStringVariable(string $deviceID, string $ident, string $name): int
@@ -249,13 +257,13 @@ private function HandleRPC(string $deviceID, string $payload): void
} }
} }
$varID = $this->RegisterVariableString($ident, $name); $id = $this->RegisterVariableString($ident, $name);
IPS_SetParent($varID, $folderID); IPS_SetParent($id, $folderID);
return $id;
return $varID;
} }
private function GetDeviceFolder(string $deviceID): int private function GetDeviceFolder(string $deviceID): int
{ {
$folderIdent = 'folder_' . $deviceID; $folderIdent = 'folder_' . $deviceID;