Alle HomeMatic Geräte automatisch in IPS erstellen inkl. Bezeichnung

Ich hab keine Ahnung, ob es hier schon so einen Skript gibt, Zumindest habe ich keinen gefunden.

Funktion
Der angehängte Skript erstellt automatisch eine Kategorie für jedes HomeMatic gerät, welches in der CCU2 angelegt ist. Dort wird dann jeder Kanal angelegt. Dabei werden die Bezeichnungen aus der CCU2 übernommen.
Erstellt werden nur Geräte, die nicht bereits vorhanden sind. Man kann also alle Kanäle danach nach herzenslust umsortieren und verschieben, ohne dass diese beim nächsten durchlauf neu erstellt werden.

Momentan werden auch Kanäle erstellt, welche „Nutzlos“ sind. Das dürfte aber weiter kein Problem sein.

Installation
Auf der CCU2 muss das XML-API installiert sein.

Nun erstellt man eine neue HomeMatic Geräte - Instanz. In dieser Instanz wählt man die CCU aus, für die das Skript ausgeführt werden soll. Die Adresse kann leer bleiben.

In dieser Instanz erstellt man ein Skript mit dem unten angezeigten Inhalt.
Danach einfach das Skript ausführen.


<?
//Erfordert XML Api auf HomMatic
// Skript in einer HomeMatic Geräte Instanz ausführen. Die Verbindung dieser Instanz wird verwendet.
set_time_limit(120);

$par_id = IPS_GetParent($_IPS['SELF']);
$Instance = IPS_GetInstance($par_id); // Instanz
$connectionID = $Instance['ConnectionID'];
$ccu_ip = IPS_GetProperty ($connectionID,'Host'); // IP der Homematic-CCU

$xml = simplexml_load_file('http://'.$ccu_ip.'/config/xmlapi/devicelist.cgi');
$xml = new simpleXMLElement($xml->asXML());
$InstanceList = IPS_GetInstanceListByModuleID("{EE4A81C6-5C90-4DB7-AD2F-F6BBD521412E}");


$i=0;

