mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 18:48:28 +02:00
Add support for time entities (#6399)
* Add time entities * Add tests * Add myself to datetime codeowners * Fix publishing times with 0 values * Log performing TimeCall * Implement `on_time` trigger * Rename var * Fix initial value for time * Add arg name for clarity * Remove useless checks
This commit is contained in:
@@ -42,6 +42,9 @@
|
||||
#ifdef USE_DATETIME_DATE
|
||||
#include "esphome/components/datetime/date_entity.h"
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
#include "esphome/components/datetime/time_entity.h"
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
#include "esphome/components/text/text.h"
|
||||
#endif
|
||||
@@ -128,6 +131,10 @@ class Application {
|
||||
void register_date(datetime::DateEntity *date) { this->dates_.push_back(date); }
|
||||
#endif
|
||||
|
||||
#ifdef USE_DATETIME_TIME
|
||||
void register_time(datetime::TimeEntity *time) { this->times_.push_back(time); }
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT
|
||||
void register_text(text::Text *text) { this->texts_.push_back(text); }
|
||||
#endif
|
||||
@@ -305,6 +312,15 @@ class Application {
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
const std::vector<datetime::TimeEntity *> &get_times() { return this->times_; }
|
||||
datetime::TimeEntity *get_time_by_key(uint32_t key, bool include_internal = false) {
|
||||
for (auto *obj : this->times_)
|
||||
if (obj->get_object_id_hash() == key && (include_internal || !obj->is_internal()))
|
||||
return obj;
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
const std::vector<text::Text *> &get_texts() { return this->texts_; }
|
||||
text::Text *get_text_by_key(uint32_t key, bool include_internal = false) {
|
||||
@@ -401,6 +417,9 @@ class Application {
|
||||
#ifdef USE_DATETIME_DATE
|
||||
std::vector<datetime::DateEntity *> dates_{};
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
std::vector<datetime::TimeEntity *> times_{};
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
std::vector<select::Select *> selects_{};
|
||||
#endif
|
||||
|
||||
@@ -217,6 +217,21 @@ void ComponentIterator::advance() {
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
case IteratorState::DATETIME_TIME:
|
||||
if (this->at_ >= App.get_times().size()) {
|
||||
advance_platform = true;
|
||||
} else {
|
||||
auto *time = App.get_times()[this->at_];
|
||||
if (time->is_internal() && !this->include_internal_) {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
success = this->on_time(time);
|
||||
}
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
case IteratorState::TEXT:
|
||||
if (this->at_ >= App.get_texts().size()) {
|
||||
|
||||
@@ -60,6 +60,9 @@ class ComponentIterator {
|
||||
#ifdef USE_DATETIME_DATE
|
||||
virtual bool on_date(datetime::DateEntity *date) = 0;
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
virtual bool on_time(datetime::TimeEntity *time) = 0;
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
virtual bool on_text(text::Text *text) = 0;
|
||||
#endif
|
||||
@@ -120,6 +123,9 @@ class ComponentIterator {
|
||||
#ifdef USE_DATETIME_DATE
|
||||
DATETIME_DATE,
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
DATETIME_TIME,
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
TEXT,
|
||||
#endif
|
||||
|
||||
@@ -65,6 +65,12 @@ void Controller::setup_controller(bool include_internal) {
|
||||
obj->add_on_state_callback([this, obj]() { this->on_date_update(obj); });
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
for (auto *obj : App.get_times()) {
|
||||
if (include_internal || !obj->is_internal())
|
||||
obj->add_on_state_callback([this, obj]() { this->on_time_update(obj); });
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
for (auto *obj : App.get_texts()) {
|
||||
if (include_internal || !obj->is_internal())
|
||||
|
||||
@@ -34,6 +34,9 @@
|
||||
#ifdef USE_DATETIME_DATE
|
||||
#include "esphome/components/datetime/date_entity.h"
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
#include "esphome/components/datetime/time_entity.h"
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
#include "esphome/components/text/text.h"
|
||||
#endif
|
||||
@@ -85,6 +88,9 @@ class Controller {
|
||||
#ifdef USE_DATETIME_DATE
|
||||
virtual void on_date_update(datetime::DateEntity *obj){};
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
virtual void on_time_update(datetime::TimeEntity *obj){};
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
virtual void on_text_update(text::Text *obj, const std::string &state){};
|
||||
#endif
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
#define USE_NUMBER
|
||||
#define USE_DATETIME
|
||||
#define USE_DATETIME_DATE
|
||||
#define USE_DATETIME_TIME
|
||||
#define USE_OTA
|
||||
#define USE_OTA_PASSWORD
|
||||
#define USE_OTA_STATE_CALLBACK
|
||||
|
||||
Reference in New Issue
Block a user