fix: support Symcon numeric dimmers and independent blind slat controls
This commit is contained in:
1 parent
b934f78b9a
commit
24188f96f7
36 files changed
+1105
-683
No files matched your search
@@ -417,12 +417,9 @@ class NextFloor extends IPSModuleStrict
|
||||
private function describeVariable(int $variableID, string $name, string $path, ?int $categoryID): array
|
||||
{
|
||||
$variable = IPS_GetVariable($variableID);
|
||||
$profile = (string) (($variable['VariableCustomProfile'] ?? '') ?: ($variable['VariableProfile'] ?? ''));
|
||||
// Symcon 9 presentations can carry a unit without any legacy variable profile.
|
||||
$presentation = IPS_GetVariablePresentation($variableID);
|
||||
$profile = (string) ($presentation['PROFILE'] ?? $profile);
|
||||
$profileData = $profile !== '' && IPS_VariableProfileExists($profile) ? IPS_GetVariableProfile($profile) : [];
|
||||
$unit = trim((string) ($presentation['SUFFIX'] ?? $profileData['Suffix'] ?? ''));
|
||||
$meta = $this->variableMetadata($variableID, $variable);
|
||||
$profile = $meta['profile'];
|
||||
$unit = $meta['unit'];
|
||||
$value = GetValue($variableID);
|
||||
$kind = $this->kindFor($variable, $profile, $name . ' ' . $path);
|
||||
$attributes = [
|
||||
@@ -441,14 +438,17 @@ class NextFloor extends IPSModuleStrict
|
||||
// Keep the actual numeric reading even when legacy name heuristics chose an actuator domain.
|
||||
// Existing synthetic entity IDs stay stable so saved plan mappings keep working.
|
||||
$attributes['symcon_numeric_value'] = $value;
|
||||
$attributes['symcon_min'] = $meta['min'];
|
||||
$attributes['symcon_max'] = $meta['max'];
|
||||
$attributes['symcon_step'] = $meta['step'];
|
||||
}
|
||||
if ($kind === 'light' && is_numeric($value)) {
|
||||
$attributes['brightness'] = (int) round(max(0, min(100, (float) $value)) * 2.55);
|
||||
$attributes['brightness'] = (int) round($this->valuePercent((float) $value, $meta) * 2.55);
|
||||
$attributes['color_mode'] = 'brightness';
|
||||
$attributes['supported_color_modes'] = ['brightness'];
|
||||
}
|
||||
if ($kind === 'cover' && is_numeric($value)) {
|
||||
$attributes['current_position'] = max(0, min(100, (int) round((float) $value)));
|
||||
$attributes['current_position'] = $this->valuePercent((float) $value, $meta);
|
||||
$attributes['supported_features'] = 4;
|
||||
}
|
||||
if ($kind === 'climate' && is_numeric($value)) {
|
||||
@@ -459,17 +459,55 @@ class NextFloor extends IPSModuleStrict
|
||||
$attributes['hvac_modes'] = ['off', 'heat'];
|
||||
$attributes['hvac_action'] = (float) $value > 0 ? 'heating' : 'idle';
|
||||
}
|
||||
$state = $this->stateFor($kind, $value);
|
||||
if (in_array($kind, ['light', 'cover'], true) && is_numeric($value)) {
|
||||
$on = $this->valuePercent((float) $value, $meta) > 0;
|
||||
$state = $kind === 'light' ? ($on ? 'on' : 'off') : ($on ? 'open' : 'closed');
|
||||
}
|
||||
return [
|
||||
'entity_id' => $kind . '.' . $variableID,
|
||||
'name' => $name,
|
||||
'path' => $path,
|
||||
'state' => $this->stateFor($kind, $value),
|
||||
'state' => $state,
|
||||
'attributes' => $attributes,
|
||||
'area_id' => $categoryID === null ? null : 'symcon-category-' . $categoryID,
|
||||
'device_id' => 'symcon-variable-' . $variableID
|
||||
];
|
||||
}
|
||||
|
||||
/** The effective Symcon 9 presentation takes precedence over an old profile's range and unit. */
|
||||
private function variableMetadata(int $variableID, array $variable): array
|
||||
{
|
||||
$presentation = IPS_GetVariablePresentation($variableID);
|
||||
$profile = (string) ($presentation['PROFILE'] ?? (($variable['VariableCustomProfile'] ?? '') ?: ($variable['VariableProfile'] ?? '')));
|
||||
$legacy = $profile !== '' && IPS_VariableProfileExists($profile) ? IPS_GetVariableProfile($profile) : [];
|
||||
$min = (float) ($presentation['MIN'] ?? $legacy['MinValue'] ?? 0);
|
||||
$max = (float) ($presentation['MAX'] ?? $legacy['MaxValue'] ?? 100);
|
||||
if (!is_finite($min) || !is_finite($max) || $max <= $min) {
|
||||
[$min, $max] = [0.0, 100.0];
|
||||
}
|
||||
$step = (float) ($presentation['STEP_SIZE'] ?? $legacy['StepSize'] ?? 0);
|
||||
return ['profile' => $profile, 'unit' => trim((string) ($presentation['SUFFIX'] ?? $legacy['Suffix'] ?? '')),
|
||||
'min' => $min, 'max' => $max, 'step' => is_finite($step) && $step > 0 ? $step : 0.0];
|
||||
}
|
||||
|
||||
private function valuePercent(float $value, array $meta): float
|
||||
{
|
||||
return max(0, min(100, ($value - $meta['min']) / ($meta['max'] - $meta['min']) * 100));
|
||||
}
|
||||
|
||||
private function percentValue(mixed $percent, array $meta): float
|
||||
{
|
||||
if (!is_numeric($percent) || !is_finite((float) $percent)) {
|
||||
throw new InvalidArgumentException('invalid_control_percentage');
|
||||
}
|
||||
$value = $meta['min'] + max(0, min(100, (float) $percent)) / 100 * ($meta['max'] - $meta['min']);
|
||||
if ($meta['step'] > 0) {
|
||||
$value = $meta['min'] + round(($value - $meta['min']) / $meta['step']) * $meta['step'];
|
||||
}
|
||||
return max($meta['min'], min($meta['max'], $value));
|
||||
}
|
||||
|
||||
private function kindFor(array $variable, string $profile, string $haystack): string
|
||||
{
|
||||
$text = mb_strtolower($profile . ' ' . $haystack);
|
||||
@@ -770,6 +808,23 @@ class NextFloor extends IPSModuleStrict
|
||||
{
|
||||
$variable = IPS_GetVariable($variableID);
|
||||
$current = GetValue($variableID);
|
||||
if ($domain === 'cover' && $service === 'stop_cover') {
|
||||
throw new InvalidArgumentException('cover_stop_not_supported');
|
||||
}
|
||||
if (in_array($domain, ['light', 'cover'], true) && in_array((int) $variable['VariableType'], [1, 2], true)) {
|
||||
$meta = $this->variableMetadata($variableID, $variable);
|
||||
$percent = match ($service) {
|
||||
'toggle' => (float) $current > $meta['min'] ? 0 : 100,
|
||||
'turn_off', 'close_cover', 'close_cover_tilt' => 0,
|
||||
'set_cover_position' => $data['position'] ?? null,
|
||||
'set_cover_tilt_position' => $data['tilt_position'] ?? null,
|
||||
'turn_on' => $data['brightness_pct'] ?? (isset($data['brightness']) ? (float) $data['brightness'] / 2.55 : 100),
|
||||
'open_cover', 'open_cover_tilt' => 100,
|
||||
default => throw new InvalidArgumentException('unsupported_control_service')
|
||||
};
|
||||
$this->setVariable($variableID, $variable, $this->percentValue($percent, $meta));
|
||||
return;
|
||||
}
|
||||
$value = match ($service) {
|
||||
'toggle', 'media_play_pause' => is_bool($current) ? !$current : (is_numeric($current) ? ((float) $current > 0 ? 0 : 100) : true),
|
||||
'turn_on', 'open_cover', 'lock' => $this->onValue($variable, $data),
|
||||
|
||||
Reference in new issue
Block a user