make time components polling components (#1443)

* make real time clock components polling components

* add test
This commit is contained in:
Florian Mösch 2021-01-12 19:37:22 +01:00 committed by Jesse Hills
parent 717aab7c8b
commit 652f6058d1
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
12 changed files with 20 additions and 21 deletions

View file

@ -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);

View file

@ -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();

View file

@ -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):

View file

@ -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);

View file

@ -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

View file

@ -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;

View file

@ -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;

View file

@ -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:

View file

@ -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

View file

@ -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 {

View file

@ -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();

View file

@ -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'