Files
NextFloor/symcon/tests/variables.php
T

65 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
// Native Symcon 9 presentation metadata, with invented KNX values and no running server.
class IPSModuleStrict {}
$variables = [
1 => ['VariableType' => 2, 'VariableAction' => 0],
2 => ['VariableType' => 2, 'VariableAction' => 99],
3 => ['VariableType' => 2, 'VariableProfile' => '~Temperature'],
4 => ['VariableType' => 2, 'VariableProfile' => '~Temperature'],
5 => ['VariableType' => 0, 'VariableAction' => 0],
6 => ['VariableType' => 1, 'VariableAction' => 99],
7 => ['VariableType' => 2, 'VariableProfile' => '~Intensity.1', 'VariableAction' => 99],
8 => ['VariableType' => 1, 'VariableAction' => 99],
9 => ['VariableType' => 1, 'VariableAction' => 99]
];
$presentations = [1 => ['SUFFIX' => ' °C'], 2 => ['SUFFIX' => "\u{00A0}°C"], 3 => ['PROFILE' => '~Temperature'], 4 => ['SUFFIX' => ' W']];
$presentations[6] = ['MIN' => 0, 'MAX' => 255, 'STEP_SIZE' => 1, 'USAGE_TYPE' => 2, 'SUFFIX' => '%'];
$presentations[8] = ['MIN' => 0, 'MAX' => 100, 'STEP_SIZE' => 1];
$presentations[9] = ['MIN' => 0, 'MAX' => 255, 'STEP_SIZE' => 1];
function IPS_GetVariable(int $id): array { global $variables; return $variables[$id] + ['VariableProfile' => '', 'VariableCustomProfile' => '', 'VariableAction' => 0, 'VariableCustomAction' => 0]; }
function IPS_GetVariablePresentation(int $id): array { global $presentations; return $presentations[$id] ?? []; }
function IPS_VariableProfileExists(string $profile): bool { return in_array($profile, ['~Temperature', '~Intensity.1'], true); }
function IPS_GetVariableProfile(string $profile): array { return $profile === '~Intensity.1' ? ['MinValue' => 0, 'MaxValue' => 1, 'StepSize' => 0.01, 'Suffix' => ' %'] : ['Suffix' => ' °C']; }
function GetValue(int $id): float|int|bool { return match ($id) { 5 => true, 6, 9 => 128, 7 => 0.4, 8 => 40, default => 21.75 }; }
function GetValueFormatted(int $id): string { return $id === 5 ? 'An' : '21,75'; }
$writes = [];
function RequestAction(int $id, mixed $value): void { global $writes; $writes[] = [$id, $value]; }
function SetValue(int $id, mixed $value): void { global $writes; $writes[] = [$id, $value]; }
require __DIR__ . '/../NextFloor/module.php';
$module = new NextFloor();
$describe = new ReflectionMethod($module, 'describeVariable');
function check(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
$plain = $describe->invoke($module, 1, 'Wert', 'Haus / Energie / KNX / Wert', 10);
check($plain['attributes']['device_class'] === 'temperature', 'The presentation unit identifies a generically named KNX temperature');
check($plain['attributes']['unit_of_measurement'] === '°C', 'The current presentation exports its unit without a legacy profile');
$actuator = $describe->invoke($module, 2, 'Wert', 'Haus / KNX / Heizung / Wert', 10);
check($actuator['entity_id'] === 'climate.2', 'Existing inferred entity IDs must stay stable');
check($actuator['state'] === 'heat' && $actuator['attributes']['symcon_numeric_value'] === 21.75, 'The original numeric reading survives the actuator state conversion');
check($actuator['attributes']['device_class'] === 'temperature', 'A nonbreaking space in the unit still identifies a temperature');
$legacy = $describe->invoke($module, 3, 'Wert', 'Haus / KNX / Wert', 10);
check($legacy['attributes']['unit_of_measurement'] === '°C', 'Legacy profile suffixes remain supported');
$override = $describe->invoke($module, 4, 'Temperatur', 'Haus / Temperatur', 10);
check($override['attributes']['unit_of_measurement'] === 'W' && $override['attributes']['device_class'] === 'power', 'The effective presentation overrides a stale legacy profile and ancestor names');
$boolean = $describe->invoke($module, 5, 'Kontakt', 'Haus / Kontakt', 10);
check(!isset($boolean['attributes']['symcon_numeric_value']), 'Boolean values are not advertised as numeric readings');
$dimmer = $describe->invoke($module, 6, 'Dimmer', 'Haus / Dimmer', 10);
check($dimmer['attributes']['brightness'] === 128, 'An integer dimmer uses its real 0-255 range');
$floatDimmer = $describe->invoke($module, 7, 'Wert', 'Haus / Wert', 10);
check($floatDimmer['attributes']['brightness'] === 102, 'A normalized float dimmer uses its legacy 0-1 profile range');
$write = new ReflectionMethod($module, 'writeAction');
$write->invoke($module, 6, 'light', 'turn_on', ['brightness_pct' => 50]);
$write->invoke($module, 7, 'light', 'turn_on', ['brightness_pct' => 30]);
$write->invoke($module, 8, 'cover', 'set_cover_position', ['position' => 60]);
$write->invoke($module, 9, 'cover', 'set_cover_tilt_position', ['tilt_position' => 25]);
$write->invoke($module, 6, 'light', 'turn_on', ['brightness' => 200]);
check($writes === [[6, 128], [7, 0.3], [8, 60], [9, 64], [6, 200]], 'Light, position and slat commands target the right variables, scale and cast to the native type');
$count = count($writes);
try { $write->invoke($module, 8, 'cover', 'stop_cover', []); throw new RuntimeException('Unsupported stop was accepted'); } catch (InvalidArgumentException) {}
try { $write->invoke($module, 8, 'cover', 'set_cover_position', ['position' => 'invalid']); throw new RuntimeException('Invalid percentage was accepted'); } catch (InvalidArgumentException) {}
check(count($writes) === $count, 'Invalid values and unsupported stop never move an actuator');
echo "OK: Symcon 9 units, KNX float readings, integer/float dimmers, separate blind actions, range scaling and legacy profiles\n";