time sync notification (#1442)

* add on_time_sync trigger

* cleanup lint

* fix review remark (sntp didn't trigger callbacks)
This commit is contained in:
Florian Mösch 2021-01-18 13:34:50 +01:00 committed by GitHub
parent d9a2651a5a
commit 4c105398f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 6 deletions

View file

@ -54,6 +54,7 @@ void SNTPComponent::loop() {
char buf[128];
time.strftime(buf, sizeof(buf), "%c");
ESP_LOGD(TAG, "Synchronized time: %s", buf);
this->time_sync_callback_.call();
this->has_time_ = true;
}

View file

@ -11,8 +11,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
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_AT, CONF_SECOND, CONF_HOUR, CONF_MINUTE
CONF_MINUTES, CONF_MONTHS, CONF_ON_TIME, CONF_ON_TIME_SYNC, CONF_SECONDS, CONF_TIMEZONE, \
CONF_TRIGGER_ID, CONF_AT, CONF_SECOND, CONF_HOUR, CONF_MINUTE
from esphome.core import coroutine, coroutine_with_priority
from esphome.automation import Condition
@ -24,6 +24,7 @@ IS_PLATFORM_COMPONENT = True
time_ns = cg.esphome_ns.namespace('time')
RealTimeClock = time_ns.class_('RealTimeClock', cg.PollingComponent)
CronTrigger = time_ns.class_('CronTrigger', automation.Trigger.template(), cg.Component)
SyncTrigger = time_ns.class_('SyncTrigger', automation.Trigger.template(), cg.Component)
ESPTime = time_ns.struct('ESPTime')
TimeHasTimeCondition = time_ns.class_('TimeHasTimeCondition', Condition)
@ -294,6 +295,9 @@ TIME_SCHEMA = cv.Schema({
cv.Optional(CONF_CRON): validate_cron_raw,
cv.Optional(CONF_AT): validate_time_at,
}, validate_cron_keys),
cv.Optional(CONF_ON_TIME_SYNC): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SyncTrigger),
}),
}).extend(cv.polling_component_schema('15min'))
@ -320,6 +324,12 @@ def setup_time_core_(time_var, config):
yield cg.register_component(trigger, conf)
yield automation.build_automation(trigger, [], conf)
for conf in config.get(CONF_ON_TIME_SYNC, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
yield cg.register_component(trigger, conf)
yield automation.build_automation(trigger, [], conf)
@coroutine
def register_time(time_var, config):

View file

@ -75,5 +75,9 @@ void CronTrigger::add_days_of_week(const std::vector<uint8_t> &days_of_week) {
}
float CronTrigger::get_setup_priority() const { return setup_priority::HARDWARE; }
SyncTrigger::SyncTrigger(RealTimeClock *rtc) : rtc_(rtc) {
rtc->add_on_time_sync_callback([this]() { this->trigger(); });
}
} // namespace time
} // namespace esphome

View file

@ -37,5 +37,12 @@ class CronTrigger : public Trigger<>, public Component {
optional<ESPTime> last_check_;
};
class SyncTrigger : public Trigger<>, public Component {
public:
explicit SyncTrigger(RealTimeClock *rtc);
protected:
RealTimeClock *rtc_;
};
} // namespace time
} // namespace esphome

View file

@ -38,6 +38,8 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
char buf[128];
time.strftime(buf, sizeof(buf), "%c");
ESP_LOGD(TAG, "Synchronized time: %s", buf);
this->time_sync_callback_.call();
}
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {

View file

@ -127,11 +127,17 @@ class RealTimeClock : public PollingComponent {
void call_setup() override;
void add_on_time_sync_callback(std::function<void()> callback) {
this->time_sync_callback_.add(std::move(callback));
};
protected:
/// Report a unix epoch as current time.
void synchronize_epoch_(uint32_t epoch);
std::string timezone_{};
CallbackManager<void()> time_sync_callback_;
};
template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {

View file

@ -350,6 +350,7 @@ CONF_ON_SHUTDOWN = 'on_shutdown'
CONF_ON_STATE = 'on_state'
CONF_ON_TAG = 'on_tag'
CONF_ON_TIME = 'on_time'
CONF_ON_TIME_SYNC = 'on_time_sync'
CONF_ON_TURN_OFF = 'on_turn_off'
CONF_ON_TURN_ON = 'on_turn_on'
CONF_ON_VALUE = 'on_value'

View file

@ -1854,10 +1854,7 @@ time:
then:
- lambda: 'ESP_LOGD("main", "time");'
- platform: gps
update_interval: 1h
on_time:
seconds: 0
minutes: /15
on_time_sync:
then:
ds1307.write:
id: ds1307_time