Skip to content

Commit 1eb7f29

Browse files
committed
fix(config): default display_levels to device preset, not global constant
The display_settings options step fell back to the global DEFAULT_DISPLAY_LEVELS (16) when no value was stored yet, ignoring the device's own preset (e.g. reterminal_e1002 supports 256 levels, trmnl_og only 2). Resolve the preset first and use its display_levels as the fallback default so a fresh entry pre-selects a value the device can actually reproduce.
1 parent fb10225 commit 1eb7f29

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

custom_components/eink_dashboard/config_flow.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,12 @@ async def async_step_display_settings(
10581058
"""Update refresh interval, optimize, and image quality settings."""
10591059
opts = self.config_entry.options
10601060
optimize = opts.get("optimize", DEFAULT_OPTIMIZE)
1061-
display_levels = opts.get("display_levels", DEFAULT_DISPLAY_LEVELS)
1061+
device_model = opts.get("device_model", "")
1062+
preset = DEVICE_PRESETS.get(device_model)
1063+
default_display_levels = (
1064+
preset.display_levels if preset else DEFAULT_DISPLAY_LEVELS
1065+
)
1066+
display_levels = opts.get("display_levels", default_display_levels)
10621067
schema_fields: dict = {
10631068
vol.Required(
10641069
"update_interval",
@@ -1087,8 +1092,6 @@ async def async_step_display_settings(
10871092
return self.async_create_entry(
10881093
data={**opts, **validated, **section},
10891094
)
1090-
device_model = opts.get("device_model", "")
1091-
preset = DEVICE_PRESETS.get(device_model)
10921095
if preset and preset.integration_dithers:
10931096
optimize_note = (
10941097
"This device's Home Assistant integration handles image"

tests/test_config_flow.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,52 @@ async def test_display_settings_has_top_level_display_levels(
873873
}
874874
assert "display_levels" in field_names
875875

876+
async def test_display_settings_defaults_to_device_preset_levels(
877+
self, hass: HomeAssistant
878+
) -> None:
879+
# When display_levels has never been saved, the field default
880+
# should come from the device's preset instead of the global
881+
# DEFAULT_DISPLAY_LEVELS constant, so devices like the
882+
# reterminal_e1002 (256 levels) show their own capability
883+
# pre-selected rather than an unrelated fallback.
884+
flow = await _make_options_flow(
885+
hass,
886+
{
887+
"update_interval": 60,
888+
"device_model": "reterminal_e1002",
889+
},
890+
)
891+
result = await flow.async_step_display_settings(None)
892+
893+
markers = {
894+
k.schema: k
895+
for k in result["data_schema"].schema
896+
if hasattr(k, "schema")
897+
}
898+
assert markers["display_levels"].default() == 256
899+
900+
async def test_display_settings_prefers_stored_levels_over_preset(
901+
self, hass: HomeAssistant
902+
) -> None:
903+
# A previously saved display_levels value always wins over the
904+
# device preset's default.
905+
flow = await _make_options_flow(
906+
hass,
907+
{
908+
"update_interval": 60,
909+
"device_model": "reterminal_e1002",
910+
"display_levels": 16,
911+
},
912+
)
913+
result = await flow.async_step_display_settings(None)
914+
915+
markers = {
916+
k.schema: k
917+
for k in result["data_schema"].schema
918+
if hasattr(k, "schema")
919+
}
920+
assert markers["display_levels"].default() == 16
921+
876922
async def test_display_settings_saves_optimize_toggle_on(
877923
self, hass: HomeAssistant
878924
) -> None:

0 commit comments

Comments
 (0)