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
* ---------------------------------------------------------*/
public function RequestAction($Ident, $Value)
{
IPS_LogMessage("ShellyDebug", "RequestAction($Ident$Value)");
public function RequestAction($Ident, $Value)
{
IPS_LogMessage('ShellyDebug', "RequestAction($Ident" . var_export($Value, true) . ')');
// Prüfen ob es eine Output-Variable ist
if (str_contains($Ident, '_output_')) {
@@ -85,33 +85,33 @@ public function RequestAction($Ident, $Value)
}
// device + index extrahieren
list($deviceID, $suffix) = explode('_output_', $Ident);
[$deviceID, $suffix] = explode('_output_', $Ident, 2);
$index = intval($suffix);
// Shelly-RPC Topic (wie im IPS-Shelly Modul)
// Shelly-RPC Topic
$topic = $deviceID . '/rpc';
// Shelly-RPC-kompatibles JSON
$payload = json_encode([
"id" => 1,
"src" => "ips",
"method" => "Switch.Set",
"params" => [
"id" => $index,
"on" => (bool)$Value
$payloadArr = [
'id' => 1,
'src' => 'ips',
'method' => 'Switch.Set',
'params' => [
'id' => $index,
'on' => (bool) $Value
]
]);
];
$payload = json_encode($payloadArr);
IPS_LogMessage("ShellyDebug", "SEND → $topic : $payload");
IPS_LogMessage('ShellyDebug', "SEND → $topic : $payload");
// absenden
$this->Publish($topic, $payload);
return;
}
throw new Exception("Unknown Ident: " . $Ident);
}
throw new Exception('Unknown Ident: ' . $Ident);
}
/* ---------------------------------------------------------
* RECEIVE MQTT DATA
@@ -193,54 +193,59 @@ public function RequestAction($Ident, $Value)
// -----------------------------------
// DYNAMISCHE OUTPUTS
// -----------------------------------
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . "_output_" . $index;
$name = "Output $index";
if (isset($mapped['outputs']) && is_array($mapped['outputs'])) {
foreach ($mapped['outputs'] as $index => $value) {
$ident = $deviceID . '_output_' . $index;
$name = 'Output ' . $index;
$varID = $this->EnsureBooleanVariable($deviceID, $ident, $name);
// damit RequestAction überhaupt ausgeführt wird
IPS_SetVariableCustomAction($varID, $this->InstanceID);
SetValue($varID, $value);
}
}
}
// -----------------------------------
// DYNAMISCHE INPUTS
// -----------------------------------
foreach ($mapped['inputs'] as $index => $value) {
$ident = $deviceID . "_input_" . $index;
$name = "Input $index";
if (isset($mapped['inputs']) && is_array($mapped['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) {
if (array_key_exists('temperature', $mapped) && $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
{
private function EnsureBooleanVariable(string $deviceID, string $ident, string $name): int
{
$folderID = $this->GetDeviceFolder($deviceID);
// vorhandene Variable finden
foreach (IPS_GetChildrenIDs($folderID) as $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;
}
}
@@ -251,10 +256,14 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
IPS_SetName($varID, $name);
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;
}
}
private function EnsureFloatVariable(string $deviceID, string $ident, string $name): int
{
@@ -324,8 +333,8 @@ private function EnsureBooleanVariable(string $deviceID, string $ident, string $
return 0;
}
private function FindVariableByIdent(string $Ident)
{
private function FindVariableByIdent(string $Ident)
{
foreach (IPS_GetChildrenIDs($this->InstanceID) as $folder) {
foreach (IPS_GetChildrenIDs($folder) as $cid) {
if (IPS_GetObject($cid)['ObjectIdent'] === $Ident) {
@@ -334,8 +343,6 @@ private function FindVariableByIdent(string $Ident)
}
}
return 0;
}
}
}
?>