mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	make time components polling components (#1443)
* make real time clock components polling components * add test
This commit is contained in:
		
				
					committed by
					
						 Jesse Hills
						Jesse Hills
					
				
			
			
				
	
			
			
			
						parent
						
							717aab7c8b
						
					
				
				
					commit
					652f6058d1
				
			| @@ -14,9 +14,10 @@ void DS1307Component::setup() { | |||||||
|   if (!this->read_rtc_()) { |   if (!this->read_rtc_()) { | ||||||
|     this->mark_failed(); |     this->mark_failed(); | ||||||
|   } |   } | ||||||
|   this->set_interval(15 * 60 * 1000, [&]() { this->read(); }); |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void DS1307Component::update() { this->read(); } | ||||||
|  |  | ||||||
| void DS1307Component::dump_config() { | void DS1307Component::dump_config() { | ||||||
|   ESP_LOGCONFIG(TAG, "DS1307:"); |   ESP_LOGCONFIG(TAG, "DS1307:"); | ||||||
|   LOG_I2C_DEVICE(this); |   LOG_I2C_DEVICE(this); | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ namespace ds1307 { | |||||||
| class DS1307Component : public time::RealTimeClock, public i2c::I2CDevice { | class DS1307Component : public time::RealTimeClock, public i2c::I2CDevice { | ||||||
|  public: |  public: | ||||||
|   void setup() override; |   void setup() override; | ||||||
|  |   void update() override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   float get_setup_priority() const override; |   float get_setup_priority() const override; | ||||||
|   void read(); |   void read(); | ||||||
|   | |||||||
| @@ -6,12 +6,12 @@ from .. import gps_ns, GPSListener, CONF_GPS_ID, GPS | |||||||
|  |  | ||||||
| DEPENDENCIES = ['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({ | CONFIG_SCHEMA = time_.TIME_SCHEMA.extend({ | ||||||
|     cv.GenerateID(): cv.declare_id(GPSTime), |     cv.GenerateID(): cv.declare_id(GPSTime), | ||||||
|     cv.GenerateID(CONF_GPS_ID): cv.use_id(GPS), |     cv.GenerateID(CONF_GPS_ID): cv.use_id(GPS), | ||||||
| }).extend(cv.COMPONENT_SCHEMA) | }).extend(cv.polling_component_schema('5min')) | ||||||
|  |  | ||||||
|  |  | ||||||
| def to_code(config): | def to_code(config): | ||||||
|   | |||||||
| @@ -9,13 +9,11 @@ namespace gps { | |||||||
|  |  | ||||||
| class GPSTime : public time::RealTimeClock, public GPSListener { | class GPSTime : public time::RealTimeClock, public GPSListener { | ||||||
|  public: |  public: | ||||||
|  |   void update() override { this->from_tiny_gps_(this->get_tiny_gps()); }; | ||||||
|   void on_update(TinyGPSPlus &tiny_gps) override { |   void on_update(TinyGPSPlus &tiny_gps) override { | ||||||
|     if (!this->has_time_) |     if (!this->has_time_) | ||||||
|       this->from_tiny_gps_(tiny_gps); |       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: |  protected: | ||||||
|   void from_tiny_gps_(TinyGPSPlus &tiny_gps); |   void from_tiny_gps_(TinyGPSPlus &tiny_gps); | ||||||
|   | |||||||
| @@ -10,17 +10,13 @@ void HomeassistantTime::dump_config() { | |||||||
|   ESP_LOGCONFIG(TAG, "Home Assistant Time:"); |   ESP_LOGCONFIG(TAG, "Home Assistant Time:"); | ||||||
|   ESP_LOGCONFIG(TAG, "  Timezone: '%s'", this->timezone_.c_str()); |   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, []() { | float HomeassistantTime::get_setup_priority() const { return setup_priority::DATA; } | ||||||
|     // re-request time every 15 minutes |  | ||||||
|     api::global_api_server->request_time(); | void HomeassistantTime::setup() { global_homeassistant_time = this; } | ||||||
|   }); |  | ||||||
| } | void HomeassistantTime::update() { api::global_api_server->request_time(); } | ||||||
|  |  | ||||||
| HomeassistantTime *global_homeassistant_time = nullptr; | HomeassistantTime *global_homeassistant_time = nullptr; | ||||||
|  |  | ||||||
| }  // namespace homeassistant | }  // namespace homeassistant | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ namespace homeassistant { | |||||||
| class HomeassistantTime : public time::RealTimeClock { | class HomeassistantTime : public time::RealTimeClock { | ||||||
|  public: |  public: | ||||||
|   void setup() override; |   void setup() override; | ||||||
|  |   void update() override; | ||||||
|   void dump_config() override; |   void dump_config() override; | ||||||
|   void set_epoch_time(uint32_t epoch) { this->synchronize_epoch_(epoch); } |   void set_epoch_time(uint32_t epoch) { this->synchronize_epoch_(epoch); } | ||||||
|   float get_setup_priority() const override; |   float get_setup_priority() const override; | ||||||
|   | |||||||
| @@ -42,6 +42,7 @@ void SNTPComponent::dump_config() { | |||||||
|   ESP_LOGCONFIG(TAG, "  Server 3: '%s'", this->server_3_.c_str()); |   ESP_LOGCONFIG(TAG, "  Server 3: '%s'", this->server_3_.c_str()); | ||||||
|   ESP_LOGCONFIG(TAG, "  Timezone: '%s'", this->timezone_.c_str()); |   ESP_LOGCONFIG(TAG, "  Timezone: '%s'", this->timezone_.c_str()); | ||||||
| } | } | ||||||
|  | void SNTPComponent::update() {} | ||||||
| void SNTPComponent::loop() { | void SNTPComponent::loop() { | ||||||
|   if (this->has_time_) |   if (this->has_time_) | ||||||
|     return; |     return; | ||||||
|   | |||||||
| @@ -24,6 +24,7 @@ class SNTPComponent : public time::RealTimeClock { | |||||||
|   } |   } | ||||||
|   float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } |   float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } | ||||||
|  |  | ||||||
|  |   void update() override; | ||||||
|   void loop() override; |   void loop() override; | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ CODEOWNERS = ['@OttoWinter'] | |||||||
| IS_PLATFORM_COMPONENT = True | IS_PLATFORM_COMPONENT = True | ||||||
|  |  | ||||||
| time_ns = cg.esphome_ns.namespace('time') | 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) | CronTrigger = time_ns.class_('CronTrigger', automation.Trigger.template(), cg.Component) | ||||||
| ESPTime = time_ns.struct('ESPTime') | ESPTime = time_ns.struct('ESPTime') | ||||||
| TimeHasTimeCondition = time_ns.class_('TimeHasTimeCondition', Condition) | TimeHasTimeCondition = time_ns.class_('TimeHasTimeCondition', Condition) | ||||||
| @@ -294,7 +294,7 @@ TIME_SCHEMA = cv.Schema({ | |||||||
|         cv.Optional(CONF_CRON): validate_cron_raw, |         cv.Optional(CONF_CRON): validate_cron_raw, | ||||||
|         cv.Optional(CONF_AT): validate_time_at, |         cv.Optional(CONF_AT): validate_time_at, | ||||||
|     }, validate_cron_keys), |     }, validate_cron_keys), | ||||||
| }) | }).extend(cv.polling_component_schema('15min')) | ||||||
|  |  | ||||||
|  |  | ||||||
| @coroutine | @coroutine | ||||||
|   | |||||||
| @@ -15,7 +15,7 @@ RealTimeClock::RealTimeClock() = default; | |||||||
| void RealTimeClock::call_setup() { | void RealTimeClock::call_setup() { | ||||||
|   setenv("TZ", this->timezone_.c_str(), 1); |   setenv("TZ", this->timezone_.c_str(), 1); | ||||||
|   tzset(); |   tzset(); | ||||||
|   this->setup(); |   PollingComponent::call_setup(); | ||||||
| } | } | ||||||
| void RealTimeClock::synchronize_epoch_(uint32_t epoch) { | void RealTimeClock::synchronize_epoch_(uint32_t epoch) { | ||||||
|   struct timeval timev { |   struct timeval timev { | ||||||
|   | |||||||
| @@ -106,7 +106,7 @@ struct ESPTime { | |||||||
| /// The C library (newlib) available on ESPs only supports TZ strings that specify an offset and DST info; | /// 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. | /// you cannot specify zone names or paths to zoneinfo files. | ||||||
| /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | /// \see https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html | ||||||
| class RealTimeClock : public Component { | class RealTimeClock : public PollingComponent { | ||||||
|  public: |  public: | ||||||
|   explicit RealTimeClock(); |   explicit RealTimeClock(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1843,6 +1843,7 @@ time: | |||||||
|       then: |       then: | ||||||
|         - lambda: 'ESP_LOGD("main", "time");' |         - lambda: 'ESP_LOGD("main", "time");' | ||||||
|   - platform: gps |   - platform: gps | ||||||
|  |     update_interval: 1h | ||||||
|     on_time: |     on_time: | ||||||
|       seconds: 0 |       seconds: 0 | ||||||
|       minutes: /15 |       minutes: /15 | ||||||
| @@ -1851,13 +1852,12 @@ time: | |||||||
|           id: ds1307_time |           id: ds1307_time | ||||||
|   - platform: ds1307 |   - platform: ds1307 | ||||||
|     id: ds1307_time |     id: ds1307_time | ||||||
|  |     update_interval: never | ||||||
|     on_time: |     on_time: | ||||||
|       seconds: 0 |       seconds: 0 | ||||||
|       then: |       then: | ||||||
|           ds1307.read |           ds1307.read | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| cover: | cover: | ||||||
|   - platform: template |   - platform: template | ||||||
|     name: 'Template Cover' |     name: 'Template Cover' | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user