8000
Skip to content

[climate] Save 48 bytes per entity by conditionally compiling visual overrides - #12406

Merged
kbx81 merged 1 commit into
devfrom
visual_overrides_rarely_used
Dec 11, 2025
Merged

[climate] Save 48 bytes per entity by conditionally compiling visual overrides#12406
kbx81 merged 1 commit into
devfrom
visual_overrides_rarely_used

Conversation

@bdraco
@bdraco bdraco commented Dec 10, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

Conditionally compile climate visual override fields to save 24 bytes per climate entity when the feature is unused.

The visual: config option (min/max temperature, temperature step, min/max humidity overrides) has been present since the original climate implementation in April 2019, but is rarely used. Most climate configurations don't override the visual limits reported by the device.

Changes

  1. Replace optional<float> with float using NAN as sentinel (saves 4 bytes per field due to alignment padding)
  2. Wrap the 6 visual override fields with #ifdef USE_CLIMATE_VISUAL_OVERRIDES
  3. Only define USE_CLIMATE_VISUAL_OVERRIDES when at least one climate uses the visual: config

Memory savings

Before: 6 × optional<float> = 48 bytes per climate entity (always allocated)

After:

  • Without visual overrides: 0 bytes (fields conditionally compiled out) → 48 bytes saved
  • With visual overrides: 6 × float = 24 bytes → 24 bytes saved

Most users don't use visual: overrides, so they save the full 48 bytes per climate entity.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

  • N/A

Pull request in esphome-docs with documentation (if applicable):

  • N/A (no user-facing changes)

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • nRF52840

Example entry for config.yaml:

# No change to user config - feature works identically
# Visual overrides are optional and rarely used:
climate:
  - platform: thermostat
    # ...
    visual:
      min_temperature: 16
      max_temperature: 30
      temperature_step:
        target_temperature: 0.5
        current_temperature: 0.1

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are 8000 added/changed:

  • Documentation added/updated in esphome-docs. (N/A - no user-facing changes)

@github-actions
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#12406
    components: [climate]
    refresh: 1h

(Added by the PR bot)

@codecov-commenter
codecov-commenter commented Dec 10, 2025
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.58%. Comparing base (d1d376e) to head (a3017ca).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #12406      +/-   ##
==========================================
+ Coverage   72.56%   72.58%   +0.02%     
==========================================
  Files          53       53              
  Lines       11192    11192              
  Branches     1517     1517              
==========================================
+ Hits         8121     8124       +3     
+ Misses       2677     2675       -2     
+ Partials      394      393       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Copy Markdown
Contributor

Memory Impact Analysis

Components: climate
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 29,292 bytes 29,292 bytes ➡️ +0 bytes (0.00%)
Flash 283,831 bytes 283,735 bytes 📉 ✅ -96 bytes (-0.03%)
📊 Component Memory Breakdown
Component Target Flash PR Flash Change
[esphome]climate 3,636 bytes 3,566 bytes 📉 ✅ -70 bytes (-1.93%)
[esphome]bang_bang 1,652 bytes 1,630 bytes 📉 ✅ -22 bytes (-1.33%)
🔍 Symbol-Level Changes (click to expand)

Changed Symbols

Symbol Target Size PR Size Change
esphome::climate::Climate::get_traits() 99 bytes 29 bytes 📉 -70 bytes (-70.71%)
esphome::bang_bang::BangBangClimate::BangBangClimate() 201 bytes 183 bytes 📉 -18 bytes (-8.96%)
non-virtual thunk to esphome::bang_bang::BangBangClimate::dump_config() 11 bytes 9 bytes 📉 -2 bytes (-18.18%)
non-virtual thunk to esphome::bang_bang::BangBangClimate::setup() 11 bytes 9 bytes 📉 -2 bytes (-18.18%)

Note: This analysis measures static RAM and Flash usage only (compile-time allocation).
Dynamic memory (heap) cannot be measured automatically.
⚠️ You must test this PR on a real device to measure free heap and ensure no runtime memory issues.

This analysis runs automatically when components change. Memory usage is measured from a representative test configuration.

@bdraco
bdraco marked this pull request as ready for review December 10, 2025 21:22
@bdraco
bdraco requested a review from a team as a code owner December 10, 2025 21:22
Copilot AI review requested due to automatic review settings December 10, 2025 21:22
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes memory usage for climate entities by conditionally compiling visual override fields, saving 24-48 bytes per climate entity depending on whether visual overrides are used. The visual override feature allows users to customize the min/max temperature/humidity ranges and temperature steps displayed to frontends, but is rarely used in practice.

Key Changes

  • Replaced optional<float> with float using NAN as sentinel value (saves 4 bytes per field due to alignment)
  • Wrapped 6 visual override fields and their setter methods with #ifdef USE_CLIMATE_VISUAL_OVERRIDES
  • Updated Python code generation to define USE_CLIMATE_VISUAL_OVERRIDES when any visual override is configured

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
esphome/core/defines.h Added USE_CLIMATE_VISUAL_OVERRIDES define for IDE/static analysis (auto-generated file)
esphome/components/climate/climate.h Guarded visual override setter methods and member fields with #ifdef USE_CLIMATE_VISUAL_OVERRIDES
esphome/components/climate/climate.cpp Guarded setter implementations and get_traits() override logic with #ifdef, replaced has_value() checks with std::isnan()
esphome/components/climate/init.py Added cg.add_define("USE_CLIMATE_VISUAL_OVERRIDES") when any visual config is used

@kbx81 kbx81 left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🍻

@kbx81
kbx81 merged commit 369cc70 into dev Dec 11, 2025
47 checks passed
@kbx81
kbx81 deleted the visual_overrides_rarely_used branch December 11, 2025 01:10
@bdraco
bdraco commented Dec 11, 2025
Copy link
Copy Markdown
Member Author

Thanks

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0