mirror of
https://github.com/brianhanifin/esphome-config.git
synced 2026-07-27 19:55:41 +00:00
76 lines
2.3 KiB
C++
76 lines
2.3 KiB
C++
#include "esphome.h"
|
|
|
|
// Declare functions before calling them.
|
|
bool scheduled_runtime(std::string);
|
|
std::string update_next_runtime(std::string);
|
|
|
|
bool scheduled_runtime(std::string time) {
|
|
// Retrieve the current time.
|
|
auto time_now = id(homeassistant_time).now();
|
|
int time_hour = time_now.hour;
|
|
int time_minute = time_now.minute;
|
|
|
|
// Split the hour and minutes.
|
|
int next_hour = atoi(time.substr(0,2).c_str());
|
|
int next_minute = atoi(time.substr(3,2).c_str());
|
|
|
|
//ESP_LOGD("scheduled_runtime()", "now: %i:%i", next_hour, next_minute);
|
|
return (time_hour == next_hour && time_minute == next_minute);
|
|
}
|
|
|
|
std::string update_next_runtime(std::string time_list) {
|
|
// Initialize variables.
|
|
std::vector<std::string> times;
|
|
std::vector<std::string> next_time;
|
|
char * token;
|
|
|
|
// Split the list of run times into an array.
|
|
token = strtok(&time_list[0], ",");
|
|
while (token != NULL) {
|
|
times.push_back(token);
|
|
token = strtok(NULL, ",");
|
|
}
|
|
|
|
// Stop now if the list does not contain more than one time.
|
|
if (times.size() <= 1) {
|
|
return time_list;
|
|
}
|
|
|
|
// Retrieve the current time.
|
|
auto time_now = id(homeassistant_time).now();
|
|
int time_hour = time_now.hour;
|
|
int time_minute = time_now.minute;
|
|
|
|
// Initialize variables.
|
|
int next_hour = 0;
|
|
int next_minute = 0;
|
|
int index = 0;
|
|
int loop_count = 0;
|
|
int time_count = times.size()-1;
|
|
|
|
// Compare the list of times with the current time, and return the next in the list.
|
|
//ESP_LOGD("update_next_runtime", "now: %i:%i", hour, minute);
|
|
for (std::string time : times) {
|
|
// Retrieve the next scheduled time from the list.
|
|
next_hour = atoi(time.substr(0,2).c_str());
|
|
next_minute = atoi(time.substr(3,2).c_str());
|
|
|
|
//ESP_LOGD("update_next_runtime", "next_hour: %s", time.c_str());
|
|
if (time_hour < next_hour || (time_hour == next_hour && time_minute < next_minute)) {
|
|
// Return this time if the next hour is greater than the current hour.
|
|
return times[loop_count].c_str();
|
|
break;
|
|
// When we reach the end of our schedule for the day, return the first time of tomorrow.
|
|
} else if (time_count == loop_count) {
|
|
return times[0].c_str();
|
|
break;
|
|
}
|
|
|
|
// Increment the loop counter and array index.
|
|
loop_count += 1;
|
|
index += 2;
|
|
}
|
|
|
|
return "unknown";
|
|
}
|