mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	Get Sunrise & Sunset for a Specific Date (#4712)
* Update real_time_clock.cpp * Update real_time_clock.h * Update sun.h * Update sun.h * Update sun.h * Enable the sunAtLocation to be used externally * Enable the sunAtLocation to be used externally * Update sun.h * Update sun.h * update * update * update to only use one function * Update sun.h * Update sun.cpp
This commit is contained in:
		| @@ -287,18 +287,17 @@ HorizontalCoordinate Sun::calc_coords_() { | ||||
|   */ | ||||
|   return sun.true_coordinate(m); | ||||
| } | ||||
| optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) { | ||||
| optional<time::ESPTime> Sun::calc_event_(time::ESPTime date, bool rising, double zenith) { | ||||
|   SunAtLocation sun{location_}; | ||||
|   auto now = this->time_->utcnow(); | ||||
|   if (!now.is_valid()) | ||||
|   if (!date.is_valid()) | ||||
|     return {}; | ||||
|   // Calculate UT1 timestamp at 0h | ||||
|   auto today = now; | ||||
|   auto today = date; | ||||
|   today.hour = today.minute = today.second = 0; | ||||
|   today.recalc_timestamp_utc(); | ||||
|  | ||||
|   auto it = sun.event(rising, today, zenith); | ||||
|   if (it.has_value() && it->timestamp < now.timestamp) { | ||||
|   if (it.has_value() && it->timestamp < date.timestamp) { | ||||
|     // We're calculating *next* sunrise/sunset, but calculated event | ||||
|     // is today, so try again tomorrow | ||||
|     time_t new_timestamp = today.timestamp + 24 * 60 * 60; | ||||
| @@ -307,9 +306,19 @@ optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) { | ||||
|   } | ||||
|   return it; | ||||
| } | ||||
| optional<time::ESPTime> Sun::calc_event_(bool rising, double zenith) { | ||||
|   auto it = Sun::calc_event_(this->time_->utcnow(), rising, zenith); | ||||
|   return it; | ||||
| } | ||||
|  | ||||
| optional<time::ESPTime> Sun::sunrise(double elevation) { return this->calc_event_(true, 90 - elevation); } | ||||
| optional<time::ESPTime> Sun::sunset(double elevation) { return this->calc_event_(false, 90 - elevation); } | ||||
| optional<time::ESPTime> Sun::sunrise(time::ESPTime date, double elevation) { | ||||
|   return this->calc_event_(date, true, 90 - elevation); | ||||
| } | ||||
| optional<time::ESPTime> Sun::sunset(time::ESPTime date, double elevation) { | ||||
|   return this->calc_event_(date, false, 90 - elevation); | ||||
| } | ||||
| double Sun::elevation() { return this->calc_coords_().elevation; } | ||||
| double Sun::azimuth() { return this->calc_coords_().azimuth; } | ||||
|  | ||||
|   | ||||
| @@ -59,6 +59,8 @@ class Sun { | ||||
|  | ||||
|   optional<time::ESPTime> sunrise(double elevation); | ||||
|   optional<time::ESPTime> sunset(double elevation); | ||||
|   optional<time::ESPTime> sunrise(time::ESPTime date, double elevation); | ||||
|   optional<time::ESPTime> sunset(time::ESPTime date, double elevation); | ||||
|  | ||||
|   double elevation(); | ||||
|   double azimuth(); | ||||
| @@ -66,6 +68,7 @@ class Sun { | ||||
|  protected: | ||||
|   internal::HorizontalCoordinate calc_coords_(); | ||||
|   optional<time::ESPTime> calc_event_(bool rising, double zenith); | ||||
|   optional<time::ESPTime> calc_event_(time::ESPTime date, bool rising, double zenith); | ||||
|  | ||||
|   time::RealTimeClock *time_; | ||||
|   internal::GeoLocation location_; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user