no message

This commit is contained in:
2025-11-14 09:46:57 +01:00
parent 807cc007bc
commit 1e23cf94c6
+45 -48
View File
@@ -73,15 +73,16 @@ class Shelly_Parser_MQTT extends IPSModule
* ---------------------------------------------------------*/
public function RequestAction($Ident, $Value)
{
$this->SetValue($Ident, $Value);
// Output: <deviceID>_output
// Ident ist z.B. "BE_1_3_14_output"
if (str_ends_with($Ident, '_output')) {
$deviceID = substr($Ident, 0, -strlen('_output'));
$topic = $deviceID . '/rpc/Switch.Set';
// lokale Variable aktualisieren
$varID = $this->EnsureBooleanVariable($deviceID, $Ident, 'Output');
SetValue($varID, (bool)$Value);
// Shelly schalten
$topic = $deviceID . '/rpc/Switch.Set';
$payload = json_encode([
'id' => 0,
'on' => (bool)$Value
@@ -91,7 +92,6 @@ class Shelly_Parser_MQTT extends IPSModule
}
}
/* ---------------------------------------------------------
* RECEIVE MQTT DATA
* ---------------------------------------------------------*/
@@ -141,14 +141,10 @@ class Shelly_Parser_MQTT extends IPSModule
{
$value = ($payload === 'true' || $payload === '1');
$this->SetValue(
$this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online'),
$value
);
$varID = $this->EnsureBooleanVariable($deviceID, $deviceID . '_online', 'Online');
SetValue($varID, $value);
}
/* ---------------------------------------------------------
* RPC-EVENTS
* ---------------------------------------------------------*/
@@ -161,59 +157,55 @@ class Shelly_Parser_MQTT extends IPSModule
// Shelly prüfen
$src = $json['src'] ?? '';
if (!str_starts_with($src, 'shelly')) {
if (!is_string($src) || !str_starts_with($src, 'shelly')) {
return;
}
// Typ extrahieren
// Typ extrahieren (z.B. "1minig3", "1g4", "plusplugs", ...)
$type = ShellyParser::ExtractType($src);
$this->SetValue(
$this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ'),
$type
);
$typeID = $this->EnsureStringVariable($deviceID, $deviceID . '_type', 'Typ');
SetValue($typeID, $type);
// Parameter extrahieren
$params = $json['params'] ?? [];
if (!is_array($params)) {
return;
}
$mapped = ShellyParser::MapParams($params);
// Output
if (array_key_exists('output', $mapped)) {
$varID = $this->EnsureBooleanVariable(
$outID = $this->EnsureBooleanVariable(
$deviceID,
$deviceID . '_output',
'Output'
);
$this->EnableAction($deviceID . '_output');
SetValue($varID, $mapped['output']);
SetValue($outID, (bool)$mapped['output']);
}
// Input (GEN4 / PRO)
if (array_key_exists('input', $mapped)) {
SetValue(
$this->EnsureBooleanVariable(
$deviceID,
$deviceID . '_input',
'Input'
),
$mapped['input']
$inID = $this->EnsureBooleanVariable(
$deviceID,
$deviceID . '_input',
'Input'
);
SetValue($inID, (bool)$mapped['input']);
}
// Temperatur
// Temperatur (falls gefunden)
if (array_key_exists('temperature', $mapped)) {
SetValue(
$this->EnsureFloatVariable(
$deviceID,
$deviceID . '_temperature',
'Temperatur'
),
$mapped['temperature']
$tempID = $this->EnsureFloatVariable(
$deviceID,
$deviceID . '_temperature',
'Temperatur'
);
SetValue($tempID, (float)$mapped['temperature']);
}
}
/* ---------------------------------------------------------
* Helper für Variablen
* ---------------------------------------------------------*/
@@ -222,7 +214,8 @@ class Shelly_Parser_MQTT extends IPSModule
$folderID = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
return $cid;
}
}
@@ -237,7 +230,8 @@ class Shelly_Parser_MQTT extends IPSModule
$folderID = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
return $cid;
}
}
@@ -252,7 +246,8 @@ class Shelly_Parser_MQTT extends IPSModule
$folderID = $this->GetDeviceFolder($deviceID);
foreach (IPS_GetChildrenIDs($folderID) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $ident) {
$obj = IPS_GetObject($cid);
if ($obj['ObjectIdent'] === $ident && $obj['ObjectType'] === OBJECTTYPE_VARIABLE) {
return $cid;
}
}
@@ -262,17 +257,22 @@ class Shelly_Parser_MQTT extends IPSModule
return $id;
}
/* ---------------------------------------------------------
* Geräte-Ordner
* ---------------------------------------------------------*/
private function GetDeviceFolder(string $deviceID): int
{
$folderIdent = 'folder_' . $deviceID;
$folderID = @IPS_GetObjectIDByIdent($folderIdent);
if ($folderID !== false) {
return $folderID;
// 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);
@@ -280,8 +280,5 @@ class Shelly_Parser_MQTT extends IPSModule
return $folderID;
}
}
?>