From 9ff0471274404bb5dfa549279dd90224c7e13641 Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Mon, 31 Jul 2023 01:30:11 +0400 Subject: [PATCH] duty_time: fix build without binary_sensor. Parented in automations. (#5156) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- .../components/duty_time/duty_time_sensor.cpp | 2 + .../components/duty_time/duty_time_sensor.h | 40 ++++++------------- esphome/components/duty_time/sensor.py | 28 ++++++++----- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/esphome/components/duty_time/duty_time_sensor.cpp b/esphome/components/duty_time/duty_time_sensor.cpp index 045cbcceac..1101c4d41e 100644 --- a/esphome/components/duty_time/duty_time_sensor.cpp +++ b/esphome/components/duty_time/duty_time_sensor.cpp @@ -6,9 +6,11 @@ namespace duty_time_sensor { static const char *const TAG = "duty_time_sensor"; +#ifdef USE_BINARY_SENSOR void DutyTimeSensor::set_sensor(binary_sensor::BinarySensor *const sensor) { sensor->add_on_state_callback([this](bool state) { this->process_state_(state); }); } +#endif void DutyTimeSensor::start() { if (!this->last_state_) diff --git a/esphome/components/duty_time/duty_time_sensor.h b/esphome/components/duty_time/duty_time_sensor.h index 27fa383847..1ec2f7b94f 100644 --- a/esphome/components/duty_time/duty_time_sensor.h +++ b/esphome/components/duty_time/duty_time_sensor.h @@ -3,8 +3,10 @@ #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/preferences.h" -#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/sensor/sensor.h" +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif namespace esphome { namespace duty_time_sensor { @@ -22,8 +24,10 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent { bool is_running() const { return this->last_state_; } void reset() { this->set_value_(0); } - void set_lambda(std::function &&func) { this->func_ = func; } +#ifdef USE_BINARY_SENSOR void set_sensor(binary_sensor::BinarySensor *sensor); +#endif + void set_lambda(std::function &&func) { this->func_ = func; } void set_last_duty_time_sensor(sensor::Sensor *sensor) { this->last_duty_time_sensor_ = sensor; } void set_restore(bool restore) { this->restore_ = restore; } @@ -43,44 +47,26 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent { bool restore_; }; -template class StartAction : public Action { - public: - explicit StartAction(DutyTimeSensor *parent) : parent_(parent) {} +template class BaseAction : public Action, public Parented {}; +template class StartAction : public BaseAction { void play(Ts... x) override { this->parent_->start(); } - - protected: - DutyTimeSensor *parent_; }; -template class StopAction : public Action { - public: - explicit StopAction(DutyTimeSensor *parent) : parent_(parent) {} - +template class StopAction : public BaseAction { void play(Ts... x) override { this->parent_->stop(); } - - protected: - DutyTimeSensor *parent_; }; -template class ResetAction : public Action { - public: - explicit ResetAction(DutyTimeSensor *parent) : parent_(parent) {} - +template class ResetAction : public BaseAction { void play(Ts... x) override { this->parent_->reset(); } - - protected: - DutyTimeSensor *parent_; }; -template class RunningCondition : public Condition { +template class RunningCondition : public Condition, public Parented { public: - explicit RunningCondition(DutyTimeSensor *parent, bool state) : parent_(parent), state_(state) {} - - bool check(Ts... x) override { return this->parent_->is_running() == this->state_; } + explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {} protected: - DutyTimeSensor *parent_; + bool check(Ts... x) override { return this->parent_->is_running() == this->state_; } bool state_; }; diff --git a/esphome/components/duty_time/sensor.py b/esphome/components/duty_time/sensor.py index 5f8582d481..556cd459a5 100644 --- a/esphome/components/duty_time/sensor.py +++ b/esphome/components/duty_time/sensor.py @@ -26,11 +26,14 @@ duty_time_sensor_ns = cg.esphome_ns.namespace("duty_time_sensor") DutyTimeSensor = duty_time_sensor_ns.class_( "DutyTimeSensor", sensor.Sensor, cg.PollingComponent ) -StartAction = duty_time_sensor_ns.class_("StartAction", Action) -StopAction = duty_time_sensor_ns.class_("StopAction", Action) -ResetAction = duty_time_sensor_ns.class_("ResetAction", Action) -SetAction = duty_time_sensor_ns.class_("SetAction", Action) -RunningCondition = duty_time_sensor_ns.class_("RunningCondition", Condition) +BaseAction = duty_time_sensor_ns.class_("BaseAction", Action, cg.Parented) +StartAction = duty_time_sensor_ns.class_("StartAction", BaseAction) +StopAction = duty_time_sensor_ns.class_("StopAction", BaseAction) +ResetAction = duty_time_sensor_ns.class_("ResetAction", BaseAction) +SetAction = duty_time_sensor_ns.class_("SetAction", BaseAction) +RunningCondition = duty_time_sensor_ns.class_( + "RunningCondition", Condition, cg.Parented +) CONFIG_SCHEMA = cv.All( @@ -89,20 +92,23 @@ DUTY_TIME_ID_SCHEMA = maybe_simple_id( @register_action("sensor.duty_time.start", StartAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_start_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_action("sensor.duty_time.stop", StopAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_stop_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_action("sensor.duty_time.reset", ResetAction, DUTY_TIME_ID_SCHEMA) async def sensor_runtime_reset_to_code(config, action_id, template_arg, args): - paren = await cg.get_variable(config[CONF_ID]) - return cg.new_Pvariable(action_id, template_arg, paren) + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var @register_condition(