while($i < count($xml->device)) {
   $var_name = (string) $xml->device[$i]['name'];
   $var_address = (string) $xml->device[$i]['address'];
   $var_interface = (string) $xml->device[$i]['interface']; // BidCos-RF, VirtualDevices, ???
   $var_device_type = (string) $xml->device[$i]['device_type']; // Beispiel: "HM-CC-VG-1"
   $var_ready_config = (string) $xml->device[$i]['ready_config']; // true, false

	if ($var_interface == "VirtualDevices"){goto Skip_Device;};

  	$DeviceID = IPS_GetCategoryIDByName ($var_name, $par_id);

	$xml2 = new simpleXMLElement($xml->device[$i]->asXML());
	$i2=0;
	
	// Vorhandene Kanäle suchen
  	   $NewInstance = 0;
  	   foreach($InstanceList as $InstanceID){
  	      if (IPS_GetProperty($InstanceID, "Address") == $var_address . ":0"){
  	         if (IPS_GetInstanceParentID($InstanceID) == $connectionID){
  	         	$NewInstance = $InstanceID;
	  	      	break;
  	         }
  	      }
  	   }
  	   
  	   if ($NewInstance == 0){
		   // Device als Kategorie erstellen
		   if ($DeviceID == false){
		  		$DeviceID = IPS_CreateCategory();
  				IPS_SetName($DeviceID, $var_name);
		  		IPS_SetParent($DeviceID, $par_id);
		  	}

		   // Neuen Kanal erstellen
		   $NewInstance = IPS_CreateInstance("{EE4A81C6-5C90-4DB7-AD2F-F6BBD521412E}");
		   if (IPS_HasInstanceParent($NewInstance) && IPS_GetInstanceParentID($NewInstance) !== $connectionID){
				IPS_DisconnectInstance($NewInstance);
				IPS_ConnectInstance($NewInstance, $connectionID);

				IPS_SetParent ($NewInstance, $DeviceID);
   			IPS_SetName ($NewInstance,$var_name . " (Wartung)");
				IPS_SetProperty($NewInstance, "Address", $var_address . ":0");
				IF ($var_interface == "BidCos-RF"){
				   IPS_SetProperty($NewInstance, "Protocol", 0); // Funk
				}else{
				   IPS_SetProperty($NewInstance, "Protocol", 1); // Kabel
				}

				// IPS_SetProperty($NewInstance, "EmulateStatus", 1);
				IPS_ApplyChanges($NewInstance);
			}
		}
  	   
  	   
  	   
	
	while($i2 < count($xml2->channel)) {
	   $ch_name = (string) $xml2->channel[$i2]['name'];
	   $ch_type = (string) $xml2->channel[$i2]['type'];
  	   $ch_address = (string) $xml2->channel[$i2]['address'];
  	   $ch_direction = (string) $xml2->channel[$i2]['direction']; // UNKNOWN, RECEIVER, SENDER
  	   $ch_visible = (string) $xml2->channel[$i2]['visible']; // true, false
  	   $ch_operate = (string) $xml2->channel[$i2]['operate']; // true, false
		
		/* if ($ch_direction !== "SENDER"){
		   switch ("ch_type"){
		   	case "27":
		   	case "17":
		   	case "22":
		   	
			   default:
		      	GoTo Skip_Channel;
		   }
		}*/
		
  	   // Vorhandene Kanäle suchen
  	   $NewInstance = 0;
  	   foreach($InstanceList as $InstanceID){
  	      if (IPS_GetProperty($InstanceID, "Address") == $ch_address){
  	         if (IPS_GetInstanceParentID($InstanceID) == $connectionID){
  	         	$NewInstance = $InstanceID;
	  	      	break;
  	         }
  	      }
  	   }
  	   
		if ($NewInstance == 0){
		   // Device als Kategorie erstellen
		   if ($DeviceID == false){
		  		$DeviceID = IPS_CreateCategory();
		  		echo ("DeviceID " . $DeviceID . "
");
  				IPS_SetName($DeviceID, $var_name);
		  		IPS_SetParent($DeviceID, $par_id);
		  		
		  	}
   		echo ($var_name . "
");
   		echo ($ch_name . "
");
   		echo ($ch_address . "
");
   	
   		echo ($par_id . "
");
		
		   // Neuen Kanal erstellen
		   $NewInstance = IPS_CreateInstance("{EE4A81C6-5C90-4DB7-AD2F-F6BBD521412E}");
		   if (IPS_HasInstanceParent($NewInstance) && IPS_GetInstanceParentID($NewInstance) !== $connectionID){
				IPS_DisconnectInstance($NewInstance);
				IPS_ConnectInstance($NewInstance, $connectionID);
			}
				
			IPS_SetParent ($NewInstance, $DeviceID);
   		IPS_SetName ($NewInstance,$ch_name);
			IPS_SetProperty($NewInstance, "Address", $ch_address);
			IF ($var_interface == "BidCos-RF"){
			   IPS_SetProperty($NewInstance, "Protocol", 0); // Funk
			}else{
			   IPS_SetProperty($NewInstance, "Protocol", 1); // Kabel
			}
				
			// IPS_SetProperty($NewInstance, "EmulateStatus", 1);
			IPS_ApplyChanges($NewInstance);
		}
  	   
		Skip_Channel:
	   $i2++;
	}
	
	Skip_Device:
	$i++;
}
?>



Darf man fragen ob du das auch nochmal für HMScript also ohne XML-Api-Patch erstellst ?
Sicherheitslücken brauche ich nicht :wink:
Michael

Mit dem HMScript hab ich mich bisher noch nicht beschäftigt.

Alternativer Vorschlag: Wenn du nicht alle 5 Minuten neue Geräte hinzufügen willst, Installier doch das XML-API auf der CCU, ließ die Geräte mit meinem Skript aus und deaktiviere danach die XML API oder entferne sie wieder komplett.

Hallo awpross,

ich wollte gerade ein Script bei mir ausprobieren, doch leider endet das mit einer Fehlermeldung die wie folgt aussieht:

Category with name Buero could not be found in /usr/share/symcon/scripts/43781.ips.php on line 27
Fatal error: Call to undefined function IPS_HasInstanceParent() in /usr/share/symcon/scripts/43781.ips.php on line 53
Abort Processing during Fatal-Error: Call to undefined function IPS_HasInstanceParent()
Error in Script /usr/share/symcon/scripts/43781.ips.php on Line 53

Das ganze wollte ich mit meiner CCU2 testen mit XML-API

Hast du eine Idee was da bei mir schief läuft?

Besten Dank schon mal für deine Hilfe!