Refactor api_connection to avoid constructing protobuf messages if the tx_buffer is full

Also do not try to dequeue if the buffer is full
This commit is contained in:
J. Nick Koston
2025-05-13 20:36:57 -05:00
parent 42c355e6d7
commit 3fb10037a9
43 changed files with 613 additions and 702 deletions

View File

@@ -13,6 +13,8 @@ static const char *const TAG = "alarm_control_panel";
AlarmControlPanelCall AlarmControlPanel::make_call() { return AlarmControlPanelCall(this); }
const char *AlarmControlPanel::get_component_type() const { return "alarm_control_panel"; }
bool AlarmControlPanel::is_state_armed(AlarmControlPanelState state) {
switch (state) {
case ACP_STATE_ARMED_AWAY:

View File

@@ -29,6 +29,8 @@ class AlarmControlPanel : public EntityBase {
*/
AlarmControlPanelCall make_call();
const char *get_component_type() const override;
/** Set the state of the alarm_control_panel.
*
* @param state The AlarmControlPanelState.

File diff suppressed because it is too large Load Diff

View File

@@ -12,9 +12,14 @@
#include <vector>
namespace esphome {
class EntityBase;
namespace api {
using send_message_t = bool(APIConnection *, void *);
// Forward declaration
std::string get_default_unique_id(const std::string &component_type, EntityBase *entity);
// Function signature for deferred message handlers
using send_message_t = bool (APIConnection::*)(void *);
/*
This class holds a pointer to the source component that wants to publish a message, and a pointer to a function that
@@ -30,10 +35,10 @@ class DeferredMessageQueue {
protected:
void *source_;
send_message_t *send_message_;
send_message_t send_message_;
public:
DeferredMessage(void *source, send_message_t *send_message) : source_(source), send_message_(send_message) {}
DeferredMessage(void *source, send_message_t send_message) : source_(source), send_message_(send_message) {}
bool operator==(const DeferredMessage &test) const {
return (source_ == test.source_ && send_message_ == test.send_message_);
}
@@ -46,12 +51,12 @@ class DeferredMessageQueue {
APIConnection *api_connection_;
// helper for allowing only unique entries in the queue
void dmq_push_back_with_dedup_(void *source, send_message_t *send_message);
void dmq_push_back_with_dedup_(void *source, send_message_t send_message);
public:
DeferredMessageQueue(APIConnection *api_connection) : api_connection_(api_connection) {}
void process_queue();
void defer(void *source, send_message_t *send_message);
void defer(void *source, send_message_t send_message);
};
class APIConnection : public APIServerConnection {
@@ -69,137 +74,213 @@ class APIConnection : public APIServerConnection {
#ifdef USE_BINARY_SENSOR
bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state);
void send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor);
static bool try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor);
static bool try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, bool state);
static bool try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor);
protected:
bool try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor);
bool try_send_binary_sensor_state_(binary_sensor::BinarySensor *binary_sensor, bool state);
bool try_send_binary_sensor_info_(binary_sensor::BinarySensor *binary_sensor);
public:
#endif
#ifdef USE_COVER
bool send_cover_state(cover::Cover *cover);
void send_cover_info(cover::Cover *cover);
static bool try_send_cover_state(APIConnection *api, void *v_cover);
static bool try_send_cover_info(APIConnection *api, void *v_cover);
void cover_command(const CoverCommandRequest &msg) override;
protected:
bool try_send_cover_state_(cover::Cover *cover);
bool try_send_cover_info_(cover::Cover *cover);
public:
#endif
#ifdef USE_FAN
bool send_fan_state(fan::Fan *fan);
void send_fan_info(fan::Fan *fan);
static bool try_send_fan_state(APIConnection *api, void *v_fan);
static bool try_send_fan_info(APIConnection *api, void *v_fan);
void fan_command(const FanCommandRequest &msg) override;
protected:
bool try_send_fan_state_(fan::Fan *fan);
bool try_send_fan_info_(fan::Fan *fan);
public:
#endif
#ifdef USE_LIGHT
bool send_light_state(light::LightState *light);
void send_light_info(light::LightState *light);
static bool try_send_light_state(APIConnection *api, void *v_light);
static bool try_send_light_info(APIConnection *api, void *v_light);
void light_command(const LightCommandRequest &msg) override;
protected:
bool try_send_light_state_(light::LightState *light);
bool try_send_light_info_(light::LightState *light);
public:
#endif
#ifdef USE_SENSOR
bool send_sensor_state(sensor::Sensor *sensor, float state);
void send_sensor_info(sensor::Sensor *sensor);
static bool try_send_sensor_state(APIConnection *api, void *v_sensor);
static bool try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state);
static bool try_send_sensor_info(APIConnection *api, void *v_sensor);
protected:
bool try_send_sensor_state_(sensor::Sensor *sensor);
bool try_send_sensor_state_(sensor::Sensor *sensor, float state);
bool try_send_sensor_info_(sensor::Sensor *sensor);
public:
#endif
#ifdef USE_SWITCH
bool send_switch_state(switch_::Switch *a_switch, bool state);
void send_switch_info(switch_::Switch *a_switch);
static bool try_send_switch_state(APIConnection *api, void *v_a_switch);
static bool try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state);
static bool try_send_switch_info(APIConnection *api, void *v_a_switch);
void switch_command(const SwitchCommandRequest &msg) override;
protected:
bool try_send_switch_state_(switch_::Switch *a_switch);
bool try_send_switch_state_(switch_::Switch *a_switch, bool state);
bool try_send_switch_info_(switch_::Switch *a_switch);
public:
#endif
#ifdef USE_TEXT_SENSOR
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state);
void send_text_sensor_info(text_sensor::TextSensor *text_sensor);
static bool try_send_text_sensor_state(APIConnection *api, void *v_text_sensor);
static bool try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, std::string state);
static bool try_send_text_sensor_info(APIConnection *api, void *v_text_sensor);
protected:
bool try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor);
bool try_send_text_sensor_state_(text_sensor::TextSensor *text_sensor, std::string state);
bool try_send_text_sensor_info_(text_sensor::TextSensor *text_sensor);
public:
#endif
#ifdef USE_ESP32_CAMERA
void set_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
void send_camera_info(esp32_camera::ESP32Camera *camera);
static bool try_send_camera_info(APIConnection *api, void *v_camera);
void camera_image(const CameraImageRequest &msg) override;
protected:
bool try_send_camera_info_(esp32_camera::ESP32Camera *camera);
public:
#endif
#ifdef USE_CLIMATE
bool send_climate_state(climate::Climate *climate);
void send_climate_info(climate::Climate *climate);
static bool try_send_climate_state(APIConnection *api, void *v_climate);
static bool try_send_climate_info(APIConnection *api, void *v_climate);
void climate_command(const ClimateCommandRequest &msg) override;
protected:
bool try_send_climate_state_(climate::Climate *climate);
bool try_send_climate_info_(climate::Climate *climate);
public:
#endif
#ifdef USE_NUMBER
bool send_number_state(number::Number *number, float state);
void send_number_info(number::Number *number);
static bool try_send_number_state(APIConnection *api, void *v_number);
static bool try_send_number_state(APIConnection *api, number::Number *number, float state);
static bool try_send_number_info(APIConnection *api, void *v_number);
void number_command(const NumberCommandRequest &msg) override;
protected:
bool try_send_number_state_(number::Number *number);
bool try_send_number_state_(number::Number *number, float state);
bool try_send_number_info_(number::Number *number);
public:
#endif
#ifdef USE_DATETIME_DATE
bool send_date_state(datetime::DateEntity *date);
void send_date_info(datetime::DateEntity *date);
static bool try_send_date_state(APIConnection *api, void *v_date);
static bool try_send_date_info(APIConnection *api, void *v_date);
void date_command(const DateCommandRequest &msg) override;
protected:
bool try_send_date_state_(datetime::DateEntity *date);
bool try_send_date_info_(datetime::DateEntity *date);
public:
#endif
#ifdef USE_DATETIME_TIME
bool send_time_state(datetime::TimeEntity *time);
void send_time_info(datetime::TimeEntity *time);
static bool try_send_time_state(APIConnection *api, void *v_time);
static bool try_send_time_info(APIConnection *api, void *v_time);
void time_command(const TimeCommandRequest &msg) override;
protected:
bool try_send_time_state_(datetime::TimeEntity *time);
bool try_send_time_info_(datetime::TimeEntity *time);
public:
#endif
#ifdef USE_DATETIME_DATETIME
bool send_datetime_state(datetime::DateTimeEntity *datetime);
void send_datetime_info(datetime::DateTimeEntity *datetime);
static bool try_send_datetime_state(APIConnection *api, void *v_datetime);
static bool try_send_datetime_info(APIConnection *api, void *v_datetime);
void datetime_command(const DateTimeCommandRequest &msg) override;
protected:
bool try_send_datetime_state_(datetime::DateTimeEntity *datetime);
bool try_send_datetime_info_(datetime::DateTimeEntity *datetime);
public:
#endif
#ifdef USE_TEXT
bool send_text_state(text::Text *text, std::string state);
void send_text_info(text::Text *text);
static bool try_send_text_state(APIConnection *api, void *v_text);
static bool try_send_text_state(APIConnection *api, text::Text *text, std::string state);
static bool try_send_text_info(APIConnection *api, void *v_text);
void text_command(const TextCommandRequest &msg) override;
protected:
bool try_send_text_state_(text::Text *text);
bool try_send_text_state_(text::Text *text, std::string state);
bool try_send_text_info_(text::Text *text);
public:
#endif
#ifdef USE_SELECT
bool send_select_state(select::Select *select, std::string state);
void send_select_info(select::Select *select);
static bool try_send_select_state(APIConnection *api, void *v_select);
static bool try_send_select_state(APIConnection *api, select::Select *select, std::string state);
static bool try_send_select_info(APIConnection *api, void *v_select);
void select_command(const SelectCommandRequest &msg) override;
protected:
bool try_send_select_state_(select::Select *select);
bool try_send_select_state_(select::Select *select, std::string state);
bool try_send_select_info_(select::Select *select);
public:
#endif
#ifdef USE_BUTTON
void send_button_info(button::Button *button);
static bool try_send_button_info(APIConnection *api, void *v_button);
void button_command(const ButtonCommandRequest &msg) override;
protected:
bool try_send_button_info_(button::Button *button);
public:
#endif
#ifdef USE_LOCK
bool send_lock_state(lock::Lock *a_lock, lock::LockState state);
void send_lock_info(lock::Lock *a_lock);
static bool try_send_lock_state(APIConnection *api, void *v_a_lock);
static bool try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state);
static bool try_send_lock_info(APIConnection *api, void *v_a_lock);
void lock_command(const LockCommandRequest &msg) override;
protected:
bool try_send_lock_state_(lock::Lock *a_lock);
bool try_send_lock_state_(lock::Lock *a_lock, lock::LockState state);
bool try_send_lock_info_(lock::Lock *a_lock);
public:
#endif
#ifdef USE_VALVE
bool send_valve_state(valve::Valve *valve);
void send_valve_info(valve::Valve *valve);
static bool try_send_valve_state(APIConnection *api, void *v_valve);
static bool try_send_valve_info(APIConnection *api, void *v_valve);
void valve_command(const ValveCommandRequest &msg) override;
protected:
bool try_send_valve_state_(valve::Valve *valve);
bool try_send_valve_info_(valve::Valve *valve);
public:
#endif
#ifdef USE_MEDIA_PLAYER
bool send_media_player_state(media_player::MediaPlayer *media_player);
void send_media_player_info(media_player::MediaPlayer *media_player);
static bool try_send_media_player_state(APIConnection *api, void *v_media_player);
static bool try_send_media_player_info(APIConnection *api, void *v_media_player);
void media_player_command(const MediaPlayerCommandRequest &msg) override;
protected:
bool try_send_media_player_state_(media_player::MediaPlayer *media_player);
bool try_send_media_player_info_(media_player::MediaPlayer *media_player);
public:
#endif
bool try_send_log_message(int level, const char *tag, const char *line);
void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
@@ -246,25 +327,37 @@ class APIConnection : public APIServerConnection {
#ifdef USE_ALARM_CONTROL_PANEL
bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
void send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
static bool try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel);
static bool try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel);
void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override;
protected:
bool try_send_alarm_control_panel_state_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
bool try_send_alarm_control_panel_info_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel);
public:
#endif
#ifdef USE_EVENT
void send_event(event::Event *event, std::string event_type);
void send_event_info(event::Event *event);
static bool try_send_event(APIConnection *api, void *v_event);
static bool try_send_event(APIConnection *api, event::Event *event, std::string event_type);
static bool try_send_event_info(APIConnection *api, void *v_event);
protected:
bool try_send_event_(event::Event *event);
bool try_send_event_(event::Event *event, std::string event_type);
bool try_send_event_info_(event::Event *event);
public:
#endif
#ifdef USE_UPDATE
bool send_update_state(update::UpdateEntity *update);
void send_update_info(update::UpdateEntity *update);
static bool try_send_update_state(APIConnection *api, void *v_update);
static bool try_send_update_info(APIConnection *api, void *v_update);
void update_command(const UpdateCommandRequest &msg) override;
protected:
bool try_send_update_state_(update::UpdateEntity *update);
bool try_send_update_info_(update::UpdateEntity *update);
public:
#endif
void on_disconnect_response(const DisconnectResponse &value) override;
@@ -320,11 +413,135 @@ class APIConnection : public APIServerConnection {
}
bool send_buffer(ProtoWriteBuffer buffer, uint32_t message_type) override;
/**
* Check if the connection can write without blocking.
* This should be called before constructing any protobuf messages to avoid
* unnecessary message construction when the buffer is full.
*/
inline bool can_write_without_blocking() { return this->helper_->can_write_without_blocking(); }
std::string get_client_combined_info() const { return this->client_combined_info_; }
// Template helper methods are in the protected section
protected:
friend APIServer;
/**
* Helper template that attempts to send a message only if the transmit buffer is available.
* Otherwise, defers the message to the queue.
*
* @tparam EntityT The entity type
* @tparam FuncT The function type (typically a member function pointer)
* @tparam ArgsT Additional argument types (if any)
* @param entity The entity to send the message for
* @param func The function to call for sending the message
* @param defer_func The function to defer if sending fails (may be the same as func)
* @param args Additional arguments to pass to the function
* @return True if successful or message deferred
*/
/**
* Generic send entity state method to reduce code duplication.
* Only attempts to build and send the message if the transmit buffer is available.
*
* This is the base version for entities that use their current state.
*
* @param entity The entity to send state for
* @param try_send_func The function that tries to send the state
* @return True on success or message deferred, false if subscription check failed
*/
template<typename EntityT> bool send_state_(EntityT *entity, bool (APIConnection::*try_send_func)(EntityT *)) {
if (!this->state_subscription_)
return false;
if (this->can_write_without_blocking() && (this->*try_send_func)(entity)) {
return true;
}
this->deferred_message_queue_.defer(entity, reinterpret_cast<send_message_t>(try_send_func));
return true;
}
/**
* Enhanced send entity state method that handles explicit state values.
* Only attempts to build and send the message if the transmit buffer is available.
*
* This overload accepts additional state parameter(s) to be used instead of the entity's current state.
* It works with specialized try_send functions that accept additional parameters.
*
* @tparam EntityT The entity type
* @tparam StateT Type of the state parameter
* @tparam Args Additional argument types (if any)
* @param entity The entity to send state for
* @param try_send_entity_func The function that tries to send the state with entity pointer only
* @param try_send_state_func The function that tries to send the state with entity and state parameters
* @param state The state value to send
* @param args Additional arguments to pass to the try_send_state_func
* @return True on success or message deferred, false if subscription check failed
*/
template<typename EntityT, typename StateT, typename... Args>
bool send_state_with_value_(EntityT *entity, bool (APIConnection::*try_send_entity_func)(EntityT *),
bool (APIConnection::*try_send_state_func)(EntityT *, StateT, Args...), StateT state,
Args... args) {
if (!this->state_subscription_)
return false;
if (this->can_write_without_blocking() && (this->*try_send_state_func)(entity, state, args...)) {
return true;
}
this->deferred_message_queue_.defer(entity, reinterpret_cast<send_message_t>(try_send_entity_func));
return true;
}
/**
* Generic send entity info method to reduce code duplication.
* Only attempts to build and send the message if the transmit buffer is available.
*
* @param entity The entity to send info for
* @param try_send_func The function that tries to send the info
*/
template<typename EntityT> void send_info_(EntityT *entity, bool (APIConnection::*try_send_func)(EntityT *)) {
if (this->can_write_without_blocking() && (this->*try_send_func)(entity)) {
return;
}
this->deferred_message_queue_.defer(entity, reinterpret_cast<send_message_t>(try_send_func));
}
/**
* Generic template for generating entity info response messages.
* This is used to reduce duplication in the try_send_*_info functions.
*
* @tparam EntityT The entity type
* @tparam ResponseT The protobuf list entities response type
* @param entity The entity to generate info for
* @param response The pre-populated response object with entity-specific fields
* @param send_response_func Function to send the response
* @return True if the message was sent successfully
*/
template<typename EntityT, typename ResponseT>
bool try_send_entity_info_(EntityT *entity, ResponseT &response,
bool (APIServerConnectionBase::*send_response_func)(const ResponseT &)) {
// Set common fields that are shared by all entity types
response.key = entity->get_object_id_hash();
response.object_id = entity->get_object_id();
if (entity->has_own_name())
response.name = entity->get_name();
// Get the unique_id - try the entity's implementation first
// This is safe because EntityBase has a default implementation that returns an empty string
response.unique_id = entity->unique_id();
// Fall back to default if empty
if (response.unique_id.empty())
response.unique_id = get_default_unique_id(entity->get_component_type(), entity);
// Set common EntityBase properties
response.icon = entity->get_icon();
response.disabled_by_default = entity->is_disabled_by_default();
response.entity_category = static_cast<enums::EntityCategory>(entity->get_entity_category());
// Send the response using the provided send method
return (this->*send_response_func)(response);
}
bool send_(const void *buf, size_t len, bool force);
enum class ConnectionState {

View File

@@ -59,6 +59,8 @@ void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
bool BinarySensor::has_state() const { return this->has_state_; }
bool BinarySensor::is_status_binary_sensor() const { return false; }
const char *BinarySensor::get_component_type() const { return "binary_sensor"; }
} // namespace binary_sensor
} // namespace esphome

View File

@@ -74,6 +74,9 @@ class BinarySensor : public EntityBase, public EntityBase_DeviceClass {
virtual bool is_status_binary_sensor() const;
// Get the component type for this entity
const char *get_component_type() const override;
protected:
CallbackManager<void(bool)> state_callback_{};
Filter *filter_list_{nullptr};

View File

@@ -13,5 +13,7 @@ void Button::press() {
}
void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
const char *Button::get_component_type() const { return "button"; }
} // namespace button
} // namespace esphome

View File

@@ -40,6 +40,8 @@ class Button : public EntityBase, public EntityBase_DeviceClass {
*/
void add_on_press_callback(std::function<void()> &&callback);
const char *get_component_type() const override;
protected:
/** You should implement this virtual method if you want to create your own button.
*/

View File

@@ -566,6 +566,8 @@ bool Climate::set_custom_preset_(const std::string &preset) {
return set_alternative(this->custom_preset, this->preset, preset);
}
const char *Climate::get_component_type() const { return "climate"; }
void Climate::dump_traits_(const char *tag) {
auto traits = this->get_traits();
ESP_LOGCONFIG(tag, "ClimateTraits:");

View File

@@ -169,6 +169,8 @@ class Climate : public EntityBase {
public:
Climate() {}
const char *get_component_type() const override;
/// The active mode of the climate device.
ClimateMode mode{CLIMATE_MODE_OFF};

View File

@@ -207,6 +207,7 @@ optional<CoverRestoreState> Cover::restore_state_() {
bool Cover::is_fully_open() const { return this->position == COVER_OPEN; }
bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; }
const char *Cover::get_component_type() const { return "cover"; }
CoverCall CoverRestoreState::to_call(Cover *cover) {
auto call = cover->make_call();

View File

@@ -163,6 +163,9 @@ class Cover : public EntityBase, public EntityBase_DeviceClass {
/// Helper method to check if the cover is fully closed. Equivalent to comparing .position against 0.0
bool is_fully_closed() const;
// Get the component type for this entity
const char *get_component_type() const override;
protected:
friend CoverCall;

View File

@@ -43,6 +43,8 @@ class DateEntity : public DateTimeBase {
void publish_state();
DateCall make_call();
const char *get_component_type() const override { return "date"; }
ESPTime state_as_esptime() const override {
ESPTime obj;
obj.year = this->year_;

View File

@@ -20,6 +20,8 @@ class DateTimeBase : public EntityBase {
void add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
virtual const char *get_component_type() const override = 0;
#ifdef USE_TIME
void set_rtc(time::RealTimeClock *rtc) { this->rtc_ = rtc; }
time::RealTimeClock *get_rtc() const { return this->rtc_; }

View File

@@ -49,6 +49,8 @@ class DateTimeEntity : public DateTimeBase {
void publish_state();
DateTimeCall make_call();
const char *get_component_type() const override { return "datetime"; }
ESPTime state_as_esptime() const override;
const uint16_t &year = year_;

View File

@@ -44,6 +44,8 @@ class TimeEntity : public DateTimeBase {
void publish_state();
TimeCall make_call();
const char *get_component_type() const override { return "time"; }
ESPTime state_as_esptime() const override {
ESPTime obj;
obj.hour = this->hour_;

View File

@@ -22,5 +22,7 @@ void Event::add_on_event_callback(std::function<void(const std::string &event_ty
this->event_callback_.add(std::move(callback));
}
const char *Event::get_component_type() const { return "event"; }
} // namespace event
} // namespace esphome

View File

@@ -30,6 +30,8 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
std::set<std::string> get_event_types() const { return this->types_; }
void add_on_event_callback(std::function<void(const std::string &event_type)> &&callback);
const char *get_component_type() const override;
protected:
CallbackManager<void(const std::string &event_type)> event_callback_;
std::set<std::string> types_;

View File

@@ -205,5 +205,7 @@ void Fan::dump_traits_(const char *tag, const char *prefix) {
}
}
const char *Fan::get_component_type() const { return "fan"; }
} // namespace fan
} // namespace esphome

View File

@@ -132,6 +132,9 @@ class Fan : public EntityBase {
/// Set the restore mode of this fan.
void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
// Get the component type for this entity
const char *get_component_type() const override;
protected:
friend FanCall;

View File

@@ -11,6 +11,8 @@ static const char *const TAG = "light";
LightState::LightState(LightOutput *output) : output_(output) {}
const char *LightState::get_component_type() const { return "light"; }
LightTraits LightState::get_traits() { return this->output_->get_traits(); }
LightCall LightState::turn_on() { return this->make_call().set_state(true); }
LightCall LightState::turn_off() { return this->make_call().set_state(false); }

View File

@@ -64,6 +64,8 @@ class LightState : public EntityBase, public Component {
public:
LightState(LightOutput *output);
const char *get_component_type() const override;
LightTraits get_traits();
/// Make a light state call

View File

@@ -57,6 +57,8 @@ void Lock::publish_state(LockState state) {
void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
const char *Lock::get_component_type() const { return "lock"; }
void LockCall::perform() {
ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
this->validate_();

View File

@@ -145,6 +145,8 @@ class Lock : public EntityBase {
*/
void add_on_state_callback(std::function<void()> &&callback);
const char *get_component_type() const override;
protected:
friend LockCall;

View File

@@ -137,5 +137,7 @@ void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
void MediaPlayer::publish_state() { this->state_callback_.call(); }
const char *MediaPlayer::get_component_type() const { return "media_player"; }
} // namespace media_player
} // namespace esphome

View File

@@ -105,6 +105,8 @@ class MediaPlayer : public EntityBase {
virtual MediaPlayerTraits get_traits() = 0;
const char *get_component_type() const override;
protected:
friend MediaPlayerCall;

View File

@@ -17,5 +17,7 @@ void Number::add_on_state_callback(std::function<void(float)> &&callback) {
this->state_callback_.add(std::move(callback));
}
const char *Number::get_component_type() const { return "number"; }
} // namespace number
} // namespace esphome

View File

@@ -51,6 +51,8 @@ class Number : public EntityBase {
/// Return whether this number has gotten a full state yet.
bool has_state() const { return has_state_; }
const char *get_component_type() const override;
protected:
friend class NumberCall;

View File

@@ -58,5 +58,7 @@ optional<std::string> Select::at(size_t index) const {
}
}
const char *Select::get_component_type() const { return "select"; }
} // namespace select
} // namespace esphome

View File

@@ -61,6 +61,8 @@ class Select : public EntityBase {
void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);
const char *get_component_type() const override;
protected:
friend class SelectCall;

View File

@@ -86,6 +86,7 @@ void Sensor::clear_filters() {
float Sensor::get_state() const { return this->state; }
float Sensor::get_raw_state() const { return this->raw_state; }
std::string Sensor::unique_id() { return ""; }
const char *Sensor::get_component_type() const { return "sensor"; }
void Sensor::internal_send_state_to_frontend(float state) {
this->has_state_ = true;

View File

@@ -145,6 +145,9 @@ class Sensor : public EntityBase, public EntityBase_DeviceClass, public EntityBa
*/
virtual std::string unique_id();
// Get the component type for this entity
const char *get_component_type() const override;
void internal_send_state_to_frontend(float state);
protected:

View File

@@ -62,6 +62,7 @@ void Switch::add_on_state_callback(std::function<void(bool)> &&callback) {
}
void Switch::set_inverted(bool inverted) { this->inverted_ = inverted; }
bool Switch::is_inverted() const { return this->inverted_; }
const char *Switch::get_component_type() const { return "switch"; }
void log_switch(const char *tag, const char *prefix, const char *type, Switch *obj) {
if (obj != nullptr) {

View File

@@ -112,6 +112,9 @@ class Switch : public EntityBase, public EntityBase_DeviceClass {
void set_restore_mode(SwitchRestoreMode restore_mode) { this->restore_mode = restore_mode; }
// Get the component type for this entity
const char *get_component_type() const override;
protected:
/** Write the given state to hardware. You should implement this
* abstract method if you want to create your own switch.

View File

@@ -22,5 +22,7 @@ void Text::add_on_state_callback(std::function<void(std::string)> &&callback) {
this->state_callback_.add(std::move(callback));
}
const char *Text::get_component_type() const { return "text"; }
} // namespace text
} // namespace esphome

View File

@@ -36,6 +36,8 @@ class Text : public EntityBase {
void add_on_state_callback(std::function<void(std::string)> &&callback);
const char *get_component_type() const override;
protected:
friend class TextCall;

View File

@@ -67,6 +67,7 @@ void TextSensor::internal_send_state_to_frontend(const std::string &state) {
std::string TextSensor::unique_id() { return ""; }
bool TextSensor::has_state() { return this->has_state_; }
const char *TextSensor::get_component_type() const { return "text_sensor"; }
} // namespace text_sensor
} // namespace esphome

View File

@@ -71,6 +71,9 @@ class TextSensor : public EntityBase, public EntityBase_DeviceClass {
void internal_send_state_to_frontend(const std::string &state);
// Get the component type for this entity
const char *get_component_type() const override;
protected:
CallbackManager<void(std::string)> raw_callback_; ///< Storage for raw state callbacks.
CallbackManager<void(std::string)> callback_; ///< Storage for filtered state callbacks.

View File

@@ -34,5 +34,7 @@ void UpdateEntity::publish_state() {
this->state_callback_.call();
}
const char *UpdateEntity::get_component_type() const { return "update"; }
} // namespace update
} // namespace esphome

View File

@@ -41,6 +41,8 @@ class UpdateEntity : public EntityBase, public EntityBase_DeviceClass {
void add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
const char *get_component_type() const override;
protected:
UpdateState state_{UPDATE_STATE_UNKNOWN};
UpdateInfo update_info_;

View File

@@ -165,6 +165,8 @@ optional<ValveRestoreState> Valve::restore_state_() {
bool Valve::is_fully_open() const { return this->position == VALVE_OPEN; }
bool Valve::is_fully_closed() const { return this->position == VALVE_CLOSED; }
const char *Valve::get_component_type() const { return "valve"; }
ValveCall ValveRestoreState::to_call(Valve *valve) {
auto call = valve->make_call();
call.set_position(this->position);

View File

@@ -136,6 +136,8 @@ class Valve : public EntityBase, public EntityBase_DeviceClass {
/// Helper method to check if the valve is fully closed. Equivalent to comparing .position against 0.0
bool is_fully_closed() const;
const char *get_component_type() const override;
protected:
friend ValveCall;

View File

@@ -29,6 +29,14 @@ class EntityBase {
// Get the unique Object ID of this Entity
uint32_t get_object_id_hash();
// Get the unique ID for this entity, can be overridden by entities
// that need custom unique IDs
virtual std::string unique_id() { return ""; }
// Get the component type for this entity, must be overridden by entities
// Returns a static string stored in flash memory
virtual const char *get_component_type() const = 0;
// Get/set whether this Entity should be hidden from outside of ESPHome
bool is_internal() const;
void set_internal(bool internal);