feat(energy-pie): finish configurable colors and Symcon 8 migration
Tests / test (push) Waiting to run
Tests / test (push) Waiting to run
This commit is contained in:
@@ -15,6 +15,16 @@ class Energiediagramm extends IPSModule
|
||||
private const ATTR_CUSTOM_END = 'CustomEnd';
|
||||
private const COLOR_SELF_CONSUMPTION = '#2F80ED';
|
||||
private const COLOR_AUTARKY = '#22A06B';
|
||||
private const COLOR_BACKGROUND = '#121216';
|
||||
private const COLOR_BACKGROUND_ACCENT_1 = '#63B3FF';
|
||||
private const COLOR_BACKGROUND_ACCENT_2 = '#A855F7';
|
||||
private const COLOR_PROPERTIES = [
|
||||
'FarbeEigenverbrauch' => self::COLOR_SELF_CONSUMPTION,
|
||||
'FarbeAutarkie' => self::COLOR_AUTARKY,
|
||||
'FarbeHintergrund' => self::COLOR_BACKGROUND,
|
||||
'FarbeHintergrundAkzent1' => self::COLOR_BACKGROUND_ACCENT_1,
|
||||
'FarbeHintergrundAkzent2' => self::COLOR_BACKGROUND_ACCENT_2,
|
||||
];
|
||||
|
||||
public function Create(): void
|
||||
{
|
||||
@@ -30,6 +40,13 @@ class Energiediagramm extends IPSModule
|
||||
$this->RegisterPropertyInteger('Aktualisierungsintervall', 60);
|
||||
$this->RegisterPropertyString('FarbeEigenverbrauch', self::COLOR_SELF_CONSUMPTION);
|
||||
$this->RegisterPropertyString('FarbeAutarkie', self::COLOR_AUTARKY);
|
||||
$this->RegisterPropertyString('FarbeHintergrund', self::COLOR_BACKGROUND);
|
||||
$this->RegisterPropertyString('FarbeHintergrundAkzent1', self::COLOR_BACKGROUND_ACCENT_1);
|
||||
$this->RegisterPropertyString('FarbeHintergrundAkzent2', self::COLOR_BACKGROUND_ACCENT_2);
|
||||
// Keep legacy string properties: changing their type discards saved colors in Symcon 8.
|
||||
foreach (self::COLOR_PROPERTIES as $property => $default) {
|
||||
$this->RegisterPropertyInteger($property . 'RGB', (int) hexdec(substr($default, 1)));
|
||||
}
|
||||
$this->RegisterPropertyBoolean('Debug', false);
|
||||
|
||||
$today = date('Y-m-d');
|
||||
@@ -37,6 +54,7 @@ class Energiediagramm extends IPSModule
|
||||
$this->RegisterAttributeString(self::ATTR_DATE, $today);
|
||||
$this->RegisterAttributeString(self::ATTR_CUSTOM_START, $today);
|
||||
$this->RegisterAttributeString(self::ATTR_CUSTOM_END, $today);
|
||||
$this->RegisterAttributeString('ColorConfiguration', '{}');
|
||||
|
||||
$visualizationType = version_compare(IPS_GetKernelVersion(), '9.0', '>=')
|
||||
? 2
|
||||
@@ -53,6 +71,10 @@ class Energiediagramm extends IPSModule
|
||||
{
|
||||
parent::ApplyChanges();
|
||||
|
||||
if ($this->synchronizeColorProperties()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$today = date('Y-m-d');
|
||||
$defaultRange = $this->ReadPropertyString('Standardzeitraum');
|
||||
if (!in_array($defaultRange, ['day', 'week', 'month', 'year', 'total'], true)) {
|
||||
@@ -84,6 +106,51 @@ class Energiediagramm extends IPSModule
|
||||
$this->recalculateAndPush();
|
||||
}
|
||||
|
||||
private function synchronizeColorProperties(): bool
|
||||
{
|
||||
$previous = json_decode($this->ReadAttributeString('ColorConfiguration'), true);
|
||||
$previous = is_array($previous) ? $previous : [];
|
||||
$state = [];
|
||||
$updates = [];
|
||||
|
||||
foreach (self::COLOR_PROPERTIES as $property => $default) {
|
||||
$legacy = $this->ReadPropertyString($property);
|
||||
$rgb = $this->ReadPropertyInteger($property . 'RGB');
|
||||
$old = $previous[$property] ?? null;
|
||||
$rgbChanged = $rgb !== ($old['rgb'] ?? (int) hexdec(substr($default, 1)));
|
||||
$legacyChanged = $legacy !== ($old['legacy'] ?? null);
|
||||
|
||||
// The native picker wins simultaneous edits; old scripts can still set hex strings.
|
||||
if ($rgbChanged && $rgb >= 0 && $rgb <= 0xFFFFFF) {
|
||||
$legacy = sprintf('#%06X', $rgb);
|
||||
} elseif (!$rgbChanged && $legacyChanged) {
|
||||
$normalized = EnergyChartCalculator::normalizeColor($legacy, '');
|
||||
if ($normalized !== '') {
|
||||
$rgb = (int) hexdec(substr($normalized, 1));
|
||||
}
|
||||
}
|
||||
|
||||
if ($legacy !== $this->ReadPropertyString($property)) {
|
||||
$updates[$property] = $legacy;
|
||||
}
|
||||
if ($rgb !== $this->ReadPropertyInteger($property . 'RGB')) {
|
||||
$updates[$property . 'RGB'] = $rgb;
|
||||
}
|
||||
$state[$property] = ['legacy' => $legacy, 'rgb' => $rgb];
|
||||
}
|
||||
|
||||
foreach ($updates as $property => $value) {
|
||||
IPS_SetProperty($this->InstanceID, $property, $value);
|
||||
}
|
||||
$this->WriteAttributeString('ColorConfiguration', json_encode($state, JSON_THROW_ON_ERROR));
|
||||
if ($updates === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
IPS_ApplyChanges($this->InstanceID);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function GetVisualizationTile(): string
|
||||
{
|
||||
$path = __DIR__ . '/module.html';
|
||||
@@ -277,6 +344,18 @@ class Energiediagramm extends IPSModule
|
||||
$this->ReadPropertyString('FarbeAutarkie'),
|
||||
self::COLOR_AUTARKY
|
||||
),
|
||||
'background' => EnergyChartCalculator::normalizeColor(
|
||||
$this->ReadPropertyString('FarbeHintergrund'),
|
||||
self::COLOR_BACKGROUND
|
||||
),
|
||||
'backgroundAccent1' => EnergyChartCalculator::normalizeColor(
|
||||
$this->ReadPropertyString('FarbeHintergrundAkzent1'),
|
||||
self::COLOR_BACKGROUND_ACCENT_1
|
||||
),
|
||||
'backgroundAccent2' => EnergyChartCalculator::normalizeColor(
|
||||
$this->ReadPropertyString('FarbeHintergrundAkzent2'),
|
||||
self::COLOR_BACKGROUND_ACCENT_2
|
||||
),
|
||||
],
|
||||
'values' => EnergyChartCalculator::metrics(0.0, 0.0, 0.0, null, $sharesAvailable),
|
||||
];
|
||||
@@ -435,6 +514,14 @@ class Energiediagramm extends IPSModule
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (self::COLOR_PROPERTIES as $property => $default) {
|
||||
$rgb = $this->ReadPropertyInteger($property . 'RGB');
|
||||
if ($rgb < 0 || $rgb > 0xFFFFFF
|
||||
|| EnergyChartCalculator::normalizeColor($this->ReadPropertyString($property), '') === '') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->ReadPropertyFloat('Zaehlerfaktor') > 0.0
|
||||
&& $this->ReadPropertyInteger('Aktualisierungsintervall') >= 10
|
||||
&& $this->ReadPropertyInteger('Aktualisierungsintervall') <= 3600
|
||||
@@ -442,15 +529,7 @@ class Energiediagramm extends IPSModule
|
||||
$this->ReadPropertyString('Standardzeitraum'),
|
||||
['day', 'week', 'month', 'year', 'total'],
|
||||
true
|
||||
)
|
||||
&& EnergyChartCalculator::normalizeColor(
|
||||
$this->ReadPropertyString('FarbeEigenverbrauch'),
|
||||
''
|
||||
) !== ''
|
||||
&& EnergyChartCalculator::normalizeColor(
|
||||
$this->ReadPropertyString('FarbeAutarkie'),
|
||||
''
|
||||
) !== '';
|
||||
);
|
||||
}
|
||||
|
||||
private function requiredSourceIdsAreEmpty(): bool
|
||||
|
||||
Reference in New Issue
Block a user