1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-17 07:15:48 +00:00
This commit is contained in:
J. Nick Koston
2025-11-07 15:44:17 -06:00
parent 295fe8da04
commit f1009a7468
26 changed files with 54 additions and 153 deletions

View File

@@ -1,7 +1,7 @@
#include <utility>
#include "alarm_control_panel.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@@ -34,6 +34,7 @@ void AlarmControlPanel::publish_state(AlarmControlPanelState state) {
LOG_STR_ARG(alarm_control_panel_state_to_string(prev_state)));
this->current_state_ = state;
this->state_callback_.call();
ControllerRegistry::notify_alarm_control_panel_update(this);
if (state == ACP_STATE_TRIGGERED) {
this->triggered_callback_.call();
} else if (state == ACP_STATE_ARMING) {

View File

@@ -348,12 +348,12 @@ API_DISPATCH_UPDATE(media_player::MediaPlayer, media_player)
#endif
#ifdef USE_EVENT
// Event is a special case - it's the only entity that passes extra parameters to the send method
void APIServer::on_event(event::Event *obj, const std::string &event_type) {
// Event is a special case - it reads event_type from obj->last_event_type
void APIServer::on_event(event::Event *obj) {
if (obj->is_internal())
return;
for (auto &c : this->clients_)
c->send_event(obj, event_type);
c->send_event(obj, *obj->last_event_type);
}
#endif

View File

@@ -141,7 +141,7 @@ class APIServer : public Component, public Controller {
void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) override;
#endif
#ifdef USE_EVENT
void on_event(event::Event *obj, const std::string &event_type) override;
void on_event(event::Event *obj) override;
#endif
#ifdef USE_UPDATE
void on_update(update::UpdateEntity *obj) override;

View File

@@ -1,4 +1,5 @@
#include "binary_sensor.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -37,6 +38,7 @@ void BinarySensor::send_state_internal(bool new_state) {
// Note that set_state_ de-dups and will only trigger callbacks if the state has actually changed
if (this->set_state_(new_state)) {
ESP_LOGD(TAG, "'%s': New state is %s", this->get_name().c_str(), ONOFF(new_state));
ControllerRegistry::notify_binary_sensor_update(this);
}
}

View File

@@ -1,4 +1,5 @@
#include "climate.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/macros.h"
namespace esphome {
@@ -463,6 +464,7 @@ void Climate::publish_state() {
// Send state to frontend
this->state_callback_.call(*this);
ControllerRegistry::notify_climate_update(this);
// Save state
this->save_state_();
}

View File

@@ -1,5 +1,6 @@
#include "cover.h"
#include <strings.h>
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -169,6 +170,7 @@ void Cover::publish_state(bool save) {
ESP_LOGD(TAG, " Current Operation: %s", cover_operation_to_str(this->current_operation));
this->state_callback_.call();
ControllerRegistry::notify_cover_update(this);
if (save) {
CoverRestoreState restore{};

View File

@@ -1,5 +1,5 @@
#include "date_entity.h"
#include "esphome/core/controller_registry.h"
#ifdef USE_DATETIME_DATE
#include "esphome/core/log.h"
@@ -32,6 +32,7 @@ void DateEntity::publish_state() {
this->set_has_state(true);
ESP_LOGD(TAG, "'%s': Sending date %d-%d-%d", this->get_name().c_str(), this->year_, this->month_, this->day_);
this->state_callback_.call();
ControllerRegistry::notify_date_update(this);
}
DateCall DateEntity::make_call() { return DateCall(this); }

View File

@@ -1,5 +1,5 @@
#include "datetime_entity.h"
#include "esphome/core/controller_registry.h"
#ifdef USE_DATETIME_DATETIME
#include "esphome/core/log.h"
@@ -48,6 +48,7 @@ void DateTimeEntity::publish_state() {
ESP_LOGD(TAG, "'%s': Sending datetime %04u-%02u-%02u %02d:%02d:%02d", this->get_name().c_str(), this->year_,
this->month_, this->day_, this->hour_, this->minute_, this->second_);
this->state_callback_.call();
ControllerRegistry::notify_datetime_update(this);
}
DateTimeCall DateTimeEntity::make_call() { return DateTimeCall(this); }

View File

@@ -1,5 +1,5 @@
#include "time_entity.h"
#include "esphome/core/controller_registry.h"
#ifdef USE_DATETIME_TIME
#include "esphome/core/log.h"
@@ -29,6 +29,7 @@ void TimeEntity::publish_state() {
ESP_LOGD(TAG, "'%s': Sending time %02d:%02d:%02d", this->get_name().c_str(), this->hour_, this->minute_,
this->second_);
this->state_callback_.call();
ControllerRegistry::notify_time_update(this);
}
TimeCall TimeEntity::make_call() { return TimeCall(this); }

View File

@@ -1,5 +1,5 @@
#include "event.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -23,6 +23,7 @@ void Event::trigger(const std::string &event_type) {
last_event_type = found;
ESP_LOGD(TAG, "'%s' Triggered event '%s'", this->get_name().c_str(), last_event_type->c_str());
this->event_callback_.call(event_type);
ControllerRegistry::notify_event(this);
}
void Event::add_on_event_callback(std::function<void(const std::string &event_type)> &&callback) {

View File

@@ -1,4 +1,5 @@
#include "fan.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -181,6 +182,7 @@ void Fan::publish_state() {
ESP_LOGD(TAG, " Preset Mode: %s", preset);
}
this->state_callback_.call();
ControllerRegistry::notify_fan_update(this);
this->save_state_();
}

View File

@@ -1,3 +1,4 @@
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
#include "light_output.h"
@@ -137,7 +138,10 @@ void LightState::loop() {
float LightState::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
void LightState::publish_state() { this->remote_values_callback_.call(); }
void LightState::publish_state() {
this->remote_values_callback_.call();
ControllerRegistry::notify_light_update(this);
}
LightOutput *LightState::get_output() const { return this->output_; }

View File

@@ -1,4 +1,5 @@
#include "lock.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -53,6 +54,7 @@ void Lock::publish_state(LockState state) {
this->rtc_.save(&this->state);
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), lock_state_to_string(state));
this->state_callback_.call();
ControllerRegistry::notify_lock_update(this);
}
void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }

