From 62f43d3353ee2b399023e205b360310a281e215e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 08:41:46 -0600 Subject: [PATCH] dry --- esphome/core/controller_registry.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/esphome/core/controller_registry.cpp b/esphome/core/controller_registry.cpp index 993a0dc241..0a84bb0d0d 100644 --- a/esphome/core/controller_registry.cpp +++ b/esphome/core/controller_registry.cpp @@ -10,7 +10,7 @@ StaticVector ControllerRegistry::controll void ControllerRegistry::register_controller(Controller *controller) { controllers.push_back(controller); } -// Macro for registry notification dispatch - iterates registered controllers and calls their handler +// Macro for standard registry notification dispatch - calls on__update() #define CONTROLLER_REGISTRY_NOTIFY(entity_type, entity_name) \ void ControllerRegistry::notify_##entity_name##_update(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \ for (auto *controller : controllers) { \ @@ -18,6 +18,14 @@ void ControllerRegistry::register_controller(Controller *controller) { controlle } \ } +// Macro for entities where controller method has no "_update" suffix (Event, Update) +#define CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX(entity_type, entity_name) \ + void ControllerRegistry::notify_##entity_name(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \ + for (auto *controller : controllers) { \ + controller->on_##entity_name(obj); \ + } \ + } + #ifdef USE_BINARY_SENSOR CONTROLLER_REGISTRY_NOTIFY(binary_sensor::BinarySensor, binary_sensor) #endif @@ -91,24 +99,15 @@ CONTROLLER_REGISTRY_NOTIFY(alarm_control_panel::AlarmControlPanel, alarm_control #endif #ifdef USE_EVENT -// Event is a special case - notify_event() calls on_event() (no "_update" suffix) -void ControllerRegistry::notify_event(event::Event *obj) { - for (auto *controller : controllers) { - controller->on_event(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX(event::Event, event) #endif #ifdef USE_UPDATE -// Update is a special case - notify_update() calls on_update() (no "_update" suffix) -void ControllerRegistry::notify_update(update::UpdateEntity *obj) { - for (auto *controller : controllers) { - controller->on_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX(update::UpdateEntity, update) #endif #undef CONTROLLER_REGISTRY_NOTIFY +#undef CONTROLLER_REGISTRY_NOTIFY_NO_UPDATE_SUFFIX } // namespace esphome