sec_touch: research multiple listeners per property ID / duplicate poll IDs

Open
#21 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Active
Tech stack
cpp
Domain
embedded-iot

Research direction

Read components/sec_touch/sec_touch.h and components/sec_touch/sec_touch.cpp, especially the listener maps and register_* functions, then trace the example registration in components/sec_touch/switch/sec_touch_summer_ventilation_switch.cpp. Compare the proposed options for duplicate listeners and poll IDs, including RAM and flash costs and whether register_fan needs the same rule. Done means choosing and implementing a consistent policy, with duplicates handled as intended.

Written by the indexing model from the issue text.

Description

question

Problem

Property listeners in sec_touch hold one callback per property ID, and registration does not de-duplicate the poll ID lists.

  • The listener maps are std::map<int, UpdateCallbackListener>: recursive_update_listeners and manual_update_listeners (sec_touch.h#L78-L84). PR #20 adds a third one, string_update_listeners (std::map<int, StringUpdateCallbackListener>), with the same pattern.
  • Each register_* function (sec_touch.cpp#L295-L311, plus register_global_setting_string_listener in PR #20) does map[property_id] = std::move(listener) and then push_back(property_id) onto fan_level_property_ids_, fan_label_property_ids_ or global_setting_property_ids_.

When two entities register for the same property ID:

  1. The second listener silently replaces the first. The first entity never gets updates.
  2. The ID appears twice in the poll list, so it is requested twice per update cycle.

An int listener and a string listener on the same ID live in separate maps, so both fire. That is intended and not part of this issue.

This is a pre-existing pattern on main, not something PR #20 introduced. It came up while reviewing that PR.

Reproduction

switch:
  - platform: sec_touch
    name: "Summer ventilation A"
  - platform: sec_touch
    name: "Summer ventilation B"

Both switches register SUMMER_VENTILATION_PROPERTY_ID via register_global_setting_recursive_listener (sec_touch_summer_ventilation_switch.cpp#L17). Only "B" gets state updates, and the ID is polled twice. With PR #20, two sensor: - platform: sec_touch entries that each set clock_drift hit the same issue on the date/clock IDs.

Open questions

  • Is more than one entity per property ID a realistic use case at all (for example a second entity with different filters or a different name), or is it always a config mistake?
  • Which option fits?
    • Multiple listeners per ID: std::multimap or std::map<int, std::vector<Listener>>, plus de-duplicated ID lists.
    • De-duplicate the ID lists only: fixes the double poll but keeps last-wins listeners.
    • Reject duplicates in Python config validation with a clear error. This fails at compile time and needs no runtime cost.
    • Log a warning on re-registration: the cheapest change, but it only shows up in the device logs.
  • What does each option cost in ESP RAM and flash? A vector per map entry adds heap allocations per property. Today's configs have a small, fixed number of IDs.
  • Should the same rule apply to register_fan / fans_ (also keyed by level ID)?
Dominant language
C++
Stars
8
Forks
4
Avg merge
15h 58m
Merged PRs (30d)
5

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from distante/esphome-components

All issues in distante/esphome-components

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.