From 4e57311dd02f290097d6b21292464228b352ef97 Mon Sep 17 00:00:00 2001 From: azinchenko Date: Fri, 8 Aug 2025 16:16:57 +0300 Subject: [PATCH] [JXD-316] All entities add its type in constructor --- esphome/components/alarm_control_panel/alarm_control_panel.h | 1 + esphome/components/button/button.h | 1 + esphome/components/climate/climate.h | 2 +- esphome/components/cover/cover.cpp | 2 +- esphome/components/datetime/date_entity.h | 1 + esphome/components/datetime/datetime_base.h | 1 + esphome/components/datetime/datetime_entity.h | 1 + esphome/components/datetime/time_entity.h | 1 + esphome/components/event/event.h | 1 + esphome/components/fan/fan.h | 1 + esphome/components/light/light_state.cpp | 2 +- esphome/components/lock/lock.cpp | 2 +- esphome/components/media_player/media_player.h | 1 + esphome/components/number/number.h | 1 + esphome/components/select/select.h | 1 + esphome/components/text/text.h | 1 + esphome/components/text_sensor/text_sensor.h | 2 +- esphome/components/update/update_entity.h | 1 + esphome/components/valve/valve.cpp | 2 +- 19 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esphome/components/alarm_control_panel/alarm_control_panel.h b/esphome/components/alarm_control_panel/alarm_control_panel.h index 85c2b2148e71..431041dec9d7 100644 --- a/esphome/components/alarm_control_panel/alarm_control_panel.h +++ b/esphome/components/alarm_control_panel/alarm_control_panel.h @@ -24,6 +24,7 @@ enum AlarmControlPanelFeature : uint8_t { class AlarmControlPanel : public EntityBase { public: + AlarmControlPanel() : EntityBase(EntityType::ALARM_CONTROL_PANEL) {} /** Make a AlarmControlPanelCall * */ diff --git a/esphome/components/button/button.h b/esphome/components/button/button.h index 75b76f9dcf30..028ccbb0d5d8 100644 --- a/esphome/components/button/button.h +++ b/esphome/components/button/button.h @@ -25,6 +25,7 @@ void log_button(const char *tag, const char *prefix, const char *type, Button *o */ class Button : public EntityBase, public EntityBase_DeviceClass { public: + Button() : EntityBase(EntityType::BUTTON) {} /** Press this button. This is called by the front-end. * * For implementing buttons, please override press_action. diff --git a/esphome/components/climate/climate.h b/esphome/components/climate/climate.h index b31a2eedf6e5..65f274db8c69 100644 --- a/esphome/components/climate/climate.h +++ b/esphome/components/climate/climate.h @@ -167,7 +167,7 @@ struct ClimateDeviceRestoreState { */ class Climate : public EntityBase { public: - Climate() {} + Climate() : EntityBase(EntityType::CLIMATE) {} /// The active mode of the climate device. ClimateMode mode{CLIMATE_MODE_OFF}; diff --git a/esphome/components/cover/cover.cpp b/esphome/components/cover/cover.cpp index 33782793715e..a23bf5e8a3fd 100644 --- a/esphome/components/cover/cover.cpp +++ b/esphome/components/cover/cover.cpp @@ -32,7 +32,7 @@ const char *cover_operation_to_str(CoverOperation op) { } } -Cover::Cover() : position{COVER_OPEN} {} +Cover::Cover() : EntityBase(EntityType::COVER), position{COVER_OPEN} {} CoverCall::CoverCall(Cover *parent) : parent_(parent) {} CoverCall &CoverCall::set_command(const char *command) { diff --git a/esphome/components/datetime/date_entity.h b/esphome/components/datetime/date_entity.h index fcbb46cf1763..12baa920ca5b 100644 --- a/esphome/components/datetime/date_entity.h +++ b/esphome/components/datetime/date_entity.h @@ -35,6 +35,7 @@ struct DateEntityRestoreState { class DateEntity : public DateTimeBase { protected: + DateEntity() : DateTimeBase(EntityType::DATETIME_DATE) {} uint16_t year_; uint8_t month_; uint8_t day_; diff --git a/esphome/components/datetime/datetime_base.h b/esphome/components/datetime/datetime_base.h index b5f54ac96f64..5b0ede2d44c3 100644 --- a/esphome/components/datetime/datetime_base.h +++ b/esphome/components/datetime/datetime_base.h @@ -13,6 +13,7 @@ namespace datetime { class DateTimeBase : public EntityBase { public: + DateTimeBase(EntityType type) : EntityBase(type) {} virtual ESPTime state_as_esptime() const = 0; void add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } diff --git a/esphome/components/datetime/datetime_entity.h b/esphome/components/datetime/datetime_entity.h index 275eedfd3b51..af029de2574a 100644 --- a/esphome/components/datetime/datetime_entity.h +++ b/esphome/components/datetime/datetime_entity.h @@ -46,6 +46,7 @@ class DateTimeEntity : public DateTimeBase { uint8_t second_; public: + DateTimeEntity() : DateTimeBase(EntityType::DATETIME_DATETIME) {} void publish_state(); DateTimeCall make_call(); diff --git a/esphome/components/datetime/time_entity.h b/esphome/components/datetime/time_entity.h index e79b8c225de1..6e6c817d1b1d 100644 --- a/esphome/components/datetime/time_entity.h +++ b/esphome/components/datetime/time_entity.h @@ -41,6 +41,7 @@ class TimeEntity : public DateTimeBase { uint8_t second_; public: + TimeEntity() : DateTimeBase(EntityType::DATETIME_TIME) {} void publish_state(); TimeCall make_call(); diff --git a/esphome/components/event/event.h b/esphome/components/event/event.h index a90c8ebe0538..754efcfa098a 100644 --- a/esphome/components/event/event.h +++ b/esphome/components/event/event.h @@ -23,6 +23,7 @@ namespace event { class Event : public EntityBase, public EntityBase_DeviceClass { public: + Event() : EntityBase(EntityType::EVENT) {} const std::string *last_event_type; void trigger(const std::string &event_type); diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index b74187eb4a00..b12aa5abd76b 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -106,6 +106,7 @@ struct FanRestoreState { class Fan : public EntityBase { public: + Fan() : EntityBase(EntityType::FAN) {} /// The current on/off state of the fan. bool state{false}; /// The current oscillation state of the fan. diff --git a/esphome/components/light/light_state.cpp b/esphome/components/light/light_state.cpp index f18d5ba1de5e..f7f2cd6f7d87 100644 --- a/esphome/components/light/light_state.cpp +++ b/esphome/components/light/light_state.cpp @@ -9,7 +9,7 @@ namespace light { static const char *const TAG = "light"; -LightState::LightState(LightOutput *output) : output_(output) {} +LightState::LightState(LightOutput *output) : EntityBase(EntityType::LIGHT), output_(output) {} LightTraits LightState::get_traits() { return this->output_->get_traits(); } LightCall LightState::turn_on() { return this->make_call().set_state(true); } diff --git a/esphome/components/lock/lock.cpp b/esphome/components/lock/lock.cpp index ddc544534967..79f05323ce59 100644 --- a/esphome/components/lock/lock.cpp +++ b/esphome/components/lock/lock.cpp @@ -24,7 +24,7 @@ const char *lock_state_to_string(LockState state) { } } -Lock::Lock() : state(LOCK_STATE_NONE) {} +Lock::Lock() : EntityBase(EntityType::LOCK), state(LOCK_STATE_NONE) {} LockCall Lock::make_call() { return LockCall(this); } void Lock::lock() { diff --git a/esphome/components/media_player/media_player.h b/esphome/components/media_player/media_player.h index 2f1c99115f91..f3712661d0aa 100644 --- a/esphome/components/media_player/media_player.h +++ b/esphome/components/media_player/media_player.h @@ -142,6 +142,7 @@ class MediaPlayer : public EntityBase { MediaPlayerState state{MEDIA_PLAYER_STATE_NONE}; float volume{1.0f}; + MediaPlayer() : EntityBase(EntityType::MEDIA_PLAYER) {} MediaPlayerCall make_call() { return MediaPlayerCall(this); } void publish_state(); diff --git a/esphome/components/number/number.h b/esphome/components/number/number.h index da91d70d5330..3b5a821193b1 100644 --- a/esphome/components/number/number.h +++ b/esphome/components/number/number.h @@ -29,6 +29,7 @@ class Number; */ class Number : public EntityBase { public: + Number() : EntityBase(EntityType::NUMBER) {} float state; void publish_state(float state); diff --git a/esphome/components/select/select.h b/esphome/components/select/select.h index 902b8a78ce72..77de2b27176e 100644 --- a/esphome/components/select/select.h +++ b/esphome/components/select/select.h @@ -33,6 +33,7 @@ class Select : public EntityBase { std::string state; SelectTraits traits; + Select() : EntityBase(EntityType::SELECT) {} void publish_state(const std::string &state); /// Instantiate a SelectCall object to modify this select component's state. diff --git a/esphome/components/text/text.h b/esphome/components/text/text.h index 74d08eda8a72..cf74ce4758c9 100644 --- a/esphome/components/text/text.h +++ b/esphome/components/text/text.h @@ -26,6 +26,7 @@ class Text : public EntityBase { std::string state; TextTraits traits; + Text() : EntityBase(EntityType::TEXT) {} void publish_state(const std::string &state); /// Instantiate a TextCall object to modify this text component's state. diff --git a/esphome/components/text_sensor/text_sensor.h b/esphome/components/text_sensor/text_sensor.h index abbea27b599d..80dffbfad0a9 100644 --- a/esphome/components/text_sensor/text_sensor.h +++ b/esphome/components/text_sensor/text_sensor.h @@ -24,7 +24,7 @@ void log_text_sensor(const char *tag, const char *prefix, const char *type, Text class TextSensor : public EntityBase, public EntityBase_DeviceClass { public: - TextSensor() = default; + TextSensor() : EntityBase(EntityType::TEXT_SENSOR) {} /// Getter-syntax for .state. std::string get_state() const; diff --git a/esphome/components/update/update_entity.h b/esphome/components/update/update_entity.h index 9424e80b9f21..de96d8f553c1 100644 --- a/esphome/components/update/update_entity.h +++ b/esphome/components/update/update_entity.h @@ -29,6 +29,7 @@ enum UpdateState : uint8_t { class UpdateEntity : public EntityBase, public EntityBase_DeviceClass { public: + UpdateEntity() : EntityBase(EntityType::UPDATE) {} void publish_state(); void perform() { this->perform(false); } diff --git a/esphome/components/valve/valve.cpp b/esphome/components/valve/valve.cpp index b041fe844962..cb428029144c 100644 --- a/esphome/components/valve/valve.cpp +++ b/esphome/components/valve/valve.cpp @@ -32,7 +32,7 @@ const char *valve_operation_to_str(ValveOperation op) { } } -Valve::Valve() : position{VALVE_OPEN} {} +Valve::Valve() : EntityBase(EntityType::VALVE), position{VALVE_OPEN} {} ValveCall::ValveCall(Valve *parent) : parent_(parent) {} ValveCall &ValveCall::set_command(const char *command) {