Skip to content

[dsmr] Add deprecated std::string overload for set_decryption_key#14180

Merged
bdraco merged 2 commits intodevfrom
dsmr-deprecated-string-overload
Feb 21, 2026
Merged

[dsmr] Add deprecated std::string overload for set_decryption_key#14180
bdraco merged 2 commits intodevfrom
dsmr-deprecated-string-overload

Conversation

@bdraco
Copy link
Member

@bdraco bdraco commented Feb 21, 2026

What does this implement/fix?

Adds a deprecated std::string overload for set_decryption_key() to ease the transition for users who were calling this method from lambdas with a std::string argument.

The const char * signature was introduced in #13375 to avoid heap allocation. While set_decryption_key() is not documented at esphome.io and therefore not public API per our policy, adding this overload gives users a deprecation warning with a clear migration path instead of a hard compile error.

The overload will be removed in 2026.8.0.

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):

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

  • N/A

Test Environment

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

Example entry for config.yaml:

# No config change needed - this restores compatibility for lambda usage:
# id(dsmr_instance).set_decryption_key(key);
# Users will see a deprecation warning suggesting:
# id(dsmr_instance).set_decryption_key(key.c_str());

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 added/changed:

@bdraco bdraco requested a review from glmnet as a code owner February 21, 2026 14:36
Copilot AI review requested due to automatic review settings February 21, 2026 14:36
@github-actions
Copy link
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#14180
    components: [dsmr]
    refresh: 1h

(Added by the PR bot)

@github-actions
Copy link
Contributor

👋 Hi there! This PR modifies 1 file(s) with codeowners.

@glmnet, @PolarGoose, @zuidwijk - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

@github-actions
Copy link
Contributor

Memory Impact Analysis

Components: dsmr
Platform: esp8266-ard

Metric Target Branch This PR Change
RAM 30,004 bytes 30,052 bytes 📈 🔸 +48 bytes (+0.16%)
Flash 295,755 bytes 296,271 bytes 📈 🔸 +516 bytes (+0.17%)
📊 Component Memory Breakdown
Component Target Flash PR Flash Change
[esphome]core 7,351 bytes 7,602 bytes 📈 🚨 +251 bytes (+3.41%)
app_framework 1,872 bytes 2,037 bytes 📈 +165 bytes (+8.81%)
rom_functions 7,388 bytes 7,406 bytes 📈 +18 bytes (+0.24%)
🔍 Symbol-Level Changes (click to expand)

Changed Symbols

Symbol Target Size PR Size Change
setup 497 bytes 622 bytes 📈 +125 bytes (+25.15%)
esphome::App 156 bytes 160 bytes 📈 +4 bytes (+2.56%)
[global constructor for: esphome::Application::register_component_(esphome::Component*)] 127 bytes 129 bytes 📈 +2 bytes (+1.57%)

New Symbols (top 15)

Symbol Size
esphome::Action<>::play_complex() 61 bytes
vtable for esphome::StartupTrigger 56 bytes
esphome::Action<>::stop_complex() 53 bytes
setup::{lambda()#1}::_FUN() 40 bytes
vtable for esphome::StatelessLambdaAction<> 28 bytes
esphome::Action<>::is_running() 22 bytes
esphome::StartupTrigger::setup() 18 bytes
esphome::StatelessLambdaAction<>::play() 5 bytes
dsmr_instance 4 bytes
esphome::StartupTrigger::get_setup_priority() const 4 bytes
startuptrigger_id 4 bytes
automation_id 4 bytes
esphome::Action<>::stop() 2 bytes

Removed Symbols (top 15)

Symbol Size
esphome_dsmr_dsmr_id 4 bytes

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.

Copy link
Contributor

Copilot AI left a comment

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 adds a deprecated std::string overload for the set_decryption_key() method in the DSMR component to provide a graceful migration path for users who were broken by the API change in 2026.2.0 (PR #13375). The original change switched from const std::string & to const char * to avoid heap allocation, but broke existing lambda usage without a deprecation period.

Changes:

  • Adds a deprecated inline std::string overload that forwards to the const char* version with a 6-month removal timeline (2026.8.0)
  • Adds test coverage to verify the deprecated overload compiles correctly in lambda contexts

Reviewed changes

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

File Description
esphome/components/dsmr/dsmr.h Adds deprecated inline std::string overload with ESPDEPRECATED macro and clear migration message
tests/components/dsmr/common.yaml Adds test case for deprecated overload in lambda context to ensure it compiles

@codecov-commenter
Copy link

codecov-commenter commented Feb 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.09%. Comparing base (6ecb01d) to head (cd0c4a6).
⚠️ Report is 8 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #14180      +/-   ##
==========================================
- Coverage   74.11%   74.09%   -0.03%     
==========================================
  Files          55       55              
  Lines       11591    11591              
  Branches     1578     1578              
==========================================
- Hits         8591     8588       -3     
- Misses       2598     2600       +2     
- Partials      402      403       +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.

@PolarGoose
Copy link
Contributor

PolarGoose commented Feb 21, 2026

I already asked this question in the previous pull request when the void set_decryption_key(const char *decryption_key) was introduced, sorry for asking it again, but given the circumstances maybe it worth to look at it again:

Are you sure you don't want to use std::string_view? Then the existing client's lambdas will still compile, and you get your performance benefits.

@bdraco
Copy link
Member Author

bdraco commented Feb 21, 2026

Thanks for the suggestion! We intentionally avoid std::string_view in this codebase; it pulls in a significant amount of STL template machinery that bloats flash on these resource-constrained embedded targets. Where we need a view type, we use our own lightweight StringRef instead. const char * is the right fit here since all callers pass either string literals (from codegen) or null-terminated strings from std::string::c_str(). Additionally, set_decryption_key() isn't documented at esphome.io and isn't public API per our policy; this deprecated overload is a courtesy to ease the transition, not a commitment to support the std::string signature long-term.

That said, as a codeowner of this component, if you'd like to keep the std::string overload permanently and document set_decryption_key() as a supported API in a follow-up PR, that's fine too. We don't disallow allocating patterns when users explicitly opt into them; we just don't want to force them into that pattern by default. However, it's better to document and push users toward the non-allocating pattern. This particular case isn't one where it makes a big difference, but the effect is cumulative and that's how we end up with heap fragmentation and crashing devices.

@PolarGoose
Copy link
Contributor

We intentionally avoid std::string_view in this codebase; it pulls in a significant amount of STL template machinery

Ok. Agreed.
Also, I agree with your other points.
I'm fine with deprecating the std::string overload. The less code, the better.

@bdraco
Copy link
Member Author

bdraco commented Feb 21, 2026

thanks

@bdraco bdraco merged commit 7fb09da into dev Feb 21, 2026
47 checks passed
@bdraco bdraco deleted the dsmr-deprecated-string-overload branch February 21, 2026 17:08
@github-actions github-actions bot locked and limited conversation to collaborators Feb 23, 2026
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.

[dsmr] Breaking change in 2026.2.0: set_decryption_key() no longer accepts std::string in lambda

6 participants