## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] Tests have been added to verify that the new code works (under `tests/` folder). If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
80 lines
2.7 KiB
C++
80 lines
2.7 KiB
C++
#include "automation.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace time {
|
|
|
|
static const char *TAG = "something.something";
|
|
|
|
void CronTrigger::add_second(uint8_t second) { this->seconds_[second] = true; }
|
|
void CronTrigger::add_minute(uint8_t minute) { this->minutes_[minute] = true; }
|
|
void CronTrigger::add_hour(uint8_t hour) { this->hours_[hour] = true; }
|
|
void CronTrigger::add_day_of_month(uint8_t day_of_month) { this->days_of_month_[day_of_month] = true; }
|
|
void CronTrigger::add_month(uint8_t month) { this->months_[month] = true; }
|
|
void CronTrigger::add_day_of_week(uint8_t day_of_week) { this->days_of_week_[day_of_week] = true; }
|
|
bool CronTrigger::matches(const ESPTime &time) {
|
|
return time.is_valid() && this->seconds_[time.second] && this->minutes_[time.minute] && this->hours_[time.hour] &&
|
|
this->days_of_month_[time.day_of_month] && this->months_[time.month] && this->days_of_week_[time.day_of_week];
|
|
}
|
|
void CronTrigger::loop() {
|
|
ESPTime time = this->rtc_->now();
|
|
if (!time.is_valid())
|
|
return;
|
|
|
|
if (this->last_check_.has_value()) {
|
|
if (*this->last_check_ >= time) {
|
|
// already handled this one
|
|
return;
|
|
}
|
|
|
|
while (true) {
|
|
this->last_check_->increment_second();
|
|
if (*this->last_check_ >= time)
|
|
break;
|
|
|
|
if (this->matches(*this->last_check_))
|
|
this->trigger();
|
|
}
|
|
}
|
|
|
|
this->last_check_ = time;
|
|
if (!time.in_range()) {
|
|
ESP_LOGW(TAG, "Time is out of range!");
|
|
ESP_LOGD(TAG, "Second=%02u Minute=%02u Hour=%02u DayOfWeek=%u DayOfMonth=%u DayOfYear=%u Month=%u time=%ld",
|
|
time.second, time.minute, time.hour, time.day_of_week, time.day_of_month, time.day_of_year, time.month,
|
|
time.time);
|
|
}
|
|
|
|
if (this->matches(time))
|
|
this->trigger();
|
|
}
|
|
CronTrigger::CronTrigger(RealTimeClock *rtc) : rtc_(rtc) {}
|
|
void CronTrigger::add_seconds(const std::vector<uint8_t> &seconds) {
|
|
for (uint8_t it : seconds)
|
|
this->add_second(it);
|
|
}
|
|
void CronTrigger::add_minutes(const std::vector<uint8_t> &minutes) {
|
|
for (uint8_t it : minutes)
|
|
this->add_minute(it);
|
|
}
|
|
void CronTrigger::add_hours(const std::vector<uint8_t> &hours) {
|
|
for (uint8_t it : hours)
|
|
this->add_hour(it);
|
|
}
|
|
void CronTrigger::add_days_of_month(const std::vector<uint8_t> &days_of_month) {
|
|
for (uint8_t it : days_of_month)
|
|
this->add_day_of_month(it);
|
|
}
|
|
void CronTrigger::add_months(const std::vector<uint8_t> &months) {
|
|
for (uint8_t it : months)
|
|
this->add_month(it);
|
|
}
|
|
void CronTrigger::add_days_of_week(const std::vector<uint8_t> &days_of_week) {
|
|
for (uint8_t it : days_of_week)
|
|
this->add_day_of_week(it);
|
|
}
|
|
float CronTrigger::get_setup_priority() const { return setup_priority::HARDWARE; }
|
|
|
|
} // namespace time
|
|
} // namespace esphome
|