Skip to content

Commit

Permalink
Allow missing keys in IO device's config homeScreen
Browse files Browse the repository at this point in the history
fix #915
  • Loading branch information
fracz committed Oct 30, 2024
1 parent 9dda940 commit 1bf16b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,28 @@ public function setConfig(IODevice $device, array $config): void {
Assert::that($value, null, 'automaticTimeSync')->boolean();
}
if ($settingName === 'homeScreen') {
Assert::that($value, null, 'homeScreen')->isArray()->keyExists('content')->keyExists('offDelay');
$homeScreenConfig = [];
Assert::that($value, null, 'homeScreen')->isArray();
Assertion::allInArray(array_keys($value), ['content', 'offDelay', 'offDelayType'], null, 'homeScreen');
$contentAvailable = $device->getProperties()['homeScreenContentAvailable'] ?? [];
$hasOffDelay = ($currentConfig['homeScreen']['offDelay'] ?? false) !== false;
$hasDelayType = $currentConfig['homeScreen']['offDelayType'] ?? false;
Assertion::count($value, $hasDelayType ? 3 : 2, null, 'homeScreen');
$availableModes = $device->getProperties()['homeScreenContentAvailable'] ?? [];
Assertion::inArray($value['content'], $availableModes, null, 'homeScreen.content');
Assert::that($value['offDelay'], null, 'homeScreen.offDelay')->integer()->between(0, 3600);
if ($contentAvailable) {
Assert::that($value, null, 'homeScreen.content')->keyExists('content');
Assertion::inArray($value['content'], $contentAvailable, null, 'homeScreen.content');
$homeScreenConfig['content'] = $value['content'];
}
if ($hasOffDelay) {
Assert::that($value, null, 'homeScreen.offDelay')->keyExists('offDelay');
Assert::that($value['offDelay'], null, 'homeScreen.offDelay')->integer()->between(0, 3600);
$homeScreenConfig['offDelay'] = $value['offDelay'];
}
if ($hasDelayType) {
Assertion::keyExists($value, 'offDelayType', null, 'homeScreen');
Assertion::keyExists($value, 'offDelayType', null, 'homeScreen.offDelayType');
Assertion::inArray($value['offDelayType'], ['ALWAYS_ENABLED', 'ENABLED_WHEN_DARK'], null, 'homeScreen.offDelayType');
$homeScreenConfig['offDelayType'] = $value['offDelayType'];
}
$value = $homeScreenConfig;
}
$device->setUserConfigValue($settingName, $value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testValidConfigs(array $config) {
'buttonVolume' => 14,
'userInterface' => ['disabled' => false],
'automaticTimeSync' => false,
'homeScreen' => [],
'homeScreen' => ['content' => 'NONE', 'offDelay' => 0],
]);
EntityUtils::setField($device, 'properties', json_encode([
'homeScreenContentAvailable' => ["NONE", "TEMPERATURE", "HUMIDITY", "TIME", "TIME_DATE"],
Expand Down Expand Up @@ -124,7 +124,6 @@ public function invalidConfigs() {
[['homeScreen' => ['offDelay' => 10000]]],
[['homeScreen' => ['content' => 'NONE', 'offDelay' => 1000, 'extra' => 'unicorn']]],
[['homeScreen' => 2]],
[['homeScreen' => ['content' => 'NONE', 'offDelay' => 100, 'offDelayType' => 'ALWAYS_ENABLED']]],
];
}

Expand Down Expand Up @@ -166,7 +165,7 @@ public function testSettingUserInterfacePartialWithoutTemperatures() {

public function testSettingHomeScreenOffDelayType() {
$device = new IODevice();
$device->setUserConfig(['homeScreen' => ['offDelayType' => 'ALWAYS_ENABLED']]);
$device->setUserConfig(['homeScreen' => ['content' => 'NONE', 'offDelay' => 100, 'offDelayType' => 'ALWAYS_ENABLED']]);
EntityUtils::setField($device, 'properties', json_encode([
'homeScreenContentAvailable' => ["NONE", "TEMPERATURE", "HUMIDITY", "TIME", "TIME_DATE"],
]));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div>
<div class="form-group">
<div class="form-group" v-if="config.homeScreenContentAvailable && config.homeScreenContentAvailable.length">
<label for="homeScreen">{{ $t('Home screen content') }}</label>
<!-- i18n:["homeScreenContent_NONE", "homeScreenContent_TEMPERATURE"] -->
<!-- i18n:["homeScreenContent_TEMPERATURE_AND_HUMIDITY"] -->
Expand All @@ -12,7 +12,7 @@
</option>
</select>
</div>
<div class="form-group">
<div class="form-group" v-if="value.offDelayType !== undefined">
<label>{{ $t('Automatic front panel turn off') }}</label>
<div>
<div class="btn-group">
Expand All @@ -33,7 +33,7 @@
</div>
</div>
<transition-expand>
<div class="form-group mt-2" v-if="homeScreenOffMode !== 'DISABLE'">
<div class="form-group mt-2" v-if="homeScreenOffMode !== 'DISABLE' && value.offDelay !== undefined">
<label>{{ $t('Automatic front panel turn off after') }}</label>
<div class="mt-3 mb-6">
<VueSlider v-model="offDelay" @change="onChange()" tooltip="always"
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/devices/details/device-settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
tooltip-placement="bottom" class="green"/>
</div>
</div>
<div class="form-group with-border-bottom"
v-if="config.homeScreen !== undefined && config.homeScreenContentAvailable && config.homeScreenContentAvailable.length">
<div class="form-group with-border-bottom" v-if="config.homeScreen !== undefined">
<DeviceSettingsHomeScreen v-model="config.homeScreen" :config="config" @input="onChange()"/>
</div>
<div class="form-group with-border-bottom" v-if="config.userInterface">
Expand Down

0 comments on commit 1bf16b1

Please sign in to comment.