Amazon Echo (Alexa) mit IP-Symcon verbinden

Hi,
die Szenen in der Alexa-Integration benutzen jetzt auch Aktionen und ich musste die Tabelle aller Alexa Kommandos etwas anpassen. Hier die geänderte Version:

<?
$id_alexa = 0;
$instanceList = IPS_GetInstanceList();
foreach ($instanceList as $id) {
    $instance = IPS_GetInstance($id);
    if ($instance['ModuleInfo']['ModuleName'] == 'Alexa'){
        $id_alexa = $instance['InstanceID'];
        break;
    }
}
if ($id_alexa == 0) return;
$list = [];
$properties = json_decode(IPS_GetConfiguration($id_alexa), true);
foreach($properties as $name => $value) {
    if (substr($name, 0, 6) == "Device") {
        $devices = json_decode($value, true);
        foreach($devices as $device) {
            switch ($name){
                case 'DeviceSimpleScene':
                    $SimpleScene = $device["SceneControllerSimpleAction"];
                    $SimpleAction = json_decode($SimpleScene, true);
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $SimpleAction["parameters"]["TARGET"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceDeactivatableScene':
                    $SceneActivate = json_decode($device["SceneControllerDeactivatableActivateAction"], true);
                    $SceneDeActivate = json_decode($device["SceneControllerDeactivatableDeactivateAction"], true);
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $SceneActivate["parameters"]["TARGET"],
                        "ID2" => $SceneDeActivate["parameters"]["TARGET"],
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
//                    print_r($device);
                    break;
                case 'DeviceGenericSwitch':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PowerControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceLightSwitch':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PowerControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceLightDimmer':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["BrightnessControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceLightExpert':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PowerControllerID"],
                        "ID2" => $device["BrightnessOnlyControllerID"],
                        "ID3" => $device["ColorOnlyControllerID"],
                        "ID4" => $device["ColorTemperatureOnlyControllerID"],
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceLock':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["LockControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceShutter':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["RangeControllerShutterID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceSpeaker':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["SpeakerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceTemperatureSensor':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["TemperatureSensorID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceGenericSlider':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PercentageControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceLightColor':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["ColorControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceMediaPlayer':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PowerControllerID"],
                        "ID2" => $device["SpeakerMuteableVolumeID"],
                        "ID3" => $device["SpeakerMuteableMuteID"],
                        "ID4" => $device["PlaybackControllerID"],
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceSpeakerMuteable':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["SpeakerMuteableVolumeID"],
                        "ID2" => $device["SpeakerMuteableMuteID"],
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
                case 'DeviceTelevision':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["PowerControllerID"],
                        "ID2" => $device["ChannelControllerID"],
                        "ID3" => $device["SpeakerMuteableVolumeID"],
                        "ID4" => $device["SpeakerMuteableMuteID"],
                        "ID5" => $device["InputControllerID"]
                    ];
                    break;
                  case 'DeviceThermostat':
                    $list[] = [
                        "Name" => $device["Name"],
                        "Type" => substr($name, 6),
                        "ID1" => $device["ThermostatControllerID"],
                        "ID2" => 0,
                        "ID3" => 0,
                        "ID4" => 0,
                        "ID5" => 0
                    ];
                    break;
              default:
                        var_dump($name);
                        var_dump($device);
                    break;
            }
        }
    }
}

$UpdateList = [];
$maxName = 0;
$maxType = 0;
$index = 0;
foreach($list as $item) {
    $UpdateList[$index]['Name'] = mb_str_pad($item['Name'], $maxName, " ") ;
    $UpdateList[$index]['Type'] = mb_str_pad($item['Type'], $maxType, " ");
    $UpdateList[$index]['ID1'] = $item['ID1'];
    $UpdateList[$index]['ID2'] = $item['ID2'];
    $UpdateList[$index]['ID3'] = $item['ID3'];
    $UpdateList[$index]['ID4'] = $item['ID4'];
    $UpdateList[$index]['ID5'] = $item['ID5'];
    $index++;
}

$Type  = array_column($UpdateList, 'Type');
$Name = array_column($UpdateList, 'Name');
array_multisort($Type, SORT_ASC, $Name, SORT_ASC, $UpdateList);

// HTML ereugen
$HintergrundFarbcode = '000000';
$TextFarbcode = 'FFFFFF';
$TextFarbcodeNOK = 'FF0000';
$TextSize = 14;
$TextSizeTitle = $TextSize + 2;
$TextWeight = 'normal';
$Textausrichtung = 'text-align:left;';

$HTML_CSS_Style = '<style type="text/css">
.bt {border-collapse: separate;border-spacing: 2px;}
.bt td {font-family:Arial, sans-serif;font-size:' . $TextSize . 'px;color:#' . $TextFarbcode . ';' . $Textausrichtung . 'padding:1px 10px;border-style:solid;border-width:1px;overfNOK:hidden;word-break:normal;}
.bt th {font-family:Arial, sans-serif;font-size:' . $TextSize . 'px;color:#' . $TextFarbcode . ';' . $Textausrichtung . 'padding:1px 10px;border-style:solid;border-width:1px;overfNOK:hidden;word-break:normal;}
.bt .tb-title {font-size:' . $TextSizeTitle . 'px;padding:2mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcode . '}
.bt .tb-content {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcode . '}
.bt .tb-contentNOK {font-size:' . $TextSize . 'px;font-weight: ' . $TextWeight . ';padding:1mm;' . $Textausrichtung . 'background-color:#' . $HintergrundFarbcode . ';color:#' . $TextFarbcodeNOK . '}
</style>';

$HTML = '<html>' . $HTML_CSS_Style;
$HTML .= '<table class="bt"';

$Titel = array();
$Titel = array('Nummer');
$Titel[] = 'Name';
$Titel[] = 'Typ';
$Titel[] = 'ID1';
$Titel[] = 'ID2';
$Titel[] = 'ID3';
$Titel[] = 'ID4';
$Titel[] = 'ID5';

$HTML .= '<tr>';

$colspan = 1;
foreach ($Titel as $TitelEntry) {
    $HTML .= '<th class="tb-title"><b>' . $TitelEntry . '</b></th>';
    $colspan++;
}
$HTML .= '</tr><tr>';

$index = 0;
foreach ($UpdateList as $Update) {
    $index++;
    $HTML .= '<th class="tb-content">' . $index . '</th>';
    $HTML .= '<th class="tb-content">' . $Update['Name'] . '</th>';
    $HTML .= '<th class="tb-content">' . $Update['Type'] . '</th>';
    if ($Update['ID1'] > 0){
        if (IPS_ObjectExists($Update['ID1'])){
            $IDName = IPS_GetName($Update['ID1']);
            $HTML .= '<th class="tb-content">' . $Update['ID1'] . ':' . $IDName . '</th>';
        }
        else{
            $HTML .= '<th class="tb-contentNOK">' . $Update['ID1'] . ':</th>';
        }
    }
    else{
        $HTML .= '<th class="tb-contentL"> </th>';

    }
    if ($Update['ID2'] > 0){
        if (IPS_ObjectExists($Update['ID2'])){
            $IDName = IPS_GetName($Update['ID2']);
            $HTML .= '<th class="tb-content">' . $Update['ID2'] . ':' . $IDName . '</th>';
        }
        else{
            $HTML .= '<th class="tb-contentNOK">' . $Update['ID2'] . ':</th>';
        }
    }
    else{
        $HTML .= '<th class="tb-contentL"> </th>';

    }
    if ($Update['ID3'] > 0){
        if (IPS_ObjectExists($Update['ID3'])){
            $IDName = IPS_GetName($Update['ID3']);
            $HTML .= '<th class="tb-content">' . $Update['ID3'] . ':' . $IDName . '</th>';
        }
        else{
            $HTML .= '<th class="tb-contentNOK">' . $Update['ID3'] . ':</th>';
        }
    }
    else{
        $HTML .= '<th class="tb-contentL"> </th>';

    }
    if ($Update['ID4'] > 0){
        if (IPS_ObjectExists($Update['ID4'])){
            $IDName = IPS_GetName($Update['ID4']);
            $HTML .= '<th class="tb-content">' . $Update['ID4'] . ':' . $IDName . '</th>';
        }
        else{
            $HTML .= '<th class="tb-contentNOK">' . $Update['ID4'] . ':</th>';
        }
    }
    else{
        $HTML .= '<th class="tb-contentL"> </th>';

    }
    if ($Update['ID5'] > 0){
        if (IPS_ObjectExists($Update['ID5'])){
            $IDName = IPS_GetName($Update['ID5']);
            $HTML .= '<th class="tb-content">' . $Update['ID5'] . ':' . $IDName . '</th>';
        }
        else{
            $HTML .= '<th class="tb-contentNOK">' . $Update['ID5'] . ':</th>';
        }
    }
    else{
        $HTML .= '<th class="tb-contentL"> </th>';

    }
    $HTML .= '</tr>';
}
$HTML .= '</table></html>';
$vid = @IPS_GetVariableIDByName("Alexa Kommandos", $_IPS['SELF']);

if ($vid == false){
    $vid = IPS_CreateVariable(3);
    IPS_SetParent($vid, $_IPS['SELF']);             //Variable zuordnen
    IPS_SetName($vid, "Alexa Kommandos");
    IPS_SetVariableCustomProfile($vid, "~HTMLBox");
}

SetValue( $vid, $HTML);

function mb_str_pad( $input, $pad_length, $pad_string = ' ', $pad_type = STR_PAD_RIGHT)
{
    $diff = strlen( $input ) - mb_strlen( $input );
    return str_pad( $input, $pad_length + $diff, $pad_string, $pad_type );
}
?>

Falls es noch weitere Änderungen gab bitte melden.

Ralf

Ich habe festgestellt, dass seit der Umstellung der Szenen auf Aktionen standardmäßig kein $_IPS[‚SENDER‘] mehr gesetzt wird, wenn ich die Aktion „Automation ausführen“ nutze. Ist das beabsichtigt und ich muss jetzt immer die erweiterte Aktion nutzen und den SENDER manuell auf „VoiceControl“ setzen?

Gruß
Slummi

EDIT:
Oder ist die Aktion generisch und wird nicht nur von diesem Modul verwendet? Dann wäre es klar, dass der SENDER nicht gesetzt werden kann. Dann sollte es aber denke ich in der Doku erwähnt werden, dass der SENDER nicht in jedem Fall „VoiceControl“ ist, wenn eine Aktion über das Alexa-Modul ausgeführt wird.

Oh ja, da muss ich die Dokumentation nochmal auf die Aktionen anpassen.

Da die Aktionen generisch sind, gibt es den SENDER VoiceControl nicht mehr. Deswegen werden alte Skriptaufrufe auch auf „Führe Automation mit Parametern aus“ konvertiert, in welcher dann SENDER und VALUE gesetzt sind.

1 „Gefällt mir“