[thermostat] Keep presets in shared flash tables (#20080)

This commit is contained in:
J. Nick Koston
2026-10-11 15:04:16 -10:00
committed by GitHub
parent 9295342c16
commit 993fd8d7a2
6 changed files with 189 additions and 110 deletions
@@ -0,0 +1,53 @@
---
esphome:
name: test
logger:
esp32:
board: esp32dev
sensor:
- platform: template
id: temp_sensor
lambda: return 21.0;
climate:
- platform: thermostat
id: thermo_a
name: Thermo A
sensor: temp_sensor
min_heating_off_time: 300s
min_heating_run_time: 300s
min_idle_time: 30s
heat_action:
- logger.log: heat
idle_action:
- logger.log: idle
preset:
- name: Away
default_target_temperature_low: 15°C
mode: HEAT
- name: Eco
default_target_temperature_low: 17°C
- name: Night Mode
default_target_temperature_low: 16°C
- platform: thermostat
id: thermo_b
name: Thermo B
sensor: temp_sensor
min_heating_off_time: 300s
min_heating_run_time: 300s
min_idle_time: 30s
heat_action:
- logger.log: heat
idle_action:
- logger.log: idle
preset:
- name: Away
default_target_temperature_low: 15°C
mode: HEAT
- name: Eco
default_target_temperature_low: 17°C
- name: Night Mode
default_target_temperature_low: 16°C
@@ -0,0 +1,28 @@
"""Tests for the thermostat preset codegen."""
from collections.abc import Callable
from pathlib import Path
import re
def test_presets_shared_progmem_tables(
generate_main: Callable[[str | Path], str],
component_config_path: Callable[[str], Path],
) -> None:
"""Thermostats with the same presets share one PROGMEM table per kind."""
main_cpp = generate_main(component_config_path("presets.yaml"))
standard = re.findall(
r"static constexpr thermostat::ThermostatPresetEntry (\w+)\[\] PROGMEM",
main_cpp,
)
custom = re.findall(
r"static constexpr thermostat::ThermostatCustomPresetEntry (\w+)\[\] PROGMEM",
main_cpp,
)
assert len(standard) == 1
assert len(custom) == 1
for var in ("thermo_a", "thermo_b"):
assert f"{var}->set_preset_config({standard[0]}, 2);" in main_cpp
assert f"{var}->set_custom_preset_config({custom[0]}, 1);" in main_cpp
assert "climate::CLIMATE_MODE_HEAT" in main_cpp