View File

@@ -1,5 +1,5 @@
#include "media_player.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -148,7 +148,10 @@ void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
this->state_callback_.add(std::move(callback));
}
void MediaPlayer::publish_state() { this->state_callback_.call(); }
void MediaPlayer::publish_state() {
this->state_callback_.call();
ControllerRegistry::notify_media_player_update(this);
}
} // namespace media_player
} // namespace esphome

View File

@@ -1,4 +1,5 @@
#include "number.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -32,6 +33,7 @@ void Number::publish_state(float state) {
this->state = state;
ESP_LOGD(TAG, "'%s': Sending state %f", this->get_name().c_str(), state);
this->state_callback_.call(state);
ControllerRegistry::notify_number_update(this);
}
void Number::add_on_state_callback(std::function<void(float)> &&callback) {

View File

@@ -1,5 +1,5 @@
#include "select.h"
#include "esphome/core/log.h"
#include "esphome/core/controller_registry.h" #include "esphome/core/log.h"
#include <cstring>
namespace esphome {
@@ -33,6 +33,7 @@ void Select::publish_state(size_t index) {
ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", this->get_name().c_str(), option, index);
// Callback signature requires std::string, create temporary for compatibility
this->state_callback_.call(std::string(option), index);
ControllerRegistry::notify_select_update(this);
}
const char *Select::current_option() const { return this->has_state() ? this->option_at(this->active_index_) : ""; }

View File

@@ -1,4 +1,5 @@
#include "sensor.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -131,6 +132,7 @@ void Sensor::internal_send_state_to_frontend(float state) {
ESP_LOGD(TAG, "'%s': Sending state %.5f %s with %d decimals of accuracy", this->get_name().c_str(), state,
this->get_unit_of_measurement_ref().c_str(), this->get_accuracy_decimals());
this->callback_.call(state);
ControllerRegistry::notify_sensor_update(this);
}
} // namespace sensor

View File

@@ -1,4 +1,5 @@
#include "switch.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -62,6 +63,7 @@ void Switch::publish_state(bool state) {
ESP_LOGD(TAG, "'%s': Sending state %s", this->name_.c_str(), ONOFF(this->state));
this->state_callback_.call(this->state);
ControllerRegistry::notify_switch_update(this);
}
bool Switch::assumed_state() { return false; }

View File

@@ -1,4 +1,5 @@
#include "text.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -16,6 +17,7 @@ void Text::publish_state(const std::string &state) {
ESP_LOGD(TAG, "'%s': Sending state %s", this->get_name().c_str(), state.c_str());
}
this->state_callback_.call(state);
ControllerRegistry::notify_text_update(this);
}
void Text::add_on_state_callback(std::function<void(std::string)> &&callback) {

View File

@@ -1,4 +1,5 @@
#include "text_sensor.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -84,6 +85,7 @@ void TextSensor::internal_send_state_to_frontend(const std::string &state) {
this->set_has_state(true);
ESP_LOGD(TAG, "'%s': Sending state '%s'", this->name_.c_str(), state.c_str());
this->callback_.call(state);
ControllerRegistry::notify_text_sensor_update(this);
}
} // namespace text_sensor

View File

@@ -1,5 +1,5 @@
#include "update_entity.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -32,6 +32,7 @@ void UpdateEntity::publish_state() {
this->set_has_state(true);
this->state_callback_.call();
ControllerRegistry::notify_update(this);
}
} // namespace update

View File

