ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
controller_dispatch.h
Go to the documentation of this file.
1#pragma once
2
3// Included once by the generated main.cpp, after it returns the registered controllers as a tuple:
4//
5// static auto esphome_controllers() { return std::tuple{api_apiserver_id, web_server_webserver_id}; }
6// #include "esphome/core/controller_dispatch.h"
7//
8// Defines ControllerRegistry::notify_*() as direct calls on those controllers. Excluded from
9// esphome.h and the clang-tidy all-headers file, so nothing else includes it.
10
11#include <tuple>
12#include <type_traits>
13
15
16namespace esphome {
17
18// NOLINTBEGIN(bugprone-macro-parentheses)
19
21template<typename T>
22concept ControllerContract = requires(T &controller) {
23 controller; // keeps the requirement list non-empty when no entity type has a callback
24#define ENTITY_TYPE_(type, singular, plural, count, upper) // no controller callback
25#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
26 controller.on_##callback(static_cast<type *>(nullptr));
28#undef ENTITY_TYPE_
29#undef ENTITY_CONTROLLER_TYPE_
30};
31
32template<typename... Controllers> constexpr bool controllers_satisfy_contract(std::tuple<Controllers...> *) {
33 return (ControllerContract<std::remove_pointer_t<Controllers>> && ...);
34}
35static_assert(controllers_satisfy_contract(static_cast<decltype(esphome_controllers()) *>(nullptr)),
36 "A registered controller is missing an on_*_update() callback for an entity type in this build "
37 "(ControllerContract in esphome/core/controller_dispatch.h)");
38
39#define ENTITY_TYPE_(type, singular, plural, count, upper) // no controller callback
40#define ENTITY_CONTROLLER_TYPE_(type, singular, plural, count, upper, callback) \
41 void ControllerRegistry::notify_##callback(type *obj) { \
42 std::apply([obj](auto *...controller) { (controller->on_##callback(obj), ...); }, esphome_controllers()); \
43 }
45#undef ENTITY_TYPE_
46#undef ENTITY_CONTROLLER_TYPE_
47
48// NOLINTEND(bugprone-macro-parentheses)
49
50} // namespace esphome
A controller provides a plain on_*_update() member for every entity type in the build.
constexpr bool controllers_satisfy_contract(std::tuple< Controllers... > *)