From da390d32f8cda845cbc9fb17c1baec366df35d05 Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Fri, 1 May 2020 00:11:26 +0200 Subject: [PATCH 1/4] Move stop/is_running implementation to Action base class Try to fix issue #1105. Until now if an UpdateComponentAction, a LambdaAction, or any action that can contain those inside their "else" or "then" action lists, resulted in a call to script.stop on the script that contains them, the script would continue running, because they didn't implement a stop() method. Basically only the asynchronous ones did: DelayAction, WaitUntilAction and ScriptWaitAction. With this change num_running_ in Action replaces DelayAction::num_running_ and WaitUntilAction::triggered_ to provide the same is_running logic to other actions. --- esphome/components/script/script.h | 13 ++------ esphome/core/automation.h | 17 ++++++++--- esphome/core/base_automation.h | 49 +++++++++++------------------- 3 files changed, 34 insertions(+), 45 deletions(-) diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index 3b97327da..a6b208167 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -53,41 +53,34 @@ template class ScriptWaitAction : public Action, public C public: ScriptWaitAction(Script *script) : script_(script) {} - void play(Ts... x) { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } void play_complex(Ts... x) override { + this->num_running_++; // Check if we can continue immediately. if (!this->script_->is_running()) { - this->triggered_ = false; this->play_next(x...); return; } this->var_ = std::make_tuple(x...); - this->triggered_ = true; this->loop(); } - void stop() override { this->triggered_ = false; } - void loop() override { - if (!this->triggered_) + if (this->num_running_ == 0) return; if (this->script_->is_running()) return; - this->triggered_ = false; this->play_next_tuple(this->var_); } float get_setup_priority() const override { return setup_priority::DATA; } - bool is_running() override { return this->triggered_ || this->is_running_next(); } - protected: Script *script_; - bool triggered_{false}; std::tuple var_{}; }; diff --git a/esphome/core/automation.h b/esphome/core/automation.h index cbe96a749..6e595fc45 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -77,17 +77,24 @@ template class Action { public: virtual void play(Ts... x) = 0; virtual void play_complex(Ts... x) { + this->num_running_++; this->play(x...); this->play_next(x...); } void play_next(Ts... x) { - if (this->next_ != nullptr) { - this->next_->play_complex(x...); + if (this->num_running_ > 0) { + this->num_running_--; + if (this->next_ != nullptr) { + this->next_->play_complex(x...); + } } } virtual void stop() {} virtual void stop_complex() { - this->stop(); + if (num_running_) { + this->stop(); + this->num_running_ = 0; + } this->stop_next(); } void stop_next() { @@ -95,7 +102,7 @@ template class Action { this->next_->stop_complex(); } } - virtual bool is_running() { return this->is_running_next(); } + virtual bool is_running() { return this->num_running_ > 0 || this->is_running_next(); } bool is_running_next() { if (this->next_ == nullptr) return false; @@ -114,6 +121,8 @@ template class Action { } Action *next_ = nullptr; + + int num_running_{0}; }; template class ActionList { diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index add3df0bb..bd3400979 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -110,27 +110,17 @@ template class DelayAction : public Action, public Compon void stop() override { this->cancel_timeout(""); - this->num_running_ = 0; } void play(Ts... x) override { /* ignore - see play_complex */ } void play_complex(Ts... x) override { - auto f = std::bind(&DelayAction::delay_end_, this, x...); + auto f = std::bind(&Action::play_next, this, x...); this->num_running_++; this->set_timeout(this->delay_.value(x...), f); } float get_setup_priority() const override { return setup_priority::HARDWARE; } - - bool is_running() override { return this->num_running_ > 0 || this->is_running_next(); } - - protected: - void delay_end_(Ts... x) { - this->num_running_--; - this->play_next(x...); - } - int num_running_{0}; }; template class LambdaAction : public Action { @@ -160,17 +150,18 @@ template class IfAction : public Action { } void play_complex(Ts... x) override { + this->num_running_++; bool res = this->condition_->check(x...); if (res) { if (this->then_.empty()) { this->play_next(x...); - } else { + } else if (this->num_running_ > 0) { this->then_.play(x...); } } else { if (this->else_.empty()) { this->play_next(x...); - } else { + } else if (this->num_running_ > 0) { this->else_.play(x...); } } @@ -181,8 +172,6 @@ template class IfAction : public Action { this->else_.stop(); } - bool is_running() override { return this->then_.is_running() || this->else_.is_running() || this->is_running_next(); } - protected: Condition *condition_; ActionList then_; @@ -196,9 +185,11 @@ template class WhileAction : public Action { void add_then(const std::vector *> &actions) { this->then_.add_actions(actions); this->then_.add_action(new LambdaAction([this](Ts... x) { - if (this->condition_->check_tuple(this->var_)) { + if (this->num_running_ > 0 && this->condition_->check_tuple(this->var_)) { // play again - this->then_.play_tuple(this->var_); + if (this->num_running_ > 0) { + this->then_.play_tuple(this->var_); + } } else { // condition false, play next this->play_next_tuple(this->var_); @@ -210,6 +201,7 @@ template class WhileAction : public Action { } void play_complex(Ts... x) override { + this->num_running_++; // Store loop parameters this->var_ = std::make_tuple(x...); // Initial condition check @@ -220,13 +212,13 @@ template class WhileAction : public Action { return; } - this->then_.play_tuple(this->var_); + if (this->num_running_ > 0) { + this->then_.play_tuple(this->var_); + } } void stop() override { this->then_.stop(); } - bool is_running() override { return this->then_.is_running() || this->is_running_next(); } - protected: Condition *condition_; ActionList then_; @@ -237,42 +229,37 @@ template class WaitUntilAction : public Action, public Co public: WaitUntilAction(Condition *condition) : condition_(condition) {} - void play(Ts... x) { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } void play_complex(Ts... x) override { + this->num_running_++; // Check if we can continue immediately. if (this->condition_->check(x...)) { - this->triggered_ = false; - this->play_next(x...); + if (this->num_running_ > 0) { + this->play_next(x...); + } return; } this->var_ = std::make_tuple(x...); - this->triggered_ = true; this->loop(); } - void stop() override { this->triggered_ = false; } - void loop() override { - if (!this->triggered_) + if (this->num_running_ == 0) return; if (!this->condition_->check_tuple(this->var_)) { return; } - this->triggered_ = false; this->play_next_tuple(this->var_); } float get_setup_priority() const override { return setup_priority::DATA; } - bool is_running() override { return this->triggered_ || this->is_running_next(); } - protected: Condition *condition_; - bool triggered_{false}; std::tuple var_{}; }; From a62b6548d238d71fc43cba86b809b5d091577919 Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Fri, 1 May 2020 12:38:34 +0200 Subject: [PATCH 2/4] Make some Action methods protected Apparently play()/stop() etc. are not meant to be called directly by users of the class and if they're called directly that would not give the expected result for the classes that have an empty play(). Make all methods except play_complex, stop_comples and is_running protected. While there also make RemoteTransmitterActionBase::encode protected. --- .../components/api/homeassistant_service.h | 5 +- esphome/components/binary_sensor/automation.h | 5 +- esphome/components/climate/automation.h | 4 +- esphome/components/cover/automation.h | 36 +++++----- .../deep_sleep/deep_sleep_component.h | 8 +-- esphome/components/dfplayer/dfplayer.h | 24 +++++-- esphome/components/display/display_buffer.h | 10 ++- esphome/components/esp8266_pwm/esp8266_pwm.h | 4 +- esphome/components/fan/automation.h | 12 ++-- .../components/globals/globals_component.h | 4 +- .../components/http_request/http_request.h | 4 +- .../integration/integration_sensor.h | 4 +- esphome/components/ledc/ledc_output.h | 4 +- esphome/components/light/automation.h | 16 ++--- esphome/components/mhz19/mhz19.h | 9 ++- esphome/components/mqtt/mqtt_client.h | 10 +-- esphome/components/output/automation.h | 11 ++-- esphome/components/pid/pid_climate.h | 12 ++-- esphome/components/remote_base/jvc_protocol.h | 4 +- esphome/components/remote_base/lg_protocol.h | 4 +- esphome/components/remote_base/nec_protocol.h | 4 +- .../remote_base/panasonic_protocol.h | 4 +- .../components/remote_base/pioneer_protocol.h | 4 +- esphome/components/remote_base/raw_protocol.h | 4 +- esphome/components/remote_base/rc5_protocol.h | 4 +- .../remote_base/rc_switch_protocol.h | 15 +++-- esphome/components/remote_base/remote_base.h | 16 ++--- .../components/remote_base/samsung_protocol.h | 4 +- .../components/remote_base/sony_protocol.h | 4 +- esphome/components/rf_bridge/rf_bridge.h | 8 +-- .../rotary_encoder/rotary_encoder.h | 2 +- esphome/components/script/script.h | 18 ++--- esphome/components/sensor/automation.h | 2 +- esphome/components/servo/servo.h | 4 +- esphome/components/sim800l/sim800l.h | 4 +- esphome/components/stepper/stepper.h | 12 ++-- esphome/components/switch/automation.h | 14 ++-- esphome/components/text_sensor/automation.h | 2 +- esphome/components/tm1651/tm1651.h | 17 +++-- esphome/components/uart/automation.h | 5 +- esphome/core/automation.h | 51 ++++++++------- esphome/core/base_automation.h | 65 ++++++++++--------- 42 files changed, 250 insertions(+), 203 deletions(-) diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index d68dac3b6..0877efbf2 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -29,7 +29,9 @@ template class HomeAssistantServiceCallAction : public Action void add_variable(std::string key, T value) { this->variables_.push_back(TemplatableKeyValuePair(key, value)); } - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { HomeassistantServiceResponse resp; resp.service = this->service_.value(x...); resp.is_event = this->is_event_; @@ -54,7 +56,6 @@ template class HomeAssistantServiceCallAction : public Actionparent_->send_homeassistant_service_call(resp); } - protected: APIServer *parent_; bool is_event_; std::vector> data_; diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index e9ff37446..b76fbdae0 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -137,12 +137,13 @@ template class BinarySensorPublishAction : public Action public: explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(bool, state) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto val = this->state_.value(x...); this->sensor_->publish_state(val); } - protected: BinarySensor *sensor_; }; diff --git a/esphome/components/climate/automation.h b/esphome/components/climate/automation.h index 0cd52b103..2fd9d81e6 100644 --- a/esphome/components/climate/automation.h +++ b/esphome/components/climate/automation.h @@ -18,7 +18,8 @@ template class ControlAction : public Action { TEMPLATABLE_VALUE(ClimateFanMode, fan_mode) TEMPLATABLE_VALUE(ClimateSwingMode, swing_mode) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto call = this->climate_->make_call(); call.set_mode(this->mode_.optional_value(x...)); call.set_target_temperature(this->target_temperature_.optional_value(x...)); @@ -30,7 +31,6 @@ template class ControlAction : public Action { call.perform(); } - protected: Climate *climate_; }; diff --git a/esphome/components/cover/automation.h b/esphome/components/cover/automation.h index a8eb0cdf9..bad641c15 100644 --- a/esphome/components/cover/automation.h +++ b/esphome/components/cover/automation.h @@ -11,9 +11,9 @@ template class OpenAction : public Action { public: explicit OpenAction(Cover *cover) : cover_(cover) {} - void play(Ts... x) override { this->cover_->open(); } - protected: + void play_(Ts... x) override { this->cover_->open(); } + Cover *cover_; }; @@ -21,9 +21,9 @@ template class CloseAction : public Action { public: explicit CloseAction(Cover *cover) : cover_(cover) {} - void play(Ts... x) override { this->cover_->close(); } - protected: + void play_(Ts... x) override { this->cover_->close(); } + Cover *cover_; }; @@ -31,9 +31,9 @@ template class StopAction : public Action { public: explicit StopAction(Cover *cover) : cover_(cover) {} - void play(Ts... x) override { this->cover_->stop(); } - protected: + void play_(Ts... x) override { this->cover_->stop(); } + Cover *cover_; }; @@ -41,7 +41,12 @@ template class ControlAction : public Action { public: explicit ControlAction(Cover *cover) : cover_(cover) {} - void play(Ts... x) override { + TEMPLATABLE_VALUE(bool, stop) + TEMPLATABLE_VALUE(float, position) + TEMPLATABLE_VALUE(float, tilt) + + protected: + void play_(Ts... x) override { auto call = this->cover_->make_call(); if (this->stop_.has_value()) call.set_stop(this->stop_.value(x...)); @@ -52,18 +57,18 @@ template class ControlAction : public Action { call.perform(); } - TEMPLATABLE_VALUE(bool, stop) - TEMPLATABLE_VALUE(float, position) - TEMPLATABLE_VALUE(float, tilt) - - protected: Cover *cover_; }; template class CoverPublishAction : public Action { public: CoverPublishAction(Cover *cover) : cover_(cover) {} - void play(Ts... x) override { + TEMPLATABLE_VALUE(float, position) + TEMPLATABLE_VALUE(float, tilt) + TEMPLATABLE_VALUE(CoverOperation, current_operation) + + protected: + void play_(Ts... x) override { if (this->position_.has_value()) this->cover_->position = this->position_.value(x...); if (this->tilt_.has_value()) @@ -73,11 +78,6 @@ template class CoverPublishAction : public Action { this->cover_->publish_state(); } - TEMPLATABLE_VALUE(float, position) - TEMPLATABLE_VALUE(float, tilt) - TEMPLATABLE_VALUE(CoverOperation, current_operation) - - protected: Cover *cover_; }; diff --git a/esphome/components/deep_sleep/deep_sleep_component.h b/esphome/components/deep_sleep/deep_sleep_component.h index 4372a3f66..6287a2259 100644 --- a/esphome/components/deep_sleep/deep_sleep_component.h +++ b/esphome/components/deep_sleep/deep_sleep_component.h @@ -85,9 +85,9 @@ template class EnterDeepSleepAction : public Action { public: EnterDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {} - void play(Ts... x) override { this->deep_sleep_->begin_sleep(true); } - protected: + void play_(Ts... x) override { this->deep_sleep_->begin_sleep(true); } + DeepSleepComponent *deep_sleep_; }; @@ -95,9 +95,9 @@ template class PreventDeepSleepAction : public Action { public: PreventDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {} - void play(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); } - protected: + void play_(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); } + DeepSleepComponent *deep_sleep_; }; diff --git a/esphome/components/dfplayer/dfplayer.h b/esphome/components/dfplayer/dfplayer.h index 22ca11c3b..89f0fb691 100644 --- a/esphome/components/dfplayer/dfplayer.h +++ b/esphome/components/dfplayer/dfplayer.h @@ -104,8 +104,8 @@ class DFPlayer : public uart::UARTDevice, public Component { #define DFPLAYER_SIMPLE_ACTION(ACTION_CLASS, ACTION_METHOD) \ template class ACTION_CLASS : public Action, public Parented { \ - public: \ - void play(Ts... x) override { this->parent_->ACTION_METHOD(); } \ + protected: \ + void play_(Ts... x) override { this->parent_->ACTION_METHOD(); } \ }; DFPLAYER_SIMPLE_ACTION(NextAction, next) @@ -115,7 +115,9 @@ template class PlayFileAction : public Action, public Par public: TEMPLATABLE_VALUE(uint16_t, file) TEMPLATABLE_VALUE(boolean, loop) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto file = this->file_.value(x...); auto loop = this->loop_.value(x...); if (loop) { @@ -131,7 +133,9 @@ template class PlayFolderAction : public Action, public P TEMPLATABLE_VALUE(uint16_t, folder) TEMPLATABLE_VALUE(uint16_t, file) TEMPLATABLE_VALUE(boolean, loop) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto folder = this->folder_.value(x...); auto file = this->file_.value(x...); auto loop = this->loop_.value(x...); @@ -146,7 +150,9 @@ template class PlayFolderAction : public Action, public P template class SetDeviceAction : public Action, public Parented { public: TEMPLATABLE_VALUE(Device, device) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto device = this->device_.value(x...); this->parent_->set_device(device); } @@ -155,7 +161,9 @@ template class SetDeviceAction : public Action, public Pa template class SetVolumeAction : public Action, public Parented { public: TEMPLATABLE_VALUE(uint8_t, volume) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto volume = this->volume_.value(x...); this->parent_->set_volume(volume); } @@ -164,7 +172,9 @@ template class SetVolumeAction : public Action, public Pa template class SetEqAction : public Action, public Parented { public: TEMPLATABLE_VALUE(EqPreset, eq) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto eq = this->eq_.value(x...); this->parent_->set_eq(eq); } diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index b12fad8c8..2a6ead330 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -391,7 +391,9 @@ class Image { template class DisplayPageShowAction : public Action { public: TEMPLATABLE_VALUE(DisplayPage *, page) - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto *page = this->page_.value(x...); if (page != nullptr) { page->show(); @@ -402,18 +404,20 @@ template class DisplayPageShowAction : public Action { template class DisplayPageShowNextAction : public Action { public: DisplayPageShowNextAction(DisplayBuffer *buffer) : buffer_(buffer) {} - void play(Ts... x) override { this->buffer_->show_next_page(); } protected: + void play_(Ts... x) override { this->buffer_->show_next_page(); } + DisplayBuffer *buffer_; }; template class DisplayPageShowPrevAction : public Action { public: DisplayPageShowPrevAction(DisplayBuffer *buffer) : buffer_(buffer) {} - void play(Ts... x) override { this->buffer_->show_prev_page(); } protected: + void play_(Ts... x) override { this->buffer_->show_prev_page(); } + DisplayBuffer *buffer_; }; diff --git a/esphome/components/esp8266_pwm/esp8266_pwm.h b/esphome/components/esp8266_pwm/esp8266_pwm.h index b6839985b..5871b0dcb 100644 --- a/esphome/components/esp8266_pwm/esp8266_pwm.h +++ b/esphome/components/esp8266_pwm/esp8266_pwm.h @@ -38,12 +38,12 @@ template class SetFrequencyAction : public Action { SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {} TEMPLATABLE_VALUE(float, frequency); - void play(Ts... x) { + protected: + void play_(Ts... x) { float freq = this->frequency_.value(x...); this->parent_->update_frequency(freq); } - protected: ESP8266PWM *parent_; }; diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index dfa72a3ea..f2435a220 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -14,7 +14,8 @@ template class TurnOnAction : public Action { TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(FanSpeed, speed) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto call = this->state_->turn_on(); if (this->oscillating_.has_value()) { call.set_oscillating(this->oscillating_.value(x...)); @@ -25,7 +26,6 @@ template class TurnOnAction : public Action { call.perform(); } - protected: FanState *state_; }; @@ -33,9 +33,9 @@ template class TurnOffAction : public Action { public: explicit TurnOffAction(FanState *state) : state_(state) {} - void play(Ts... x) override { this->state_->turn_off().perform(); } - protected: + void play_(Ts... x) override { this->state_->turn_off().perform(); } + FanState *state_; }; @@ -43,9 +43,9 @@ template class ToggleAction : public Action { public: explicit ToggleAction(FanState *state) : state_(state) {} - void play(Ts... x) override { this->state_->toggle().perform(); } - protected: + void play_(Ts... x) override { this->state_->toggle().perform(); } + FanState *state_; }; diff --git a/esphome/components/globals/globals_component.h b/esphome/components/globals/globals_component.h index 397c55f6c..bf839b425 100644 --- a/esphome/components/globals/globals_component.h +++ b/esphome/components/globals/globals_component.h @@ -59,9 +59,9 @@ template class GlobalVarSetAction : public Actionparent_->value() = this->value_.value(x...); } - protected: + void play_(Ts... x) override { this->parent_->value() = this->value_.value(x...); } + C *parent_; }; diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index e6c0510b3..4f164f340 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -71,7 +71,8 @@ template class HttpRequestSendAction : public Action { void set_json(std::function json_func) { this->json_func_ = json_func; } - void play(Ts... x) override { + protected: + void play_(Ts... x) override { this->parent_->set_url(this->url_.value(x...)); this->parent_->set_method(this->method_.value(x...)); if (this->body_.has_value()) { @@ -106,7 +107,6 @@ template class HttpRequestSendAction : public Action { this->parent_->close(); } - protected: void encode_json_(Ts... x, JsonObject &root) { for (const auto &item : this->json_) { auto val = item.second; diff --git a/esphome/components/integration/integration_sensor.h b/esphome/components/integration/integration_sensor.h index 2fcec069b..85a89f6e4 100644 --- a/esphome/components/integration/integration_sensor.h +++ b/esphome/components/integration/integration_sensor.h @@ -76,9 +76,9 @@ template class ResetAction : public Action { public: explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {} - void play(Ts... x) override { this->parent_->reset(); } - protected: + void play_(Ts... x) override { this->parent_->reset(); } + IntegrationSensor *parent_; }; diff --git a/esphome/components/ledc/ledc_output.h b/esphome/components/ledc/ledc_output.h index 3f56f502b..7ff0987e0 100644 --- a/esphome/components/ledc/ledc_output.h +++ b/esphome/components/ledc/ledc_output.h @@ -43,12 +43,12 @@ template class SetFrequencyAction : public Action { SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {} TEMPLATABLE_VALUE(float, frequency); - void play(Ts... x) { + protected: + void play_(Ts... x) { float freq = this->frequency_.value(x...); this->parent_->apply_frequency(freq); } - protected: LEDCOutput *parent_; }; diff --git a/esphome/components/light/automation.h b/esphome/components/light/automation.h index dfab78065..1c641562d 100644 --- a/esphome/components/light/automation.h +++ b/esphome/components/light/automation.h @@ -13,13 +13,13 @@ template class ToggleAction : public Action { TEMPLATABLE_VALUE(uint32_t, transition_length) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto call = this->state_->toggle(); call.set_transition_length(this->transition_length_.optional_value(x...)); call.perform(); } - protected: LightState *state_; }; @@ -38,7 +38,8 @@ template class LightControlAction : public Action { TEMPLATABLE_VALUE(float, color_temperature) TEMPLATABLE_VALUE(std::string, effect) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto call = this->parent_->make_call(); call.set_state(this->state_.optional_value(x...)); call.set_brightness(this->brightness_.optional_value(x...)); @@ -53,7 +54,6 @@ template class LightControlAction : public Action { call.perform(); } - protected: LightState *parent_; }; @@ -64,7 +64,8 @@ template class DimRelativeAction : public Action { TEMPLATABLE_VALUE(float, relative_brightness) TEMPLATABLE_VALUE(uint32_t, transition_length) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto call = this->parent_->make_call(); float rel = this->relative_brightness_.value(x...); float cur; @@ -77,7 +78,6 @@ template class DimRelativeAction : public Action { call.perform(); } - protected: LightState *parent_; }; @@ -143,7 +143,8 @@ template class AddressableSet : public Action { TEMPLATABLE_VALUE(uint8_t, blue) TEMPLATABLE_VALUE(uint8_t, white) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto *out = (AddressableLight *) this->parent_->get_output(); int32_t range_from = this->range_from_.value_or(x..., 0); int32_t range_to = this->range_to_.value_or(x..., out->size() - 1) + 1; @@ -159,7 +160,6 @@ template class AddressableSet : public Action { out->schedule_show(); } - protected: LightState *parent_; }; diff --git a/esphome/components/mhz19/mhz19.h b/esphome/components/mhz19/mhz19.h index 2201fc87f..bdb2c50d1 100644 --- a/esphome/components/mhz19/mhz19.h +++ b/esphome/components/mhz19/mhz19.h @@ -37,27 +37,30 @@ class MHZ19Component : public PollingComponent, public uart::UARTDevice { template class MHZ19CalibrateZeroAction : public Action { public: MHZ19CalibrateZeroAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - void play(Ts... x) override { this->mhz19_->calibrate_zero(); } protected: + void play_(Ts... x) override { this->mhz19_->calibrate_zero(); } + MHZ19Component *mhz19_; }; template class MHZ19ABCEnableAction : public Action { public: MHZ19ABCEnableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - void play(Ts... x) override { this->mhz19_->abc_enable(); } protected: + void play_(Ts... x) override { this->mhz19_->abc_enable(); } + MHZ19Component *mhz19_; }; template class MHZ19ABCDisableAction : public Action { public: MHZ19ABCDisableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - void play(Ts... x) override { this->mhz19_->abc_disable(); } protected: + void play_(Ts... x) override { this->mhz19_->abc_disable(); } + MHZ19Component *mhz19_; }; diff --git a/esphome/components/mqtt/mqtt_client.h b/esphome/components/mqtt/mqtt_client.h index 6f14b0c92..fbd2435bf 100644 --- a/esphome/components/mqtt/mqtt_client.h +++ b/esphome/components/mqtt/mqtt_client.h @@ -299,12 +299,12 @@ template class MQTTPublishAction : public Action { TEMPLATABLE_VALUE(uint8_t, qos) TEMPLATABLE_VALUE(bool, retain) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { this->parent_->publish(this->topic_.value(x...), this->payload_.value(x...), this->qos_.value(x...), this->retain_.value(x...)); } - protected: MQTTClientComponent *parent_; }; @@ -316,15 +316,15 @@ template class MQTTPublishJsonAction : public Action { TEMPLATABLE_VALUE(bool, retain) void set_payload(std::function payload) { this->payload_ = payload; } - void play(Ts... x) override { + + protected: + void play_(Ts... x) override { auto f = std::bind(&MQTTPublishJsonAction::encode_, this, x..., std::placeholders::_1); auto topic = this->topic_.value(x...); auto qos = this->qos_.value(x...); auto retain = this->retain_.value(x...); this->parent_->publish_json(topic, f, qos, retain); } - - protected: void encode_(Ts... x, JsonObject &root) { this->payload_(x..., root); } std::function payload_; MQTTClientComponent *parent_; diff --git a/esphome/components/output/automation.h b/esphome/components/output/automation.h index 8c8a5ab61..c65b0d3f6 100644 --- a/esphome/components/output/automation.h +++ b/esphome/components/output/automation.h @@ -12,9 +12,9 @@ template class TurnOffAction : public Action { public: TurnOffAction(BinaryOutput *output) : output_(output) {} - void play(Ts... x) override { this->output_->turn_off(); } - protected: + void play_(Ts... x) override { this->output_->turn_off(); } + BinaryOutput *output_; }; @@ -22,9 +22,9 @@ template class TurnOnAction : public Action { public: TurnOnAction(BinaryOutput *output) : output_(output) {} - void play(Ts... x) override { this->output_->turn_on(); } - protected: + void play_(Ts... x) override { this->output_->turn_on(); } + BinaryOutput *output_; }; @@ -33,9 +33,10 @@ template class SetLevelAction : public Action { SetLevelAction(FloatOutput *output) : output_(output) {} TEMPLATABLE_VALUE(float, level) - void play(Ts... x) override { this->output_->set_level(this->level_.value(x...)); } protected: + void play_(Ts... x) override { this->output_->set_level(this->level_.value(x...)); } + FloatOutput *output_; }; diff --git a/esphome/components/pid/pid_climate.h b/esphome/components/pid/pid_climate.h index 8f379c47b..0c92cee79 100644 --- a/esphome/components/pid/pid_climate.h +++ b/esphome/components/pid/pid_climate.h @@ -71,7 +71,12 @@ template class PIDAutotuneAction : public Action { public: PIDAutotuneAction(PIDClimate *parent) : parent_(parent) {} - void play(Ts... x) { + void set_noiseband(float noiseband) { noiseband_ = noiseband; } + void set_positive_output(float positive_output) { positive_output_ = positive_output; } + void set_negative_output(float negative_output) { negative_output_ = negative_output; } + + protected: + void play_(Ts... x) { auto tuner = make_unique(); tuner->set_noiseband(this->noiseband_); tuner->set_output_negative(this->negative_output_); @@ -79,11 +84,6 @@ template class PIDAutotuneAction : public Action { this->parent_->start_autotune(std::move(tuner)); } - void set_noiseband(float noiseband) { noiseband_ = noiseband; } - void set_positive_output(float positive_output) { positive_output_ = positive_output; } - void set_negative_output(float negative_output) { negative_output_ = negative_output; } - - protected: float noiseband_; float positive_output_; float negative_output_; diff --git a/esphome/components/remote_base/jvc_protocol.h b/esphome/components/remote_base/jvc_protocol.h index 8a216f534..af666bf13 100644 --- a/esphome/components/remote_base/jvc_protocol.h +++ b/esphome/components/remote_base/jvc_protocol.h @@ -23,7 +23,9 @@ DECLARE_REMOTE_PROTOCOL(JVC) template class JVCAction : public RemoteTransmitterActionBase { public: TEMPLATABLE_VALUE(uint32_t, data) - void encode(RemoteTransmitData *dst, Ts... x) override { + + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { JVCData data{}; data.data = this->data_.value(x...); JVCProtocol().encode(dst, data); diff --git a/esphome/components/remote_base/lg_protocol.h b/esphome/components/remote_base/lg_protocol.h index b810115f5..a2e9489d0 100644 --- a/esphome/components/remote_base/lg_protocol.h +++ b/esphome/components/remote_base/lg_protocol.h @@ -26,7 +26,9 @@ template class LGAction : public RemoteTransmitterActionBasedata_.value(x...); data.nbits = this->nbits_.value(x...); diff --git a/esphome/components/remote_base/nec_protocol.h b/esphome/components/remote_base/nec_protocol.h index c794991ea..8c1db632b 100644 --- a/esphome/components/remote_base/nec_protocol.h +++ b/esphome/components/remote_base/nec_protocol.h @@ -25,7 +25,9 @@ template class NECAction : public RemoteTransmitterActionBaseaddress_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/panasonic_protocol.h b/esphome/components/remote_base/panasonic_protocol.h index b13bd3e92..1a09b2ffd 100644 --- a/esphome/components/remote_base/panasonic_protocol.h +++ b/esphome/components/remote_base/panasonic_protocol.h @@ -26,7 +26,9 @@ template class PanasonicAction : public RemoteTransmitterActionB public: TEMPLATABLE_VALUE(uint16_t, address) TEMPLATABLE_VALUE(uint32_t, command) - void encode(RemoteTransmitData *dst, Ts... x) override { + + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { PanasonicData data{}; data.address = this->address_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/pioneer_protocol.h b/esphome/components/remote_base/pioneer_protocol.h index f93e51a03..876176223 100644 --- a/esphome/components/remote_base/pioneer_protocol.h +++ b/esphome/components/remote_base/pioneer_protocol.h @@ -25,7 +25,9 @@ template class PioneerAction : public RemoteTransmitterActionBas public: TEMPLATABLE_VALUE(uint16_t, rc_code_1) TEMPLATABLE_VALUE(uint16_t, rc_code_2) - void encode(RemoteTransmitData *dst, Ts... x) override { + + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { PioneerData data{}; data.rc_code_1 = this->rc_code_1_.value(x...); data.rc_code_2 = this->rc_code_2_.value(x...); diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 1d9f1c5ac..39da5fa8f 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -46,7 +46,8 @@ template class RawAction : public RemoteTransmitterActionBasecode_static_ != nullptr) { for (size_t i = 0; i < this->code_static_len_; i++) { auto val = this->code_static_[i]; @@ -61,7 +62,6 @@ template class RawAction : public RemoteTransmitterActionBaseset_carrier_frequency(this->carrier_frequency_.value(x...)); } - protected: std::function(Ts...)> code_func_{}; const int32_t *code_static_{nullptr}; int32_t code_static_len_{0}; diff --git a/esphome/components/remote_base/rc5_protocol.h b/esphome/components/remote_base/rc5_protocol.h index 2e1da74d9..f90284c23 100644 --- a/esphome/components/remote_base/rc5_protocol.h +++ b/esphome/components/remote_base/rc5_protocol.h @@ -26,7 +26,9 @@ template class RC5Action : public RemoteTransmitterActionBaseaddress_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/rc_switch_protocol.h b/esphome/components/remote_base/rc_switch_protocol.h index 8362899ce..480f49ea9 100644 --- a/esphome/components/remote_base/rc_switch_protocol.h +++ b/esphome/components/remote_base/rc_switch_protocol.h @@ -71,7 +71,8 @@ template class RCSwitchRawAction : public RemoteTransmitterActio TEMPLATABLE_VALUE(RCSwitchBase, protocol); TEMPLATABLE_VALUE(std::string, code); - void encode(RemoteTransmitData *dst, Ts... x) override { + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { auto code = this->code_.value(x...); uint64_t the_code = decode_binary_string(code); uint8_t nbits = code.size(); @@ -88,7 +89,8 @@ template class RCSwitchTypeAAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(std::string, device); TEMPLATABLE_VALUE(bool, state); - void encode(RemoteTransmitData *dst, Ts... x) override { + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { auto group = this->group_.value(x...); auto device = this->device_.value(x...); auto state = this->state_.value(x...); @@ -111,7 +113,8 @@ template class RCSwitchTypeBAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, channel); TEMPLATABLE_VALUE(bool, state); - void encode(RemoteTransmitData *dst, Ts... x) override { + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { auto address = this->address_.value(x...); auto channel = this->channel_.value(x...); auto state = this->state_.value(x...); @@ -133,7 +136,8 @@ template class RCSwitchTypeCAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, device); TEMPLATABLE_VALUE(bool, state); - void encode(RemoteTransmitData *dst, Ts... x) override { + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { auto family = this->family_.value(x...); auto group = this->group_.value(x...); auto device = this->device_.value(x...); @@ -156,7 +160,8 @@ template class RCSwitchTypeDAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, device); TEMPLATABLE_VALUE(bool, state); - void encode(RemoteTransmitData *dst, Ts... x) override { + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { auto group = this->group_.value(x...); auto device = this->device_.value(x...); auto state = this->state_.value(x...); diff --git a/esphome/components/remote_base/remote_base.h b/esphome/components/remote_base/remote_base.h index 250b59e55..c9cae96c7 100644 --- a/esphome/components/remote_base/remote_base.h +++ b/esphome/components/remote_base/remote_base.h @@ -323,20 +323,20 @@ template class RemoteTransmitterActionBase : public Actionparent_ = parent; } - void play(Ts... x) override { + TEMPLATABLE_VALUE(uint32_t, send_times); + TEMPLATABLE_VALUE(uint32_t, send_wait); + + protected: + virtual void encode_(RemoteTransmitData *dst, Ts... x) = 0; + + void play_(Ts... x) override { auto call = this->parent_->transmit(); - this->encode(call.get_data(), x...); + this->encode_(call.get_data(), x...); call.set_send_times(this->send_times_.value_or(x..., 1)); call.set_send_wait(this->send_wait_.value_or(x..., 0)); call.perform(); } - virtual void encode(RemoteTransmitData *dst, Ts... x) = 0; - - TEMPLATABLE_VALUE(uint32_t, send_times); - TEMPLATABLE_VALUE(uint32_t, send_wait); - - protected: RemoteTransmitterBase *parent_{}; }; diff --git a/esphome/components/remote_base/samsung_protocol.h b/esphome/components/remote_base/samsung_protocol.h index 50ff02c1a..0e1666b3b 100644 --- a/esphome/components/remote_base/samsung_protocol.h +++ b/esphome/components/remote_base/samsung_protocol.h @@ -24,7 +24,9 @@ DECLARE_REMOTE_PROTOCOL(Samsung) template class SamsungAction : public RemoteTransmitterActionBase { public: TEMPLATABLE_VALUE(uint32_t, data) - void encode(RemoteTransmitData *dst, Ts... x) override { + + protected: + void encode_(RemoteTransmitData *dst, Ts... x) override { SamsungData data{}; data.data = this->data_.value(x...); SamsungProtocol().encode(dst, data); diff --git a/esphome/components/remote_base/sony_protocol.h b/esphome/components/remote_base/sony_protocol.h index 9f0bcdf82..a122d3758 100644 --- a/esphome/components/remote_base/sony_protocol.h +++ b/esphome/components/remote_base/sony_protocol.h @@ -26,7 +26,9 @@ template class SonyAction : public RemoteTransmitterActionBasedata_.value(x...); data.nbits = this->nbits_.value(x...); diff --git a/esphome/components/rf_bridge/rf_bridge.h b/esphome/components/rf_bridge/rf_bridge.h index 86713b8a5..c747be997 100644 --- a/esphome/components/rf_bridge/rf_bridge.h +++ b/esphome/components/rf_bridge/rf_bridge.h @@ -68,7 +68,8 @@ template class RFBridgeSendCodeAction : public Action { TEMPLATABLE_VALUE(uint16_t, high) TEMPLATABLE_VALUE(uint32_t, code) - void play(Ts... x) { + protected: + void play_(Ts... x) { RFBridgeData data{}; data.sync = this->sync_.value(x...); data.low = this->low_.value(x...); @@ -77,7 +78,6 @@ template class RFBridgeSendCodeAction : public Action { this->parent_->send_code(data); } - protected: RFBridgeComponent *parent_; }; @@ -85,9 +85,9 @@ template class RFBridgeLearnAction : public Action { public: RFBridgeLearnAction(RFBridgeComponent *parent) : parent_(parent) {} - void play(Ts... x) { this->parent_->learn(); } - protected: + void play_(Ts... x) { this->parent_->learn(); } + RFBridgeComponent *parent_; }; diff --git a/esphome/components/rotary_encoder/rotary_encoder.h b/esphome/components/rotary_encoder/rotary_encoder.h index 422064547..0bbcf21dd 100644 --- a/esphome/components/rotary_encoder/rotary_encoder.h +++ b/esphome/components/rotary_encoder/rotary_encoder.h @@ -74,9 +74,9 @@ template class RotaryEncoderSetValueAction : public Actionencoder_->set_value(this->value_.value(x...)); } protected: + void play_(Ts... x) override { this->encoder_->set_value(this->value_.value(x...)); } RotaryEncoderSensor *encoder_; }; diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index a6b208167..92eba15e4 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -23,9 +23,9 @@ template class ScriptExecuteAction : public Action { public: ScriptExecuteAction(Script *script) : script_(script) {} - void play(Ts... x) override { this->script_->trigger(); } - protected: + void play_(Ts... x) override { this->script_->trigger(); } + Script *script_; }; @@ -33,9 +33,9 @@ template class ScriptStopAction : public Action { public: ScriptStopAction(Script *script) : script_(script) {} - void play(Ts... x) override { this->script_->stop(); } - protected: + void play_(Ts... x) override { this->script_->stop(); } + Script *script_; }; @@ -53,14 +53,11 @@ template class ScriptWaitAction : public Action, public C public: ScriptWaitAction(Script *script) : script_(script) {} - void play(Ts... x) override { /* ignore - see play_complex */ - } - void play_complex(Ts... x) override { this->num_running_++; // Check if we can continue immediately. if (!this->script_->is_running()) { - this->play_next(x...); + this->play_next_(x...); return; } this->var_ = std::make_tuple(x...); @@ -74,12 +71,15 @@ template class ScriptWaitAction : public Action, public C if (this->script_->is_running()) return; - this->play_next_tuple(this->var_); + this->play_next_tuple_(this->var_); } float get_setup_priority() const override { return setup_priority::DATA; } protected: + void play_(Ts... x) override { /* ignore - see play_complex */ + } + Script *script_; std::tuple var_{}; }; diff --git a/esphome/components/sensor/automation.h b/esphome/components/sensor/automation.h index 079077dba..e6ddc3b95 100644 --- a/esphome/components/sensor/automation.h +++ b/esphome/components/sensor/automation.h @@ -25,9 +25,9 @@ template class SensorPublishAction : public Action { public: SensorPublishAction(Sensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(float, state) - void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } protected: + void play_(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } Sensor *sensor_; }; diff --git a/esphome/components/servo/servo.h b/esphome/components/servo/servo.h index a37188740..b4076e8cd 100644 --- a/esphome/components/servo/servo.h +++ b/esphome/components/servo/servo.h @@ -64,18 +64,18 @@ template class ServoWriteAction : public Action { public: ServoWriteAction(Servo *servo) : servo_(servo) {} TEMPLATABLE_VALUE(float, value) - void play(Ts... x) override { this->servo_->write(this->value_.value(x...)); } protected: + void play_(Ts... x) override { this->servo_->write(this->value_.value(x...)); } Servo *servo_; }; template class ServoDetachAction : public Action { public: ServoDetachAction(Servo *servo) : servo_(servo) {} - void play(Ts... x) override { this->servo_->detach(); } protected: + void play_(Ts... x) override { this->servo_->detach(); } Servo *servo_; }; diff --git a/esphome/components/sim800l/sim800l.h b/esphome/components/sim800l/sim800l.h index 696eb8890..afdb50468 100644 --- a/esphome/components/sim800l/sim800l.h +++ b/esphome/components/sim800l/sim800l.h @@ -78,13 +78,13 @@ template class Sim800LSendSmsAction : public Action { TEMPLATABLE_VALUE(std::string, recipient) TEMPLATABLE_VALUE(std::string, message) - void play(Ts... x) { + protected: + void play_(Ts... x) { auto recipient = this->recipient_.value(x...); auto message = this->message_.value(x...); this->parent_->send_sms(recipient, message); } - protected: Sim800LComponent *parent_; }; diff --git a/esphome/components/stepper/stepper.h b/esphome/components/stepper/stepper.h index 33777dce8..31bdf59bf 100644 --- a/esphome/components/stepper/stepper.h +++ b/esphome/components/stepper/stepper.h @@ -43,9 +43,9 @@ template class SetTargetAction : public Action { TEMPLATABLE_VALUE(int32_t, target) - void play(Ts... x) override { this->parent_->set_target(this->target_.value(x...)); } - protected: + void play_(Ts... x) override { this->parent_->set_target(this->target_.value(x...)); } + Stepper *parent_; }; @@ -55,9 +55,9 @@ template class ReportPositionAction : public Action { TEMPLATABLE_VALUE(int32_t, position) - void play(Ts... x) override { this->parent_->report_position(this->position_.value(x...)); } - protected: + void play_(Ts... x) override { this->parent_->report_position(this->position_.value(x...)); } + Stepper *parent_; }; @@ -67,13 +67,13 @@ template class SetSpeedAction : public Action { TEMPLATABLE_VALUE(float, speed); - void play(Ts... x) override { + protected: + void play_(Ts... x) override { float speed = this->speed_.value(x...); this->parent_->set_max_speed(speed); this->parent_->on_update_speed(); } - protected: Stepper *parent_; }; diff --git a/esphome/components/switch/automation.h b/esphome/components/switch/automation.h index 90bdabf0f..c4824b106 100644 --- a/esphome/components/switch/automation.h +++ b/esphome/components/switch/automation.h @@ -11,9 +11,9 @@ template class TurnOnAction : public Action { public: explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {} - void play(Ts... x) override { this->switch_->turn_on(); } - protected: + void play_(Ts... x) override { this->switch_->turn_on(); } + Switch *switch_; }; @@ -21,9 +21,9 @@ template class TurnOffAction : public Action { public: explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {} - void play(Ts... x) override { this->switch_->turn_off(); } - protected: + void play_(Ts... x) override { this->switch_->turn_off(); } + Switch *switch_; }; @@ -31,9 +31,9 @@ template class ToggleAction : public Action { public: explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {} - void play(Ts... x) override { this->switch_->toggle(); } - protected: + void play_(Ts... x) override { this->switch_->toggle(); } + Switch *switch_; }; @@ -73,9 +73,9 @@ template class SwitchPublishAction : public Action { public: SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {} TEMPLATABLE_VALUE(bool, state) - void play(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); } protected: + void play_(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); } Switch *switch_; }; diff --git a/esphome/components/text_sensor/automation.h b/esphome/components/text_sensor/automation.h index 496efb1cc..9fac17c4c 100644 --- a/esphome/components/text_sensor/automation.h +++ b/esphome/components/text_sensor/automation.h @@ -30,9 +30,9 @@ template class TextSensorPublishAction : public Action { public: TextSensorPublishAction(TextSensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(std::string, state) - void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } protected: + void play_(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } TextSensor *sensor_; }; diff --git a/esphome/components/tm1651/tm1651.h b/esphome/components/tm1651/tm1651.h index 6eab24687..6291cf1ec 100644 --- a/esphome/components/tm1651/tm1651.h +++ b/esphome/components/tm1651/tm1651.h @@ -43,7 +43,8 @@ template class SetLevelPercentAction : public Action, pub public: TEMPLATABLE_VALUE(uint8_t, level_percent) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto level_percent = this->level_percent_.value(x...); this->parent_->set_level_percent(level_percent); } @@ -53,7 +54,8 @@ template class SetLevelAction : public Action, public Par public: TEMPLATABLE_VALUE(uint8_t, level) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto level = this->level_.value(x...); this->parent_->set_level(level); } @@ -63,20 +65,21 @@ template class SetBrightnessAction : public Action, publi public: TEMPLATABLE_VALUE(uint8_t, brightness) - void play(Ts... x) override { + protected: + void play_(Ts... x) override { auto brightness = this->brightness_.value(x...); this->parent_->set_brightness(brightness); } }; template class TurnOnAction : public Action, public Parented { - public: - void play(Ts... x) override { this->parent_->turn_on(); } + protected: + void play_(Ts... x) override { this->parent_->turn_on(); } }; template class TurnOffAction : public Action, public Parented { - public: - void play(Ts... x) override { this->parent_->turn_off(); } + protected: + void play_(Ts... x) override { this->parent_->turn_off(); } }; } // namespace tm1651 diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 9686f9441..6889bc12e 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -17,7 +17,8 @@ template class UARTWriteAction : public Action, public Pa this->static_ = true; } - void play(Ts... x) override { + protected: + void play_(Ts... x) override { if (this->static_) { this->parent_->write_array(this->data_static_); } else { @@ -25,8 +26,6 @@ template class UARTWriteAction : public Action, public Pa this->parent_->write_array(val); } } - - protected: bool static_{false}; std::function(Ts...)> data_func_{}; std::vector data_static_{}; diff --git a/esphome/core/automation.h b/esphome/core/automation.h index 6e595fc45..cb9536467 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -75,13 +75,25 @@ template class ActionList; template class Action { public: - virtual void play(Ts... x) = 0; virtual void play_complex(Ts... x) { this->num_running_++; - this->play(x...); - this->play_next(x...); + this->play_(x...); + this->play_next_(x...); } - void play_next(Ts... x) { + virtual void stop_complex() { + if (num_running_) { + this->stop_(); + this->num_running_ = 0; + } + this->stop_next_(); + } + virtual bool is_running() { return this->num_running_ > 0 || this->is_running_next_(); } + + protected: + friend ActionList; + + virtual void play_(Ts... x) = 0; + void play_next_(Ts... x) { if (this->num_running_ > 0) { this->num_running_--; if (this->next_ != nullptr) { @@ -89,37 +101,26 @@ template class Action { } } } - virtual void stop() {} - virtual void stop_complex() { - if (num_running_) { - this->stop(); - this->num_running_ = 0; - } - this->stop_next(); + template void play_next_tuple_(const std::tuple &tuple, seq) { + this->play_next_(std::get(tuple)...); } - void stop_next() { + void play_next_tuple_(const std::tuple &tuple) { + this->play_next_tuple_(tuple, typename gens::type()); + } + + virtual void stop_() {} + void stop_next_() { if (this->next_ != nullptr) { this->next_->stop_complex(); } } - virtual bool is_running() { return this->num_running_ > 0 || this->is_running_next(); } - bool is_running_next() { + + bool is_running_next_() { if (this->next_ == nullptr) return false; return this->next_->is_running(); } - void play_next_tuple(const std::tuple &tuple) { - this->play_next_tuple_(tuple, typename gens::type()); - } - - protected: - friend ActionList; - - template void play_next_tuple_(const std::tuple &tuple, seq) { - this->play_next(std::get(tuple)...); - } - Action *next_ = nullptr; int num_running_{0}; diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index bd3400979..4edd45942 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -108,27 +108,29 @@ template class DelayAction : public Action, public Compon TEMPLATABLE_VALUE(uint32_t, delay) - void stop() override { - this->cancel_timeout(""); - } - - void play(Ts... x) override { /* ignore - see play_complex */ - } - void play_complex(Ts... x) override { - auto f = std::bind(&Action::play_next, this, x...); + auto f = std::bind(&DelayAction::play_next_, this, x...); this->num_running_++; this->set_timeout(this->delay_.value(x...), f); } float get_setup_priority() const override { return setup_priority::HARDWARE; } + + protected: + void play_(Ts... x) override { /* ignore - see play_complex */ + } + + void stop_() override { + this->cancel_timeout(""); + } }; template class LambdaAction : public Action { public: explicit LambdaAction(std::function &&f) : f_(std::move(f)) {} - void play(Ts... x) override { this->f_(x...); } protected: + void play_(Ts... x) override { this->f_(x...); } + std::function f_; }; @@ -138,15 +140,12 @@ template class IfAction : public Action { void add_then(const std::vector *> &actions) { this->then_.add_actions(actions); - this->then_.add_action(new LambdaAction([this](Ts... x) { this->play_next(x...); })); + this->then_.add_action(new LambdaAction([this](Ts... x) { this->play_next_(x...); })); } void add_else(const std::vector *> &actions) { this->else_.add_actions(actions); - this->else_.add_action(new LambdaAction([this](Ts... x) { this->play_next(x...); })); - } - - void play(Ts... x) override { /* ignore - see play_complex */ + this->else_.add_action(new LambdaAction([this](Ts... x) { this->play_next_(x...); })); } void play_complex(Ts... x) override { @@ -154,25 +153,28 @@ template class IfAction : public Action { bool res = this->condition_->check(x...); if (res) { if (this->then_.empty()) { - this->play_next(x...); + this->play_next_(x...); } else if (this->num_running_ > 0) { this->then_.play(x...); } } else { if (this->else_.empty()) { - this->play_next(x...); + this->play_next_(x...); } else if (this->num_running_ > 0) { this->else_.play(x...); } } } - void stop() override { + protected: + void play_(Ts... x) override { /* ignore - see play_complex */ + } + + void stop_() override { this->then_.stop(); this->else_.stop(); } - protected: Condition *condition_; ActionList then_; ActionList else_; @@ -192,14 +194,11 @@ template class WhileAction : public Action { } } else { // condition false, play next - this->play_next_tuple(this->var_); + this->play_next_tuple_(this->var_); } })); } - void play(Ts... x) override { /* ignore - see play_complex */ - } - void play_complex(Ts... x) override { this->num_running_++; // Store loop parameters @@ -208,7 +207,7 @@ template class WhileAction : public Action { if (!this->condition_->check_tuple(this->var_)) { // If new condition check failed, stop loop if running this->then_.stop(); - this->play_next_tuple(this->var_); + this->play_next_tuple_(this->var_); return; } @@ -217,9 +216,12 @@ template class WhileAction : public Action { } } - void stop() override { this->then_.stop(); } - protected: + void play_(Ts... x) override { /* ignore - see play_complex */ + } + + void stop_() override { this->then_.stop(); } + Condition *condition_; ActionList then_; std::tuple var_{}; @@ -229,15 +231,12 @@ template class WaitUntilAction : public Action, public Co public: WaitUntilAction(Condition *condition) : condition_(condition) {} - void play(Ts... x) override { /* ignore - see play_complex */ - } - void play_complex(Ts... x) override { this->num_running_++; // Check if we can continue immediately. if (this->condition_->check(x...)) { if (this->num_running_ > 0) { - this->play_next(x...); + this->play_next_(x...); } return; } @@ -253,12 +252,15 @@ template class WaitUntilAction : public Action, public Co return; } - this->play_next_tuple(this->var_); + this->play_next_tuple_(this->var_); } float get_setup_priority() const override { return setup_priority::DATA; } protected: + void play_(Ts... x) override { /* ignore - see play_complex */ + } + Condition *condition_; std::tuple var_{}; }; @@ -266,9 +268,10 @@ template class WaitUntilAction : public Action, public Co template class UpdateComponentAction : public Action { public: UpdateComponentAction(PollingComponent *component) : component_(component) {} - void play(Ts... x) override { this->component_->update(); } protected: + void play_(Ts... x) override { this->component_->update(); } + PollingComponent *component_; }; From 1bec1faf6db9a7696e96c51152d92b94eda01b29 Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Sun, 24 May 2020 23:27:28 -0300 Subject: [PATCH 3/4] lint --- .../components/api/homeassistant_service.h | 4 +-- esphome/components/binary_sensor/automation.h | 4 +-- esphome/components/climate/automation.h | 4 +-- esphome/components/cover/automation.h | 20 ++++++------ .../deep_sleep/deep_sleep_component.h | 8 ++--- esphome/components/dfplayer/dfplayer.h | 18 ++++------- esphome/components/display/display_buffer.h | 9 ++---- esphome/components/esp8266_pwm/esp8266_pwm.h | 3 +- esphome/components/fan/automation.h | 9 ++---- .../components/globals/globals_component.h | 4 +-- .../components/http_request/http_request.h | 4 +-- .../integration/integration_sensor.h | 4 +-- esphome/components/ledc/ledc_output.h | 4 +-- esphome/components/light/automation.h | 16 +++++----- esphome/components/mhz19/mhz19.h | 12 +++---- esphome/components/mqtt/mqtt_client.h | 9 +++--- esphome/components/output/automation.h | 12 +++---- esphome/components/pid/pid_climate.h | 4 +-- esphome/components/remote_base/jvc_protocol.h | 3 +- esphome/components/remote_base/lg_protocol.h | 3 +- esphome/components/remote_base/nec_protocol.h | 3 +- .../remote_base/panasonic_protocol.h | 3 +- .../components/remote_base/pioneer_protocol.h | 3 +- esphome/components/remote_base/raw_protocol.h | 4 +-- esphome/components/remote_base/rc5_protocol.h | 3 +- .../remote_base/rc_switch_protocol.h | 15 +++------ esphome/components/remote_base/remote_base.h | 10 +++--- .../components/remote_base/samsung_protocol.h | 3 +- .../components/remote_base/sony_protocol.h | 3 +- esphome/components/rf_bridge/rf_bridge.h | 8 ++--- .../rotary_encoder/rotary_encoder.h | 3 +- esphome/components/script/script.h | 12 +++---- esphome/components/sensor/automation.h | 3 +- esphome/components/servo/servo.h | 6 ++-- esphome/components/sim800l/sim800l.h | 4 +-- esphome/components/stepper/stepper.h | 12 +++---- esphome/components/switch/automation.h | 15 ++++----- esphome/components/text_sensor/automation.h | 3 +- esphome/components/tm1651/tm1651.h | 17 +++++----- esphome/components/uart/automation.h | 4 +-- esphome/core/automation.h | 8 ++--- esphome/core/base_automation.h | 31 +++++++++---------- 42 files changed, 151 insertions(+), 176 deletions(-) diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 0877efbf2..8a7276519 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -30,8 +30,7 @@ template class HomeAssistantServiceCallAction : public Actionvariables_.push_back(TemplatableKeyValuePair(key, value)); } - protected: - void play_(Ts... x) override { + void play(Ts... x) override { HomeassistantServiceResponse resp; resp.service = this->service_.value(x...); resp.is_event = this->is_event_; @@ -56,6 +55,7 @@ template class HomeAssistantServiceCallAction : public Actionparent_->send_homeassistant_service_call(resp); } + protected: APIServer *parent_; bool is_event_; std::vector> data_; diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index b76fbdae0..6b0321628 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -138,12 +138,12 @@ template class BinarySensorPublishAction : public Action explicit BinarySensorPublishAction(BinarySensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(bool, state) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto val = this->state_.value(x...); this->sensor_->publish_state(val); } + protected: BinarySensor *sensor_; }; diff --git a/esphome/components/climate/automation.h b/esphome/components/climate/automation.h index 2fd9d81e6..0cd52b103 100644 --- a/esphome/components/climate/automation.h +++ b/esphome/components/climate/automation.h @@ -18,8 +18,7 @@ template class ControlAction : public Action { TEMPLATABLE_VALUE(ClimateFanMode, fan_mode) TEMPLATABLE_VALUE(ClimateSwingMode, swing_mode) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->climate_->make_call(); call.set_mode(this->mode_.optional_value(x...)); call.set_target_temperature(this->target_temperature_.optional_value(x...)); @@ -31,6 +30,7 @@ template class ControlAction : public Action { call.perform(); } + protected: Climate *climate_; }; diff --git a/esphome/components/cover/automation.h b/esphome/components/cover/automation.h index bad641c15..0092f987f 100644 --- a/esphome/components/cover/automation.h +++ b/esphome/components/cover/automation.h @@ -11,9 +11,9 @@ template class OpenAction : public Action { public: explicit OpenAction(Cover *cover) : cover_(cover) {} - protected: - void play_(Ts... x) override { this->cover_->open(); } + void play(Ts... x) override { this->cover_->open(); } + protected: Cover *cover_; }; @@ -21,9 +21,9 @@ template class CloseAction : public Action { public: explicit CloseAction(Cover *cover) : cover_(cover) {} - protected: - void play_(Ts... x) override { this->cover_->close(); } + void play(Ts... x) override { this->cover_->close(); } + protected: Cover *cover_; }; @@ -31,9 +31,9 @@ template class StopAction : public Action { public: explicit StopAction(Cover *cover) : cover_(cover) {} - protected: - void play_(Ts... x) override { this->cover_->stop(); } + void play(Ts... x) override { this->cover_->stop(); } + protected: Cover *cover_; }; @@ -45,8 +45,7 @@ template class ControlAction : public Action { TEMPLATABLE_VALUE(float, position) TEMPLATABLE_VALUE(float, tilt) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->cover_->make_call(); if (this->stop_.has_value()) call.set_stop(this->stop_.value(x...)); @@ -57,6 +56,7 @@ template class ControlAction : public Action { call.perform(); } + protected: Cover *cover_; }; @@ -67,8 +67,7 @@ template class CoverPublishAction : public Action { TEMPLATABLE_VALUE(float, tilt) TEMPLATABLE_VALUE(CoverOperation, current_operation) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { if (this->position_.has_value()) this->cover_->position = this->position_.value(x...); if (this->tilt_.has_value()) @@ -78,6 +77,7 @@ template class CoverPublishAction : public Action { this->cover_->publish_state(); } + protected: Cover *cover_; }; diff --git a/esphome/components/deep_sleep/deep_sleep_component.h b/esphome/components/deep_sleep/deep_sleep_component.h index 6287a2259..4372a3f66 100644 --- a/esphome/components/deep_sleep/deep_sleep_component.h +++ b/esphome/components/deep_sleep/deep_sleep_component.h @@ -85,9 +85,9 @@ template class EnterDeepSleepAction : public Action { public: EnterDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {} - protected: - void play_(Ts... x) override { this->deep_sleep_->begin_sleep(true); } + void play(Ts... x) override { this->deep_sleep_->begin_sleep(true); } + protected: DeepSleepComponent *deep_sleep_; }; @@ -95,9 +95,9 @@ template class PreventDeepSleepAction : public Action { public: PreventDeepSleepAction(DeepSleepComponent *deep_sleep) : deep_sleep_(deep_sleep) {} - protected: - void play_(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); } + void play(Ts... x) override { this->deep_sleep_->prevent_deep_sleep(); } + protected: DeepSleepComponent *deep_sleep_; }; diff --git a/esphome/components/dfplayer/dfplayer.h b/esphome/components/dfplayer/dfplayer.h index 89f0fb691..cb9686bb6 100644 --- a/esphome/components/dfplayer/dfplayer.h +++ b/esphome/components/dfplayer/dfplayer.h @@ -104,8 +104,7 @@ class DFPlayer : public uart::UARTDevice, public Component { #define DFPLAYER_SIMPLE_ACTION(ACTION_CLASS, ACTION_METHOD) \ template class ACTION_CLASS : public Action, public Parented { \ - protected: \ - void play_(Ts... x) override { this->parent_->ACTION_METHOD(); } \ + void play(Ts... x) override { this->parent_->ACTION_METHOD(); } \ }; DFPLAYER_SIMPLE_ACTION(NextAction, next) @@ -116,8 +115,7 @@ template class PlayFileAction : public Action, public Par TEMPLATABLE_VALUE(uint16_t, file) TEMPLATABLE_VALUE(boolean, loop) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto file = this->file_.value(x...); auto loop = this->loop_.value(x...); if (loop) { @@ -134,8 +132,7 @@ template class PlayFolderAction : public Action, public P TEMPLATABLE_VALUE(uint16_t, file) TEMPLATABLE_VALUE(boolean, loop) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto folder = this->folder_.value(x...); auto file = this->file_.value(x...); auto loop = this->loop_.value(x...); @@ -151,8 +148,7 @@ template class SetDeviceAction : public Action, public Pa public: TEMPLATABLE_VALUE(Device, device) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto device = this->device_.value(x...); this->parent_->set_device(device); } @@ -162,8 +158,7 @@ template class SetVolumeAction : public Action, public Pa public: TEMPLATABLE_VALUE(uint8_t, volume) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto volume = this->volume_.value(x...); this->parent_->set_volume(volume); } @@ -173,8 +168,7 @@ template class SetEqAction : public Action, public Parent public: TEMPLATABLE_VALUE(EqPreset, eq) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto eq = this->eq_.value(x...); this->parent_->set_eq(eq); } diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index 2a6ead330..85871df16 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -392,8 +392,7 @@ template class DisplayPageShowAction : public Action { public: TEMPLATABLE_VALUE(DisplayPage *, page) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto *page = this->page_.value(x...); if (page != nullptr) { page->show(); @@ -405,8 +404,7 @@ template class DisplayPageShowNextAction : public Action public: DisplayPageShowNextAction(DisplayBuffer *buffer) : buffer_(buffer) {} - protected: - void play_(Ts... x) override { this->buffer_->show_next_page(); } + void play(Ts... x) override { this->buffer_->show_next_page(); } DisplayBuffer *buffer_; }; @@ -415,8 +413,7 @@ template class DisplayPageShowPrevAction : public Action public: DisplayPageShowPrevAction(DisplayBuffer *buffer) : buffer_(buffer) {} - protected: - void play_(Ts... x) override { this->buffer_->show_prev_page(); } + void play(Ts... x) override { this->buffer_->show_prev_page(); } DisplayBuffer *buffer_; }; diff --git a/esphome/components/esp8266_pwm/esp8266_pwm.h b/esphome/components/esp8266_pwm/esp8266_pwm.h index 5871b0dcb..51b74f48b 100644 --- a/esphome/components/esp8266_pwm/esp8266_pwm.h +++ b/esphome/components/esp8266_pwm/esp8266_pwm.h @@ -38,8 +38,7 @@ template class SetFrequencyAction : public Action { SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {} TEMPLATABLE_VALUE(float, frequency); - protected: - void play_(Ts... x) { + void play(Ts... x) { float freq = this->frequency_.value(x...); this->parent_->update_frequency(freq); } diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index f2435a220..d96ed994e 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -14,8 +14,7 @@ template class TurnOnAction : public Action { TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(FanSpeed, speed) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->state_->turn_on(); if (this->oscillating_.has_value()) { call.set_oscillating(this->oscillating_.value(x...)); @@ -33,8 +32,7 @@ template class TurnOffAction : public Action { public: explicit TurnOffAction(FanState *state) : state_(state) {} - protected: - void play_(Ts... x) override { this->state_->turn_off().perform(); } + void play(Ts... x) override { this->state_->turn_off().perform(); } FanState *state_; }; @@ -43,8 +41,7 @@ template class ToggleAction : public Action { public: explicit ToggleAction(FanState *state) : state_(state) {} - protected: - void play_(Ts... x) override { this->state_->toggle().perform(); } + void play(Ts... x) override { this->state_->toggle().perform(); } FanState *state_; }; diff --git a/esphome/components/globals/globals_component.h b/esphome/components/globals/globals_component.h index bf839b425..397c55f6c 100644 --- a/esphome/components/globals/globals_component.h +++ b/esphome/components/globals/globals_component.h @@ -59,9 +59,9 @@ template class GlobalVarSetAction : public Actionparent_->value() = this->value_.value(x...); } + void play(Ts... x) override { this->parent_->value() = this->value_.value(x...); } + protected: C *parent_; }; diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index 4f164f340..e6c0510b3 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -71,8 +71,7 @@ template class HttpRequestSendAction : public Action { void set_json(std::function json_func) { this->json_func_ = json_func; } - protected: - void play_(Ts... x) override { + void play(Ts... x) override { this->parent_->set_url(this->url_.value(x...)); this->parent_->set_method(this->method_.value(x...)); if (this->body_.has_value()) { @@ -107,6 +106,7 @@ template class HttpRequestSendAction : public Action { this->parent_->close(); } + protected: void encode_json_(Ts... x, JsonObject &root) { for (const auto &item : this->json_) { auto val = item.second; diff --git a/esphome/components/integration/integration_sensor.h b/esphome/components/integration/integration_sensor.h index 85a89f6e4..2fcec069b 100644 --- a/esphome/components/integration/integration_sensor.h +++ b/esphome/components/integration/integration_sensor.h @@ -76,9 +76,9 @@ template class ResetAction : public Action { public: explicit ResetAction(IntegrationSensor *parent) : parent_(parent) {} - protected: - void play_(Ts... x) override { this->parent_->reset(); } + void play(Ts... x) override { this->parent_->reset(); } + protected: IntegrationSensor *parent_; }; diff --git a/esphome/components/ledc/ledc_output.h b/esphome/components/ledc/ledc_output.h index 7ff0987e0..3f56f502b 100644 --- a/esphome/components/ledc/ledc_output.h +++ b/esphome/components/ledc/ledc_output.h @@ -43,12 +43,12 @@ template class SetFrequencyAction : public Action { SetFrequencyAction(LEDCOutput *parent) : parent_(parent) {} TEMPLATABLE_VALUE(float, frequency); - protected: - void play_(Ts... x) { + void play(Ts... x) { float freq = this->frequency_.value(x...); this->parent_->apply_frequency(freq); } + protected: LEDCOutput *parent_; }; diff --git a/esphome/components/light/automation.h b/esphome/components/light/automation.h index 1c641562d..dfab78065 100644 --- a/esphome/components/light/automation.h +++ b/esphome/components/light/automation.h @@ -13,13 +13,13 @@ template class ToggleAction : public Action { TEMPLATABLE_VALUE(uint32_t, transition_length) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->state_->toggle(); call.set_transition_length(this->transition_length_.optional_value(x...)); call.perform(); } + protected: LightState *state_; }; @@ -38,8 +38,7 @@ template class LightControlAction : public Action { TEMPLATABLE_VALUE(float, color_temperature) TEMPLATABLE_VALUE(std::string, effect) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->parent_->make_call(); call.set_state(this->state_.optional_value(x...)); call.set_brightness(this->brightness_.optional_value(x...)); @@ -54,6 +53,7 @@ template class LightControlAction : public Action { call.perform(); } + protected: LightState *parent_; }; @@ -64,8 +64,7 @@ template class DimRelativeAction : public Action { TEMPLATABLE_VALUE(float, relative_brightness) TEMPLATABLE_VALUE(uint32_t, transition_length) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto call = this->parent_->make_call(); float rel = this->relative_brightness_.value(x...); float cur; @@ -78,6 +77,7 @@ template class DimRelativeAction : public Action { call.perform(); } + protected: LightState *parent_; }; @@ -143,8 +143,7 @@ template class AddressableSet : public Action { TEMPLATABLE_VALUE(uint8_t, blue) TEMPLATABLE_VALUE(uint8_t, white) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto *out = (AddressableLight *) this->parent_->get_output(); int32_t range_from = this->range_from_.value_or(x..., 0); int32_t range_to = this->range_to_.value_or(x..., out->size() - 1) + 1; @@ -160,6 +159,7 @@ template class AddressableSet : public Action { out->schedule_show(); } + protected: LightState *parent_; }; diff --git a/esphome/components/mhz19/mhz19.h b/esphome/components/mhz19/mhz19.h index bdb2c50d1..151351be4 100644 --- a/esphome/components/mhz19/mhz19.h +++ b/esphome/components/mhz19/mhz19.h @@ -38,9 +38,9 @@ template class MHZ19CalibrateZeroAction : public Action { public: MHZ19CalibrateZeroAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - protected: - void play_(Ts... x) override { this->mhz19_->calibrate_zero(); } + void play(Ts... x) override { this->mhz19_->calibrate_zero(); } + protected: MHZ19Component *mhz19_; }; @@ -48,9 +48,9 @@ template class MHZ19ABCEnableAction : public Action { public: MHZ19ABCEnableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - protected: - void play_(Ts... x) override { this->mhz19_->abc_enable(); } + void play(Ts... x) override { this->mhz19_->abc_enable(); } + protected: MHZ19Component *mhz19_; }; @@ -58,9 +58,9 @@ template class MHZ19ABCDisableAction : public Action { public: MHZ19ABCDisableAction(MHZ19Component *mhz19) : mhz19_(mhz19) {} - protected: - void play_(Ts... x) override { this->mhz19_->abc_disable(); } + void play(Ts... x) override { this->mhz19_->abc_disable(); } + protected: MHZ19Component *mhz19_; }; diff --git a/esphome/components/mqtt/mqtt_client.h b/esphome/components/mqtt/mqtt_client.h index fbd2435bf..2bbebff84 100644 --- a/esphome/components/mqtt/mqtt_client.h +++ b/esphome/components/mqtt/mqtt_client.h @@ -299,12 +299,12 @@ template class MQTTPublishAction : public Action { TEMPLATABLE_VALUE(uint8_t, qos) TEMPLATABLE_VALUE(bool, retain) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { this->parent_->publish(this->topic_.value(x...), this->payload_.value(x...), this->qos_.value(x...), this->retain_.value(x...)); } + protected: MQTTClientComponent *parent_; }; @@ -317,14 +317,15 @@ template class MQTTPublishJsonAction : public Action { void set_payload(std::function payload) { this->payload_ = payload; } - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto f = std::bind(&MQTTPublishJsonAction::encode_, this, x..., std::placeholders::_1); auto topic = this->topic_.value(x...); auto qos = this->qos_.value(x...); auto retain = this->retain_.value(x...); this->parent_->publish_json(topic, f, qos, retain); } + + protected: void encode_(Ts... x, JsonObject &root) { this->payload_(x..., root); } std::function payload_; MQTTClientComponent *parent_; diff --git a/esphome/components/output/automation.h b/esphome/components/output/automation.h index c65b0d3f6..51c284970 100644 --- a/esphome/components/output/automation.h +++ b/esphome/components/output/automation.h @@ -12,9 +12,9 @@ template class TurnOffAction : public Action { public: TurnOffAction(BinaryOutput *output) : output_(output) {} - protected: - void play_(Ts... x) override { this->output_->turn_off(); } + void play(Ts... x) override { this->output_->turn_off(); } + protected: BinaryOutput *output_; }; @@ -22,9 +22,9 @@ template class TurnOnAction : public Action { public: TurnOnAction(BinaryOutput *output) : output_(output) {} - protected: - void play_(Ts... x) override { this->output_->turn_on(); } + void play(Ts... x) override { this->output_->turn_on(); } + protected: BinaryOutput *output_; }; @@ -34,9 +34,9 @@ template class SetLevelAction : public Action { TEMPLATABLE_VALUE(float, level) - protected: - void play_(Ts... x) override { this->output_->set_level(this->level_.value(x...)); } + void play(Ts... x) override { this->output_->set_level(this->level_.value(x...)); } + protected: FloatOutput *output_; }; diff --git a/esphome/components/pid/pid_climate.h b/esphome/components/pid/pid_climate.h index 0c92cee79..3dae92af5 100644 --- a/esphome/components/pid/pid_climate.h +++ b/esphome/components/pid/pid_climate.h @@ -75,8 +75,7 @@ template class PIDAutotuneAction : public Action { void set_positive_output(float positive_output) { positive_output_ = positive_output; } void set_negative_output(float negative_output) { negative_output_ = negative_output; } - protected: - void play_(Ts... x) { + void play(Ts... x) { auto tuner = make_unique(); tuner->set_noiseband(this->noiseband_); tuner->set_output_negative(this->negative_output_); @@ -84,6 +83,7 @@ template class PIDAutotuneAction : public Action { this->parent_->start_autotune(std::move(tuner)); } + protected: float noiseband_; float positive_output_; float negative_output_; diff --git a/esphome/components/remote_base/jvc_protocol.h b/esphome/components/remote_base/jvc_protocol.h index af666bf13..fc40a6a87 100644 --- a/esphome/components/remote_base/jvc_protocol.h +++ b/esphome/components/remote_base/jvc_protocol.h @@ -24,8 +24,7 @@ template class JVCAction : public RemoteTransmitterActionBasedata_.value(x...); JVCProtocol().encode(dst, data); diff --git a/esphome/components/remote_base/lg_protocol.h b/esphome/components/remote_base/lg_protocol.h index a2e9489d0..626756044 100644 --- a/esphome/components/remote_base/lg_protocol.h +++ b/esphome/components/remote_base/lg_protocol.h @@ -27,8 +27,7 @@ template class LGAction : public RemoteTransmitterActionBasedata_.value(x...); data.nbits = this->nbits_.value(x...); diff --git a/esphome/components/remote_base/nec_protocol.h b/esphome/components/remote_base/nec_protocol.h index 8c1db632b..593a3efe1 100644 --- a/esphome/components/remote_base/nec_protocol.h +++ b/esphome/components/remote_base/nec_protocol.h @@ -26,8 +26,7 @@ template class NECAction : public RemoteTransmitterActionBaseaddress_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/panasonic_protocol.h b/esphome/components/remote_base/panasonic_protocol.h index 1a09b2ffd..eae97a8a1 100644 --- a/esphome/components/remote_base/panasonic_protocol.h +++ b/esphome/components/remote_base/panasonic_protocol.h @@ -27,8 +27,7 @@ template class PanasonicAction : public RemoteTransmitterActionB TEMPLATABLE_VALUE(uint16_t, address) TEMPLATABLE_VALUE(uint32_t, command) - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { PanasonicData data{}; data.address = this->address_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/pioneer_protocol.h b/esphome/components/remote_base/pioneer_protocol.h index 876176223..4cac4f9f3 100644 --- a/esphome/components/remote_base/pioneer_protocol.h +++ b/esphome/components/remote_base/pioneer_protocol.h @@ -26,8 +26,7 @@ template class PioneerAction : public RemoteTransmitterActionBas TEMPLATABLE_VALUE(uint16_t, rc_code_1) TEMPLATABLE_VALUE(uint16_t, rc_code_2) - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { PioneerData data{}; data.rc_code_1 = this->rc_code_1_.value(x...); data.rc_code_2 = this->rc_code_2_.value(x...); diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 39da5fa8f..1d9f1c5ac 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -46,8 +46,7 @@ template class RawAction : public RemoteTransmitterActionBasecode_static_ != nullptr) { for (size_t i = 0; i < this->code_static_len_; i++) { auto val = this->code_static_[i]; @@ -62,6 +61,7 @@ template class RawAction : public RemoteTransmitterActionBaseset_carrier_frequency(this->carrier_frequency_.value(x...)); } + protected: std::function(Ts...)> code_func_{}; const int32_t *code_static_{nullptr}; int32_t code_static_len_{0}; diff --git a/esphome/components/remote_base/rc5_protocol.h b/esphome/components/remote_base/rc5_protocol.h index f90284c23..589c8d42d 100644 --- a/esphome/components/remote_base/rc5_protocol.h +++ b/esphome/components/remote_base/rc5_protocol.h @@ -27,8 +27,7 @@ template class RC5Action : public RemoteTransmitterActionBaseaddress_.value(x...); data.command = this->command_.value(x...); diff --git a/esphome/components/remote_base/rc_switch_protocol.h b/esphome/components/remote_base/rc_switch_protocol.h index 480f49ea9..8362899ce 100644 --- a/esphome/components/remote_base/rc_switch_protocol.h +++ b/esphome/components/remote_base/rc_switch_protocol.h @@ -71,8 +71,7 @@ template class RCSwitchRawAction : public RemoteTransmitterActio TEMPLATABLE_VALUE(RCSwitchBase, protocol); TEMPLATABLE_VALUE(std::string, code); - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { auto code = this->code_.value(x...); uint64_t the_code = decode_binary_string(code); uint8_t nbits = code.size(); @@ -89,8 +88,7 @@ template class RCSwitchTypeAAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(std::string, device); TEMPLATABLE_VALUE(bool, state); - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { auto group = this->group_.value(x...); auto device = this->device_.value(x...); auto state = this->state_.value(x...); @@ -113,8 +111,7 @@ template class RCSwitchTypeBAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, channel); TEMPLATABLE_VALUE(bool, state); - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { auto address = this->address_.value(x...); auto channel = this->channel_.value(x...); auto state = this->state_.value(x...); @@ -136,8 +133,7 @@ template class RCSwitchTypeCAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, device); TEMPLATABLE_VALUE(bool, state); - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { auto family = this->family_.value(x...); auto group = this->group_.value(x...); auto device = this->device_.value(x...); @@ -160,8 +156,7 @@ template class RCSwitchTypeDAction : public RemoteTransmitterAct TEMPLATABLE_VALUE(uint8_t, device); TEMPLATABLE_VALUE(bool, state); - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { auto group = this->group_.value(x...); auto device = this->device_.value(x...); auto state = this->state_.value(x...); diff --git a/esphome/components/remote_base/remote_base.h b/esphome/components/remote_base/remote_base.h index c9cae96c7..916fe29c1 100644 --- a/esphome/components/remote_base/remote_base.h +++ b/esphome/components/remote_base/remote_base.h @@ -326,17 +326,17 @@ template class RemoteTransmitterActionBase : public Actionparent_->transmit(); - this->encode_(call.get_data(), x...); + this->encode(call.get_data(), x...); call.set_send_times(this->send_times_.value_or(x..., 1)); call.set_send_wait(this->send_wait_.value_or(x..., 0)); call.perform(); } + protected: + virtual void encode(RemoteTransmitData *dst, Ts... x) = 0; + RemoteTransmitterBase *parent_{}; }; diff --git a/esphome/components/remote_base/samsung_protocol.h b/esphome/components/remote_base/samsung_protocol.h index 0e1666b3b..f7a54788e 100644 --- a/esphome/components/remote_base/samsung_protocol.h +++ b/esphome/components/remote_base/samsung_protocol.h @@ -25,8 +25,7 @@ template class SamsungAction : public RemoteTransmitterActionBas public: TEMPLATABLE_VALUE(uint32_t, data) - protected: - void encode_(RemoteTransmitData *dst, Ts... x) override { + void encode(RemoteTransmitData *dst, Ts... x) override { SamsungData data{}; data.data = this->data_.value(x...); SamsungProtocol().encode(dst, data); diff --git a/esphome/components/remote_base/sony_protocol.h b/esphome/components/remote_base/sony_protocol.h index a122d3758..aecc8ab91 100644 --- a/esphome/components/remote_base/sony_protocol.h +++ b/esphome/components/remote_base/sony_protocol.h @@ -27,8 +27,7 @@ template class SonyAction : public RemoteTransmitterActionBasedata_.value(x...); data.nbits = this->nbits_.value(x...); diff --git a/esphome/components/rf_bridge/rf_bridge.h b/esphome/components/rf_bridge/rf_bridge.h index c747be997..86713b8a5 100644 --- a/esphome/components/rf_bridge/rf_bridge.h +++ b/esphome/components/rf_bridge/rf_bridge.h @@ -68,8 +68,7 @@ template class RFBridgeSendCodeAction : public Action { TEMPLATABLE_VALUE(uint16_t, high) TEMPLATABLE_VALUE(uint32_t, code) - protected: - void play_(Ts... x) { + void play(Ts... x) { RFBridgeData data{}; data.sync = this->sync_.value(x...); data.low = this->low_.value(x...); @@ -78,6 +77,7 @@ template class RFBridgeSendCodeAction : public Action { this->parent_->send_code(data); } + protected: RFBridgeComponent *parent_; }; @@ -85,9 +85,9 @@ template class RFBridgeLearnAction : public Action { public: RFBridgeLearnAction(RFBridgeComponent *parent) : parent_(parent) {} - protected: - void play_(Ts... x) { this->parent_->learn(); } + void play(Ts... x) { this->parent_->learn(); } + protected: RFBridgeComponent *parent_; }; diff --git a/esphome/components/rotary_encoder/rotary_encoder.h b/esphome/components/rotary_encoder/rotary_encoder.h index 0bbcf21dd..f0e47dfe0 100644 --- a/esphome/components/rotary_encoder/rotary_encoder.h +++ b/esphome/components/rotary_encoder/rotary_encoder.h @@ -75,8 +75,9 @@ template class RotaryEncoderSetValueAction : public Actionencoder_->set_value(this->value_.value(x...)); } + protected: - void play_(Ts... x) override { this->encoder_->set_value(this->value_.value(x...)); } RotaryEncoderSensor *encoder_; }; diff --git a/esphome/components/script/script.h b/esphome/components/script/script.h index 92eba15e4..8495014f0 100644 --- a/esphome/components/script/script.h +++ b/esphome/components/script/script.h @@ -23,9 +23,9 @@ template class ScriptExecuteAction : public Action { public: ScriptExecuteAction(Script *script) : script_(script) {} - protected: - void play_(Ts... x) override { this->script_->trigger(); } + void play(Ts... x) override { this->script_->trigger(); } + protected: Script *script_; }; @@ -33,9 +33,9 @@ template class ScriptStopAction : public Action { public: ScriptStopAction(Script *script) : script_(script) {} - protected: - void play_(Ts... x) override { this->script_->stop(); } + void play(Ts... x) override { this->script_->stop(); } + protected: Script *script_; }; @@ -76,10 +76,10 @@ template class ScriptWaitAction : public Action, public C float get_setup_priority() const override { return setup_priority::DATA; } - protected: - void play_(Ts... x) override { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } + protected: Script *script_; std::tuple var_{}; }; diff --git a/esphome/components/sensor/automation.h b/esphome/components/sensor/automation.h index e6ddc3b95..c70fb9396 100644 --- a/esphome/components/sensor/automation.h +++ b/esphome/components/sensor/automation.h @@ -26,8 +26,9 @@ template class SensorPublishAction : public Action { SensorPublishAction(Sensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(float, state) + void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } + protected: - void play_(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } Sensor *sensor_; }; diff --git a/esphome/components/servo/servo.h b/esphome/components/servo/servo.h index b4076e8cd..b864efc87 100644 --- a/esphome/components/servo/servo.h +++ b/esphome/components/servo/servo.h @@ -65,8 +65,9 @@ template class ServoWriteAction : public Action { ServoWriteAction(Servo *servo) : servo_(servo) {} TEMPLATABLE_VALUE(float, value) + void play(Ts... x) override { this->servo_->write(this->value_.value(x...)); } + protected: - void play_(Ts... x) override { this->servo_->write(this->value_.value(x...)); } Servo *servo_; }; @@ -74,8 +75,9 @@ template class ServoDetachAction : public Action { public: ServoDetachAction(Servo *servo) : servo_(servo) {} + void play(Ts... x) override { this->servo_->detach(); } + protected: - void play_(Ts... x) override { this->servo_->detach(); } Servo *servo_; }; diff --git a/esphome/components/sim800l/sim800l.h b/esphome/components/sim800l/sim800l.h index afdb50468..696eb8890 100644 --- a/esphome/components/sim800l/sim800l.h +++ b/esphome/components/sim800l/sim800l.h @@ -78,13 +78,13 @@ template class Sim800LSendSmsAction : public Action { TEMPLATABLE_VALUE(std::string, recipient) TEMPLATABLE_VALUE(std::string, message) - protected: - void play_(Ts... x) { + void play(Ts... x) { auto recipient = this->recipient_.value(x...); auto message = this->message_.value(x...); this->parent_->send_sms(recipient, message); } + protected: Sim800LComponent *parent_; }; diff --git a/esphome/components/stepper/stepper.h b/esphome/components/stepper/stepper.h index 31bdf59bf..33777dce8 100644 --- a/esphome/components/stepper/stepper.h +++ b/esphome/components/stepper/stepper.h @@ -43,9 +43,9 @@ template class SetTargetAction : public Action { TEMPLATABLE_VALUE(int32_t, target) - protected: - void play_(Ts... x) override { this->parent_->set_target(this->target_.value(x...)); } + void play(Ts... x) override { this->parent_->set_target(this->target_.value(x...)); } + protected: Stepper *parent_; }; @@ -55,9 +55,9 @@ template class ReportPositionAction : public Action { TEMPLATABLE_VALUE(int32_t, position) - protected: - void play_(Ts... x) override { this->parent_->report_position(this->position_.value(x...)); } + void play(Ts... x) override { this->parent_->report_position(this->position_.value(x...)); } + protected: Stepper *parent_; }; @@ -67,13 +67,13 @@ template class SetSpeedAction : public Action { TEMPLATABLE_VALUE(float, speed); - protected: - void play_(Ts... x) override { + void play(Ts... x) override { float speed = this->speed_.value(x...); this->parent_->set_max_speed(speed); this->parent_->on_update_speed(); } + protected: Stepper *parent_; }; diff --git a/esphome/components/switch/automation.h b/esphome/components/switch/automation.h index c4824b106..579daf4d2 100644 --- a/esphome/components/switch/automation.h +++ b/esphome/components/switch/automation.h @@ -11,9 +11,9 @@ template class TurnOnAction : public Action { public: explicit TurnOnAction(Switch *a_switch) : switch_(a_switch) {} - protected: - void play_(Ts... x) override { this->switch_->turn_on(); } + void play(Ts... x) override { this->switch_->turn_on(); } + protected: Switch *switch_; }; @@ -21,9 +21,9 @@ template class TurnOffAction : public Action { public: explicit TurnOffAction(Switch *a_switch) : switch_(a_switch) {} - protected: - void play_(Ts... x) override { this->switch_->turn_off(); } + void play(Ts... x) override { this->switch_->turn_off(); } + protected: Switch *switch_; }; @@ -31,9 +31,9 @@ template class ToggleAction : public Action { public: explicit ToggleAction(Switch *a_switch) : switch_(a_switch) {} - protected: - void play_(Ts... x) override { this->switch_->toggle(); } + void play(Ts... x) override { this->switch_->toggle(); } + protected: Switch *switch_; }; @@ -74,8 +74,9 @@ template class SwitchPublishAction : public Action { SwitchPublishAction(Switch *a_switch) : switch_(a_switch) {} TEMPLATABLE_VALUE(bool, state) + void play(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); } + protected: - void play_(Ts... x) override { this->switch_->publish_state(this->state_.value(x...)); } Switch *switch_; }; diff --git a/esphome/components/text_sensor/automation.h b/esphome/components/text_sensor/automation.h index 9fac17c4c..6810d10b1 100644 --- a/esphome/components/text_sensor/automation.h +++ b/esphome/components/text_sensor/automation.h @@ -31,8 +31,9 @@ template class TextSensorPublishAction : public Action { TextSensorPublishAction(TextSensor *sensor) : sensor_(sensor) {} TEMPLATABLE_VALUE(std::string, state) + void play(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } + protected: - void play_(Ts... x) override { this->sensor_->publish_state(this->state_.value(x...)); } TextSensor *sensor_; }; diff --git a/esphome/components/tm1651/tm1651.h b/esphome/components/tm1651/tm1651.h index 6291cf1ec..6eab24687 100644 --- a/esphome/components/tm1651/tm1651.h +++ b/esphome/components/tm1651/tm1651.h @@ -43,8 +43,7 @@ template class SetLevelPercentAction : public Action, pub public: TEMPLATABLE_VALUE(uint8_t, level_percent) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto level_percent = this->level_percent_.value(x...); this->parent_->set_level_percent(level_percent); } @@ -54,8 +53,7 @@ template class SetLevelAction : public Action, public Par public: TEMPLATABLE_VALUE(uint8_t, level) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto level = this->level_.value(x...); this->parent_->set_level(level); } @@ -65,21 +63,20 @@ template class SetBrightnessAction : public Action, publi public: TEMPLATABLE_VALUE(uint8_t, brightness) - protected: - void play_(Ts... x) override { + void play(Ts... x) override { auto brightness = this->brightness_.value(x...); this->parent_->set_brightness(brightness); } }; template class TurnOnAction : public Action, public Parented { - protected: - void play_(Ts... x) override { this->parent_->turn_on(); } + public: + void play(Ts... x) override { this->parent_->turn_on(); } }; template class TurnOffAction : public Action, public Parented { - protected: - void play_(Ts... x) override { this->parent_->turn_off(); } + public: + void play(Ts... x) override { this->parent_->turn_off(); } }; } // namespace tm1651 diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 6889bc12e..2212e5844 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -17,8 +17,7 @@ template class UARTWriteAction : public Action, public Pa this->static_ = true; } - protected: - void play_(Ts... x) override { + void play(Ts... x) override { if (this->static_) { this->parent_->write_array(this->data_static_); } else { @@ -26,6 +25,7 @@ template class UARTWriteAction : public Action, public Pa this->parent_->write_array(val); } } + protected: bool static_{false}; std::function(Ts...)> data_func_{}; std::vector data_static_{}; diff --git a/esphome/core/automation.h b/esphome/core/automation.h index cb9536467..02bd8bb29 100644 --- a/esphome/core/automation.h +++ b/esphome/core/automation.h @@ -77,12 +77,12 @@ template class Action { public: virtual void play_complex(Ts... x) { this->num_running_++; - this->play_(x...); + this->play(x...); this->play_next_(x...); } virtual void stop_complex() { if (num_running_) { - this->stop_(); + this->stop(); this->num_running_ = 0; } this->stop_next_(); @@ -92,7 +92,7 @@ template class Action { protected: friend ActionList; - virtual void play_(Ts... x) = 0; + virtual void play(Ts... x) = 0; void play_next_(Ts... x) { if (this->num_running_ > 0) { this->num_running_--; @@ -108,7 +108,7 @@ template class Action { this->play_next_tuple_(tuple, typename gens::type()); } - virtual void stop_() {} + virtual void stop() {} void stop_next_() { if (this->next_ != nullptr) { this->next_->stop_complex(); diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h index 4edd45942..d2656290b 100644 --- a/esphome/core/base_automation.h +++ b/esphome/core/base_automation.h @@ -115,22 +115,19 @@ template class DelayAction : public Action, public Compon } float get_setup_priority() const override { return setup_priority::HARDWARE; } - protected: - void play_(Ts... x) override { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } - void stop_() override { - this->cancel_timeout(""); - } + void stop() override { this->cancel_timeout(""); } }; template class LambdaAction : public Action { public: explicit LambdaAction(std::function &&f) : f_(std::move(f)) {} - protected: - void play_(Ts... x) override { this->f_(x...); } + void play(Ts... x) override { this->f_(x...); } + protected: std::function f_; }; @@ -166,15 +163,15 @@ template class IfAction : public Action { } } - protected: - void play_(Ts... x) override { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } - void stop_() override { + void stop() override { this->then_.stop(); this->else_.stop(); } + protected: Condition *condition_; ActionList then_; ActionList else_; @@ -216,12 +213,12 @@ template class WhileAction : public Action { } } - protected: - void play_(Ts... x) override { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } - void stop_() override { this->then_.stop(); } + void stop() override { this->then_.stop(); } + protected: Condition *condition_; ActionList then_; std::tuple var_{}; @@ -257,10 +254,10 @@ template class WaitUntilAction : public Action, public Co float get_setup_priority() const override { return setup_priority::DATA; } - protected: - void play_(Ts... x) override { /* ignore - see play_complex */ + void play(Ts... x) override { /* ignore - see play_complex */ } + protected: Condition *condition_; std::tuple var_{}; }; @@ -269,9 +266,9 @@ template class UpdateComponentAction : public Action { public: UpdateComponentAction(PollingComponent *component) : component_(component) {} - protected: - void play_(Ts... x) override { this->component_->update(); } + void play(Ts... x) override { this->component_->update(); } + protected: PollingComponent *component_; }; From 9e7e8ab116b47e784dd480dd870c4d23ad891ee9 Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Sun, 24 May 2020 23:45:15 -0300 Subject: [PATCH 4/4] format --- esphome/components/uart/automation.h | 1 + 1 file changed, 1 insertion(+) diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 2212e5844..9686f9441 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -25,6 +25,7 @@ template class UARTWriteAction : public Action, public Pa this->parent_->write_array(val); } } + protected: bool static_{false}; std::function(Ts...)> data_func_{};