Add new time.has_time condition (#1255)

This commit is contained in:
Ash McKenzie 2020-10-05 02:22:28 +11:00 committed by GitHub
parent 002861f13b
commit 5c86f332b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View file

@ -10,10 +10,11 @@ import tzlocal
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome import automation from esphome import automation
from esphome.const import CONF_CRON, CONF_DAYS_OF_MONTH, CONF_DAYS_OF_WEEK, CONF_HOURS, \ from esphome.const import CONF_ID, CONF_CRON, CONF_DAYS_OF_MONTH, CONF_DAYS_OF_WEEK, CONF_HOURS, \
CONF_MINUTES, CONF_MONTHS, CONF_ON_TIME, CONF_SECONDS, CONF_TIMEZONE, CONF_TRIGGER_ID, \ CONF_MINUTES, CONF_MONTHS, CONF_ON_TIME, CONF_SECONDS, CONF_TIMEZONE, CONF_TRIGGER_ID, \
CONF_AT, CONF_SECOND, CONF_HOUR, CONF_MINUTE CONF_AT, CONF_SECOND, CONF_HOUR, CONF_MINUTE
from esphome.core import coroutine, coroutine_with_priority from esphome.core import coroutine, coroutine_with_priority
from esphome.automation import Condition
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -24,6 +25,7 @@ time_ns = cg.esphome_ns.namespace('time')
RealTimeClock = time_ns.class_('RealTimeClock', cg.Component) RealTimeClock = time_ns.class_('RealTimeClock', cg.Component)
CronTrigger = time_ns.class_('CronTrigger', automation.Trigger.template(), cg.Component) CronTrigger = time_ns.class_('CronTrigger', automation.Trigger.template(), cg.Component)
ESPTime = time_ns.struct('ESPTime') ESPTime = time_ns.struct('ESPTime')
TimeHasTimeCondition = time_ns.class_('TimeHasTimeCondition', Condition)
def _tz_timedelta(td): def _tz_timedelta(td):
@ -328,3 +330,11 @@ def register_time(time_var, config):
def to_code(config): def to_code(config):
cg.add_define('USE_TIME') cg.add_define('USE_TIME')
cg.add_global(time_ns.using) cg.add_global(time_ns.using)
@automation.register_condition('time.has_time', TimeHasTimeCondition, cv.Schema({
cv.GenerateID(): cv.use_id(RealTimeClock),
}))
def time_has_time_to_code(config, condition_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
yield cg.new_Pvariable(condition_id, template_arg, paren)

View file

@ -2,6 +2,7 @@
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/automation.h"
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <bitset> #include <bitset>
@ -133,5 +134,14 @@ class RealTimeClock : public Component {
std::string timezone_{}; std::string timezone_{};
}; };
template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
public:
TimeHasTimeCondition(RealTimeClock *parent) : parent_(parent) {}
bool check(Ts... x) override { return this->parent_->now().is_valid(); }
protected:
RealTimeClock *parent_;
};
} // namespace time } // namespace time
} // namespace esphome } // namespace esphome

View file

@ -8,6 +8,7 @@ esphome:
- wait_until: - wait_until:
- api.connected - api.connected
- wifi.connected - wifi.connected
- time.has_time
includes: includes:
- custom.h - custom.h