@@ -1,4 +1,5 @@
#include "valve.h"
#include "esphome/core/controller_registry.h"
#include "esphome/core/log.h"
#include <strings.h>
@@ -147,6 +148,7 @@ void Valve::publish_state(bool save) {
ESP_LOGD(TAG, " Current Operation: %s", valve_operation_to_str(this->current_operation));
this->state_callback_.call();
ControllerRegistry::notify_valve_update(this);
if (save) {
ValveRestoreState restore{};

View File

@@ -1,134 +0,0 @@
#include "controller.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
namespace esphome {
void Controller::setup_controller(bool include_internal) {
#ifdef USE_BINARY_SENSOR
for (auto *obj : App.get_binary_sensors()) {
if (include_internal || !obj->is_internal()) {
obj->add_full_state_callback(
[this, obj](optional<bool> previous, optional<bool> state) { this->on_binary_sensor_update(obj); });
}
}
#endif
#ifdef USE_FAN
for (auto *obj : App.get_fans()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_fan_update(obj); });
}
#endif
#ifdef USE_LIGHT
for (auto *obj : App.get_lights()) {
if (include_internal || !obj->is_internal())
obj->add_new_remote_values_callback([this, obj]() { this->on_light_update(obj); });
}
#endif
#ifdef USE_SENSOR
for (auto *obj : App.get_sensors()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](float state) { this->on_sensor_update(obj, state); });
}
#endif
#ifdef USE_SWITCH
for (auto *obj : App.get_switches()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](bool state) { this->on_switch_update(obj, state); });
}
#endif
#ifdef USE_COVER
for (auto *obj : App.get_covers()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_cover_update(obj); });
}
#endif
#ifdef USE_TEXT_SENSOR
for (auto *obj : App.get_text_sensors()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](const std::string &state) { this->on_text_sensor_update(obj, state); });
}
#endif
#ifdef USE_CLIMATE
for (auto *obj : App.get_climates()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](climate::Climate & /*unused*/) { this->on_climate_update(obj); });
}
#endif
#ifdef USE_NUMBER
for (auto *obj : App.get_numbers()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](float state) { this->on_number_update(obj, state); });
}
#endif
#ifdef USE_DATETIME_DATE
for (auto *obj : App.get_dates()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_date_update(obj); });
}
#endif
#ifdef USE_DATETIME_TIME
for (auto *obj : App.get_times()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_time_update(obj); });
}
#endif
#ifdef USE_DATETIME_DATETIME
for (auto *obj : App.get_datetimes()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_datetime_update(obj); });
}
#endif
#ifdef USE_TEXT
for (auto *obj : App.get_texts()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj](const std::string &state) { this->on_text_update(obj, state); });
}
#endif
#ifdef USE_SELECT
for (auto *obj : App.get_selects()) {
if (include_internal || !obj->is_internal()) {
obj->add_on_state_callback(
[this, obj](const std::string &state, size_t index) { this->on_select_update(obj, state, index); });
}
}
#endif
#ifdef USE_LOCK
for (auto *obj : App.get_locks()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_lock_update(obj); });
}
#endif
#ifdef USE_VALVE
for (auto *obj : App.get_valves()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_valve_update(obj); });
}
#endif
#ifdef USE_MEDIA_PLAYER
for (auto *obj : App.get_media_players()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_media_player_update(obj); });
}
#endif
#ifdef USE_ALARM_CONTROL_PANEL
for (auto *obj : App.get_alarm_control_panels()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_alarm_control_panel_update(obj); });
}
#endif
#ifdef USE_EVENT
for (auto *obj : App.get_events()) {
if (include_internal || !obj->is_internal())
obj->add_on_event_callback([this, obj](const std::string &event_type) { this->on_event(obj, event_type); });
}
#endif
#ifdef USE_UPDATE
for (auto *obj : App.get_updates()) {
if (include_internal || !obj->is_internal())
obj->add_on_state_callback([this, obj]() { this->on_update(obj); });
}
#endif
}
} // namespace esphome

View File

@@ -69,7 +69,6 @@ namespace esphome {
class Controller {
public:
void setup_controller(bool include_internal = false);
#ifdef USE_BINARY_SENSOR
virtual void on_binary_sensor_update(binary_sensor::BinarySensor *obj){};
#endif
@@ -125,7 +124,7 @@ class Controller {
virtual void on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj){};
#endif
#ifdef USE_EVENT
virtual void on_event(event::Event *obj, const std::string &event_type){};
virtual void on_event(event::Event *obj){};
#endif
#ifdef USE_UPDATE
virtual void on_update(update::UpdateEntity *obj){};

View File

@@ -159,9 +159,9 @@ void ControllerRegistry::notify_alarm_control_panel_update(alarm_control_panel::
#endif
#ifdef USE_EVENT
void ControllerRegistry::notify_event(event::Event *obj, const std::string &event_type) {
void ControllerRegistry::notify_event(event::Event *obj) {
for (auto *controller : controllers_) {
controller->on_event(obj, event_type);
controller->on_event(obj);
}
}
#endif

View File

@@ -248,7 +248,7 @@ class ControllerRegistry {
#ifdef USE_EVENT
/** Notify all controllers of an event trigger. */
static void notify_event(event::Event *obj, const std::string &event_type);
static void notify_event(event::Event *obj);
#endif
#ifdef USE_UPDATE