mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[duty_time] Use the apply helpers for the start, stop, reset and is_running automations (#19634)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <cinttypes>
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
@@ -47,27 +46,4 @@ class DutyTimeSensor final : public sensor::Sensor, public PollingComponent {
|
||||
bool restore_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class BaseAction : public Action<Ts...>, public Parented<DutyTimeSensor> {};
|
||||
|
||||
template<typename... Ts> class StartAction : public BaseAction<Ts...> {
|
||||
void play(const Ts &...x) override { this->parent_->start(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class StopAction : public BaseAction<Ts...> {
|
||||
void play(const Ts &...x) override { this->parent_->stop(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class ResetAction : public BaseAction<Ts...> {
|
||||
void play(const Ts &...x) override { this->parent_->reset(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class RunningCondition final : public Condition<Ts...>, public Parented<DutyTimeSensor> {
|
||||
public:
|
||||
explicit RunningCondition(DutyTimeSensor *parent, bool state) : Parented(parent), state_(state) {}
|
||||
|
||||
protected:
|
||||
bool check(const Ts &...x) override { return this->parent_->is_running() == this->state_; }
|
||||
bool state_;
|
||||
};
|
||||
|
||||
} // namespace esphome::duty_time_sensor
|
||||
|
||||
@@ -13,8 +13,6 @@ from esphome.const import (
|
||||
STATE_CLASS_TOTAL_INCREASING,
|
||||
UNIT_SECOND,
|
||||
)
|
||||
from esphome.core import ID
|
||||
from esphome.cpp_generator import MockObj, TemplateArgsType
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CONF_LAST_TIME = "last_time"
|
||||
@@ -23,14 +21,6 @@ duty_time_sensor_ns = cg.esphome_ns.namespace("duty_time_sensor")
|
||||
DutyTimeSensor = duty_time_sensor_ns.class_(
|
||||
"DutyTimeSensor", sensor.Sensor, cg.PollingComponent
|
||||
)
|
||||
BaseAction = duty_time_sensor_ns.class_("BaseAction", automation.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", automation.Condition, cg.Parented
|
||||
)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
@@ -87,51 +77,18 @@ DUTY_TIME_ID_SCHEMA = automation.maybe_simple_id(
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"sensor.duty_time.start",
|
||||
StartAction,
|
||||
DUTY_TIME_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
for _name, _call in (
|
||||
("sensor.duty_time.start", "start()"),
|
||||
("sensor.duty_time.stop", "stop()"),
|
||||
("sensor.duty_time.reset", "reset()"),
|
||||
):
|
||||
automation.register_apply_action(
|
||||
_name, DUTY_TIME_ID_SCHEMA, automation.ApplyCall(_call)
|
||||
)
|
||||
|
||||
automation.register_apply_condition(
|
||||
"sensor.duty_time.is_running", DUTY_TIME_ID_SCHEMA, "is_running()"
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"sensor.duty_time.stop",
|
||||
StopAction,
|
||||
DUTY_TIME_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
automation.register_apply_condition(
|
||||
"sensor.duty_time.is_not_running", DUTY_TIME_ID_SCHEMA, "is_running() == false"
|
||||
)
|
||||
|
||||
|
||||
automation.register_parented_action(
|
||||
"sensor.duty_time.reset",
|
||||
ResetAction,
|
||||
DUTY_TIME_ID_SCHEMA,
|
||||
synchronous=True,
|
||||
)
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"sensor.duty_time.is_running", RunningCondition, DUTY_TIME_ID_SCHEMA
|
||||
)
|
||||
async def duty_time_is_running_to_code(
|
||||
config: ConfigType,
|
||||
condition_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren, True)
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"sensor.duty_time.is_not_running", RunningCondition, DUTY_TIME_ID_SCHEMA
|
||||
)
|
||||
async def duty_time_is_not_running_to_code(
|
||||
config: ConfigType,
|
||||
condition_id: ID,
|
||||
template_arg: cg.TemplateArguments,
|
||||
args: TemplateArgsType,
|
||||
) -> MockObj:
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren, False)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
button:
|
||||
- platform: template
|
||||
name: Duty Time Actions
|
||||
on_press:
|
||||
- if:
|
||||
condition:
|
||||
sensor.duty_time.is_running: pump_duty_time
|
||||
then:
|
||||
- sensor.duty_time.stop: pump_duty_time
|
||||
- if:
|
||||
condition:
|
||||
sensor.duty_time.is_not_running: pump_duty_time
|
||||
then:
|
||||
- sensor.duty_time.start: pump_duty_time
|
||||
- sensor.duty_time.reset: pump_duty_time
|
||||
@@ -9,5 +9,6 @@ binary_sensor:
|
||||
|
||||
sensor:
|
||||
- platform: duty_time
|
||||
id: pump_duty_time
|
||||
name: Duty Time
|
||||
sensor: duty_time_bin1
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
duty_time: !include common.yaml
|
||||
actions: !include common-actions.yaml
|
||||
Reference in New Issue
Block a user