From bf453ad8cd64848e9d23db70ec46845812a19918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=B6sch?= Date: Tue, 12 Jan 2021 19:37:22 +0100 Subject: [PATCH] make time components polling components (#1443) * make real time clock components polling components * add test --- esphome/components/ds1307/ds1307.cpp | 3 ++- esphome/components/ds1307/ds1307.h | 1 + esphome/components/gps/time/__init__.py | 4 ++-- esphome/components/gps/time/gps_time.h | 4 +--- .../homeassistant/time/homeassistant_time.cpp | 14 +++++--------- .../homeassistant/time/homeassistant_time.h | 1 + esphome/components/sntp/sntp_component.cpp | 1 + esphome/components/sntp/sntp_component.h | 1 + esphome/components/time/__init__.py | 4 ++-- esphome/components/time/real_time_clock.cpp | 2 +- esphome/components/time/real_time_clock.h | 2 +- tests/test1.yaml | 4 ++-- 12 files changed, 20 insertions(+), 21 deletions(-) diff --git a/esphome/components/ds1307/ds1307.cpp b/esphome/components/ds1307/ds1307.cpp index 990767ddd4..599ec16e4f 100644 --- a/esphome/components/ds1307/ds1307.cpp +++ b/esphome/components/ds1307/ds1307.cpp @@ -14,9 +14,10 @@ void DS1307Component::setup() { if (!this->read_rtc_()) { this->mark_failed(); } - this->set_interval(15 * 60 * 1000, [&]() { this->read(); }); } +void DS1307Component::update() { this->read(); } + void DS1307Component::dump_config() { ESP_LOGCONFIG(TAG, "DS1307:"); LOG_I2C_DEVICE(this); diff --git a/esphome/components/ds1307/ds1307.h b/esphome/components/ds1307/ds1307.h index 1f732a7c7d..8e318bf395 100644 --- a/esphome/components/ds1307/ds1307.h +++ b/esphome/components/ds1307/ds1307.h @@ -10,6 +10,7 @@ namespace ds1307 { class DS1307Component : public time::RealTimeClock, public i2c::I2CDevice { public: void setup() override; + void update() override; void dump_config() override; float get_setup_priority() const override; void read(); diff --git a/esphome/components/gps/time/__init__.py b/esphome/components/gps/time/__init__.py index bf746d19b2..421d2e6717 100644 --- a/esphome/components/gps/time/__init__.py +++ b/esphome/components/gps/time/__init__.py @@ -6,12 +6,12 @@ from .. import gps_ns, GPSListener, CONF_GPS_ID, GPS DEPENDENCIES = ['gps'] -GPSTime = gps_ns.class_('GPSTime', time_.RealTimeClock, GPSListener) +GPSTime = gps_ns.class_('GPSTime', cg.PollingComponent, time_.RealTimeClock, GPSListener) CONFIG_SCHEMA = time_.TIME_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(GPSTime), cv.GenerateID(CONF_GPS_ID): cv.use_id(GPS), -}).extend(cv.COMPONENT_SCHEMA) +}).extend(cv.polling_component_schema('5min')) def to_code(config): diff --git a/esphome/components/gps/time/gps_time.h b/esphome/components/gps/time/gps_time.h index f6462be3e0..a1f69a7130 100644 --- a/esphome/components/gps/time/gps_time.h +++ b/esphome/components/gps/time/gps_time.h @@ -9,13 +9,11 @@ namespace gps { class GPSTime : public time::RealTimeClock, public GPSListener { public: + void update() override { this->from_tiny_gps_(this->get_tiny_gps()); }; void on_update(TinyGPSPlus &tiny_gps) override { if (!this->has_time_) this->from_tiny_gps_(tiny_gps); } - void setup() override { - this->set_interval(5 * 60 * 1000, [this]() { this->from_tiny_gps_(this->get_tiny_gps()); }); - } protected: void from_tiny_gps_(TinyGPSPlus &tiny_gps); diff --git a/esphome/components/homeassistant/time/homeassistant_time.cpp b/esphome/components/homeassistant/time/homeassistant_time.cpp index e9d97690fb..9ace8cf67f 100644 --- a/esphome/components/homeassistant/time/homeassistant_time.cpp +++ b/esphome/components/homeassistant/time/homeassistant_time.cpp @@ -10,17 +10,13 @@ void HomeassistantTime::dump_config() { ESP_LOGCONFIG(TAG, "Home Assistant Time:"); ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str()); } -float HomeassistantTime::get_setup_priority() const { return setup_priority::DATA; } -void HomeassistantTime::setup() { - global_homeassistant_time = this; - this->set_interval(15 * 60 * 1000, []() { - // re-request time every 15 minutes - api::global_api_server->request_time(); - }); -} +float HomeassistantTime::get_setup_priority() const { return setup_priority::DATA; } + +void HomeassistantTime::setup() { global_homeassistant_time = this; } + +void HomeassistantTime::update() { api::global_api_server->request_time(); } HomeassistantTime *global_homeassistant_time = nullptr; - } // namespace homeassistant } // namespace esphome diff --git a/esphome/components/homeassistant/time/homeassistant_time.h b/esphome/components/homeassistant/time/homeassistant_time.h index 8ab09d1185..94f4704c2f 100644 --- a/esphome/components/homeassistant/time/homeassistant_time.h +++ b/esphome/components/homeassistant/time/homeassistant_time.h @@ -10,6 +10,7 @@ namespace homeassistant { class HomeassistantTime : public time::RealTimeClock { public: void setup() override; + void update() override; void dump_config() override; void set_epoch_time(uint32_t epoch) { this->synchronize_epoch_(epoch); } float get_setup_priority() const override; diff --git a/esphome/components/sntp/sntp_component.cpp b/esphome/components/sntp/sntp_component.cpp index 253f3ba2c1..641d66091c 100644 --- a/esphome/components/sntp/sntp_component.cpp +++ b/esphome/components/sntp/sntp_component.cpp @@ -42,6 +42,7 @@ void SNTPComponent::dump_config() { ESP_LOGCONFIG(TAG, " Server 3: '%s'", this->server_3_.c_str()); ESP_LOGCONFIG(TAG, " Timezone: '%s'", this->timezone_.c_str()); } +void SNTPComponent::update() {} void SNTPComponent::loop() { if (this->has_time_) return; diff --git a/esphome/components/sntp/sntp_component.h b/esphome/components/sntp/sntp_component.h index 785f458d6c..4c70a6b09f 100644 --- a/esphome/components/sntp/sntp_component.h +++ b/esphome/components/sntp/sntp_component.h @@ -24,6 +24,7 @@ class SNTPComponent : public time::RealTimeClock { } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } + void update() override; void loop() override; protected: diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index 5f071aef11..e5ed5034ab 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -22,7 +22,7 @@ CODEOWNERS = ['@OttoWinter'] IS_PLATFORM_COMPONENT = True time_ns = cg.esphome_ns.namespace('time') -RealTimeClock = time_ns.class_('RealTimeClock', cg.Component) +RealTimeClock = time_ns.class_('RealTimeClock', cg.PollingComponent) CronTrigger = time_ns.class_('CronTrigger', automation.Trigger.template(), cg.Component) ESPTime = time_ns.struct('ESPTime') TimeHasTimeCondition = time_ns.class_('TimeHasTimeCondition', Condition) @@ -294,7 +294,7 @@ TIME_SCHEMA = cv.Schema({ cv.Optional(CONF_CRON): validate_cron_raw, cv.Optional(CONF_AT): validate_time_at, }, validate_cron_keys), -}) +}).extend(cv.polling_component_schema('15min')) @coroutine diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index cdcfcb14ad..7d7c1013dd 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -15,7 +15,7 @@ RealTimeClock::RealTimeClock() = default; void RealTimeClock::call_setup() { setenv("TZ", this->timezone_.c_str(), 1); tzset(); - this->setup(); + PollingComponent::call_setup(); } void RealTimeClock::synchronize_epoch_(uint32_t epoch) { struct timeval timev { diff --git a/esphome/components/time/real_time_clock.h b/esphome/components/time/real_time_clock.h index 8de4509089..880a4e9d5c 100644 --- a/esphome/components/time/real_time_clock.h +++ b/esphome/components/time/real_time_clock.h @@ -106,7 +106,7 @@ struct ESPTime { /// The C library (newlib) available on ESPs only supports TZ strings that specify an offset and DST info; /// you cannot specify zone names or paths to zoneinfo files. /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html -class RealTimeClock : public Component { +class RealTimeClock : public PollingComponent { public: explicit RealTimeClock(); diff --git a/tests/test1.yaml b/tests/test1.yaml index ef98c10ad5..4598049732 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1843,6 +1843,7 @@ time: then: - lambda: 'ESP_LOGD("main", "time");' - platform: gps + update_interval: 1h on_time: seconds: 0 minutes: /15 @@ -1851,13 +1852,12 @@ time: id: ds1307_time - platform: ds1307 id: ds1307_time + update_interval: never on_time: seconds: 0 then: ds1307.read - - cover: - platform: template name: 'Template Cover'