mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 08:55:22 +01:00
duty_time: fix build without binary_sensor. Parented in automations. (#5156)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
fdb20e4a30
commit
9ff0471274
3 changed files with 32 additions and 38 deletions
|
@ -6,9 +6,11 @@ namespace duty_time_sensor {
|
||||||
|
|
||||||
static const char *const TAG = "duty_time_sensor";
|
static const char *const TAG = "duty_time_sensor";
|
||||||
|
|
||||||
|
#ifdef USE_BINARY_SENSOR
|
||||||
void DutyTimeSensor::set_sensor(binary_sensor::BinarySensor *const sensor) {
|
void DutyTimeSensor::set_sensor(binary_sensor::BinarySensor *const sensor) {
|
||||||
sensor->add_on_state_callback([this](bool state) { this->process_state_(state); });
|
sensor->add_on_state_callback([this](bool state) { this->process_state_(state); });
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void DutyTimeSensor::start() {
|
void DutyTimeSensor::start() {
|
||||||
if (!this->last_state_)
|
if (!this->last_state_)
|
||||||
|
|
|
@ -3,8 +3,10 @@
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/preferences.h"
|
#include "esphome/core/preferences.h"
|
||||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
|
||||||
#include "esphome/components/sensor/sensor.h"
|
#include "esphome/components/sensor/sensor.h"
|
||||||
|
#ifdef USE_BINARY_SENSOR
|
||||||
|
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace duty_time_sensor {
|
namespace duty_time_sensor {
|
||||||
|
@ -22,8 +24,10 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent {
|
||||||
bool is_running() const { return this->last_state_; }
|
bool is_running() const { return this->last_state_; }
|
||||||
void reset() { this->set_value_(0); }
|
void reset() { this->set_value_(0); }
|
||||||
|
|
||||||
void set_lambda(std::function<bool()> &&func) { this->func_ = func; }
|
#ifdef USE_BINARY_SENSOR
|
||||||
void set_sensor(binary_sensor::BinarySensor *sensor);
|
void set_sensor(binary_sensor::BinarySensor *sensor);
|
||||||
|
#endif
|
||||||
|
void set_lambda(std::function<bool()> &&func) { this->func_ = func; }
|
||||||
void set_last_duty_time_sensor(sensor::Sensor *sensor) { this->last_duty_time_sensor_ = sensor; }
|
void set_last_duty_time_sensor(sensor::Sensor *sensor) { this->last_duty_time_sensor_ = sensor; }
|
||||||
void set_restore(bool restore) { this->restore_ = restore; }
|
void set_restore(bool restore) { this->restore_ = restore; }
|
||||||
|
|
||||||
|
@ -43,44 +47,26 @@ class DutyTimeSensor : public sensor::Sensor, public PollingComponent {
|
||||||
bool restore_;
|
bool restore_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class StartAction : public Action<Ts...> {
|
template<typename... Ts> class BaseAction : public Action<Ts...>, public Parented<DutyTimeSensor> {};
|
||||||
public:
|
|
||||||
explicit StartAction(DutyTimeSensor *parent) : parent_(parent) {}
|
|
||||||
|
|
||||||
|
template<typename... Ts> class StartAction : public BaseAction<Ts...> {
|
||||||
void play(Ts... x) override { this->parent_->start(); }
|
void play(Ts... x) override { this->parent_->start(); }
|
||||||
|
|
||||||
protected:
|
|
||||||
DutyTimeSensor *parent_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class StopAction : public Action<Ts...> {
|
template<typename... Ts> class StopAction : public BaseAction<Ts...> {
|
||||||
public:
|
|
||||||
explicit StopAction(DutyTimeSensor *parent) : parent_(parent) {}
|
|
||||||
|
|
||||||
void play(Ts... x) override { this->parent_->stop(); }
|
void play(Ts... x) override { this->parent_->stop(); }
|
||||||
|
|
||||||
protected:
|
|
||||||
DutyTimeSensor *parent_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class ResetAction : public Action<Ts...> {
|
template<typename... Ts> class ResetAction : public BaseAction<Ts...> {
|
||||||
public:
|
|
||||||
explicit ResetAction(DutyTimeSensor *parent) : parent_(parent) {}
|
|
||||||
|
|
||||||
void play(Ts... x) override { this->parent_->reset(); }
|
void play(Ts... x) override { this->parent_->reset(); }
|
||||||
|
|
||||||
protected:
|
|
||||||
DutyTimeSensor *parent_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class RunningCondition : public Condition<Ts...> {
|
template<typename... Ts> class RunningCondition : public Condition<Ts...>, public Parented<DutyTimeSensor> {
|
||||||
public:
|
public:
|
||||||
explicit RunningCondition(DutyTimeSensor *parent, bool state) : parent_(parent), state_(state) {}
|
explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {}
|
||||||
|
|
||||||
bool check(Ts... x) override { return this->parent_->is_running() == this->state_; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
DutyTimeSensor *parent_;
|
bool check(Ts... x) override { return this->parent_->is_running() == this->state_; }
|
||||||
bool state_;
|
bool state_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,11 +26,14 @@ duty_time_sensor_ns = cg.esphome_ns.namespace("duty_time_sensor")
|
||||||
DutyTimeSensor = duty_time_sensor_ns.class_(
|
DutyTimeSensor = duty_time_sensor_ns.class_(
|
||||||
"DutyTimeSensor", sensor.Sensor, cg.PollingComponent
|
"DutyTimeSensor", sensor.Sensor, cg.PollingComponent
|
||||||
)
|
)
|
||||||
StartAction = duty_time_sensor_ns.class_("StartAction", Action)
|
BaseAction = duty_time_sensor_ns.class_("BaseAction", Action, cg.Parented)
|
||||||
StopAction = duty_time_sensor_ns.class_("StopAction", Action)
|
StartAction = duty_time_sensor_ns.class_("StartAction", BaseAction)
|
||||||
ResetAction = duty_time_sensor_ns.class_("ResetAction", Action)
|
StopAction = duty_time_sensor_ns.class_("StopAction", BaseAction)
|
||||||
SetAction = duty_time_sensor_ns.class_("SetAction", Action)
|
ResetAction = duty_time_sensor_ns.class_("ResetAction", BaseAction)
|
||||||
RunningCondition = duty_time_sensor_ns.class_("RunningCondition", Condition)
|
SetAction = duty_time_sensor_ns.class_("SetAction", BaseAction)
|
||||||
|
RunningCondition = duty_time_sensor_ns.class_(
|
||||||
|
"RunningCondition", Condition, cg.Parented
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
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)
|
@register_action("sensor.duty_time.start", StartAction, DUTY_TIME_ID_SCHEMA)
|
||||||
async def sensor_runtime_start_to_code(config, action_id, template_arg, args):
|
async def sensor_runtime_start_to_code(config, action_id, template_arg, args):
|
||||||
paren = await cg.get_variable(config[CONF_ID])
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
await cg.register_parented(var, config[CONF_ID])
|
||||||
|
return var
|
||||||
|
|
||||||
|
|
||||||
@register_action("sensor.duty_time.stop", StopAction, DUTY_TIME_ID_SCHEMA)
|
@register_action("sensor.duty_time.stop", StopAction, DUTY_TIME_ID_SCHEMA)
|
||||||
async def sensor_runtime_stop_to_code(config, action_id, template_arg, args):
|
async def sensor_runtime_stop_to_code(config, action_id, template_arg, args):
|
||||||
paren = await cg.get_variable(config[CONF_ID])
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
await cg.register_parented(var, config[CONF_ID])
|
||||||
|
return var
|
||||||
|
|
||||||
|
|
||||||
@register_action("sensor.duty_time.reset", ResetAction, DUTY_TIME_ID_SCHEMA)
|
@register_action("sensor.duty_time.reset", ResetAction, DUTY_TIME_ID_SCHEMA)
|
||||||
async def sensor_runtime_reset_to_code(config, action_id, template_arg, args):
|
async def sensor_runtime_reset_to_code(config, action_id, template_arg, args):
|
||||||
paren = await cg.get_variable(config[CONF_ID])
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
await cg.register_parented(var, config[CONF_ID])
|
||||||
|
return var
|
||||||
|
|
||||||
|
|
||||||
@register_condition(
|
@register_condition(
|
||||||
|
|
Loading…
Reference in a new issue