From b264c6caaca2ec80b2e343721e286a10807f077f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 7 Nov 2025 18:16:22 -0600 Subject: [PATCH 1/3] cleanup defines --- .../components/alarm_control_panel/alarm_control_panel.cpp | 7 ++++--- esphome/components/cover/cover.cpp | 4 +++- esphome/components/light/light_state.cpp | 3 +-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.cpp b/esphome/components/alarm_control_panel/alarm_control_panel.cpp index febd00f2c5..c29e02c8ef 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.cpp +++ b/esphome/components/alarm_control_panel/alarm_control_panel.cpp @@ -1,8 +1,9 @@ -#include -#include "esphome/core/defines.h" - #include "alarm_control_panel.h" +#include "esphome/core/defines.h" #include "esphome/core/controller_registry.h" + +#include + #include "esphome/core/application.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" diff --git a/esphome/components/cover/cover.cpp b/esphome/components/cover/cover.cpp index ab800a8749..3062dba28a 100644 --- a/esphome/components/cover/cover.cpp +++ b/esphome/components/cover/cover.cpp @@ -1,7 +1,9 @@ #include "cover.h" #include "esphome/core/defines.h" -#include #include "esphome/core/controller_registry.h" + +#include + #include "esphome/core/log.h" namespace esphome { diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index 5bb9087c5b..4c253ec5a8 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -1,9 +1,8 @@ +#include "light_state.h" #include "esphome/core/defines.h" #include "esphome/core/controller_registry.h" #include "esphome/core/log.h" - #include "light_output.h" -#include "light_state.h" #include "transformers.h" namespace esphome { From 7e96f10a79b3cd573ffcf7a4790c634eed7af582 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 08:39:23 -0600 Subject: [PATCH 2/3] dry --- esphome/core/controller_registry.cpp | 120 +++++++-------------------- 1 file changed, 30 insertions(+), 90 deletions(-) diff --git a/esphome/core/controller_registry.cpp b/esphome/core/controller_registry.cpp index b22ec487d5..993a0dc241 100644 --- a/esphome/core/controller_registry.cpp +++ b/esphome/core/controller_registry.cpp @@ -10,151 +10,88 @@ StaticVector ControllerRegistry::controll void ControllerRegistry::register_controller(Controller *controller) { controllers.push_back(controller); } -#ifdef USE_BINARY_SENSOR -void ControllerRegistry::notify_binary_sensor_update(binary_sensor::BinarySensor *obj) { - for (auto *controller : controllers) { - controller->on_binary_sensor_update(obj); +// Macro for registry notification dispatch - iterates registered controllers and calls their handler +#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) { \ + controller->on_##entity_name##_update(obj); \ + } \ } -} + +#ifdef USE_BINARY_SENSOR +CONTROLLER_REGISTRY_NOTIFY(binary_sensor::BinarySensor, binary_sensor) #endif #ifdef USE_FAN -void ControllerRegistry::notify_fan_update(fan::Fan *obj) { - for (auto *controller : controllers) { - controller->on_fan_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(fan::Fan, fan) #endif #ifdef USE_LIGHT -void ControllerRegistry::notify_light_update(light::LightState *obj) { - for (auto *controller : controllers) { - controller->on_light_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(light::LightState, light) #endif #ifdef USE_SENSOR -void ControllerRegistry::notify_sensor_update(sensor::Sensor *obj) { - for (auto *controller : controllers) { - controller->on_sensor_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(sensor::Sensor, sensor) #endif #ifdef USE_SWITCH -void ControllerRegistry::notify_switch_update(switch_::Switch *obj) { - for (auto *controller : controllers) { - controller->on_switch_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(switch_::Switch, switch) #endif #ifdef USE_COVER -void ControllerRegistry::notify_cover_update(cover::Cover *obj) { - for (auto *controller : controllers) { - controller->on_cover_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(cover::Cover, cover) #endif #ifdef USE_TEXT_SENSOR -void ControllerRegistry::notify_text_sensor_update(text_sensor::TextSensor *obj) { - for (auto *controller : controllers) { - controller->on_text_sensor_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(text_sensor::TextSensor, text_sensor) #endif #ifdef USE_CLIMATE -void ControllerRegistry::notify_climate_update(climate::Climate *obj) { - for (auto *controller : controllers) { - controller->on_climate_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(climate::Climate, climate) #endif #ifdef USE_NUMBER -void ControllerRegistry::notify_number_update(number::Number *obj) { - for (auto *controller : controllers) { - controller->on_number_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(number::Number, number) #endif #ifdef USE_DATETIME_DATE -void ControllerRegistry::notify_date_update(datetime::DateEntity *obj) { - for (auto *controller : controllers) { - controller->on_date_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(datetime::DateEntity, date) #endif #ifdef USE_DATETIME_TIME -void ControllerRegistry::notify_time_update(datetime::TimeEntity *obj) { - for (auto *controller : controllers) { - controller->on_time_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(datetime::TimeEntity, time) #endif #ifdef USE_DATETIME_DATETIME -void ControllerRegistry::notify_datetime_update(datetime::DateTimeEntity *obj) { - for (auto *controller : controllers) { - controller->on_datetime_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(datetime::DateTimeEntity, datetime) #endif #ifdef USE_TEXT -void ControllerRegistry::notify_text_update(text::Text *obj) { - for (auto *controller : controllers) { - controller->on_text_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(text::Text, text) #endif #ifdef USE_SELECT -void ControllerRegistry::notify_select_update(select::Select *obj) { - for (auto *controller : controllers) { - controller->on_select_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(select::Select, select) #endif #ifdef USE_LOCK -void ControllerRegistry::notify_lock_update(lock::Lock *obj) { - for (auto *controller : controllers) { - controller->on_lock_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(lock::Lock, lock) #endif #ifdef USE_VALVE -void ControllerRegistry::notify_valve_update(valve::Valve *obj) { - for (auto *controller : controllers) { - controller->on_valve_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(valve::Valve, valve) #endif #ifdef USE_MEDIA_PLAYER -void ControllerRegistry::notify_media_player_update(media_player::MediaPlayer *obj) { - for (auto *controller : controllers) { - controller->on_media_player_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(media_player::MediaPlayer, media_player) #endif #ifdef USE_ALARM_CONTROL_PANEL -void ControllerRegistry::notify_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) { - for (auto *controller : controllers) { - controller->on_alarm_control_panel_update(obj); - } -} +CONTROLLER_REGISTRY_NOTIFY(alarm_control_panel::AlarmControlPanel, alarm_control_panel) #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); @@ -163,6 +100,7 @@ void ControllerRegistry::notify_event(event::Event *obj) { #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); @@ -170,6 +108,8 @@ void ControllerRegistry::notify_update(update::UpdateEntity *obj) { } #endif +#undef CONTROLLER_REGISTRY_NOTIFY + } // namespace esphome #endif // USE_CONTROLLER_REGISTRY From 62f43d3353ee2b399023e205b360310a281e215e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 08:41:46 -0600 Subject: [PATCH 3/3] 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