Files
NextFloor/symcon/tests/variables.php
T

40 lines
3.0 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]
];
$presentations = [1 => ['SUFFIX' => ' °C'], 2 => ['SUFFIX' => "\u{00A0}°C"], 3 => ['PROFILE' => '~Temperature'], 4 => ['SUFFIX' => ' W']];
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 $profile === '~Temperature'; }
function IPS_GetVariableProfile(string $profile): array { return ['Suffix' => ' °C']; }
function GetValue(int $id): float|bool { return $id === 5 ? true : 21.75; }
function GetValueFormatted(int $id): string { return $id === 5 ? 'An' : '21,75'; }
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');
echo "OK: Symcon 9 presentation units, KNX float readings, stable mappings and legacy profiles\n";