ESPHome  2024.10.2
sun.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/time.h"
7 
9 
10 namespace esphome {
11 namespace sun {
12 
13 namespace internal {
14 
15 /* Usually, ESPHome uses single-precision floating point values
16  * because those tend to be accurate enough and are more efficient.
17  *
18  * However, some of the data in this class has to be quite accurate, so double is
19  * used everywhere.
20  */
21 using num_t = double;
22 struct GeoLocation {
25 
26  num_t latitude_rad() const;
27  num_t longitude_rad() const;
28 };
29 
30 struct Moment {
32 
33  num_t jd() const;
34  num_t jde() const;
35 };
36 
40 
41  num_t right_ascension_rad() const;
42  num_t declination_rad() const;
43 };
44 
48 
49  num_t elevation_rad() const;
50  num_t azimuth_rad() const;
51 };
52 
53 } // namespace internal
54 
55 class Sun {
56  public:
57  void set_time(time::RealTimeClock *time) { time_ = time; }
58  time::RealTimeClock *get_time() const { return time_; }
59  void set_latitude(double latitude) { location_.latitude = latitude; }
60  void set_longitude(double longitude) { location_.longitude = longitude; }
61 
62  optional<ESPTime> sunrise(double elevation);
63  optional<ESPTime> sunset(double elevation);
64  optional<ESPTime> sunrise(ESPTime date, double elevation);
65  optional<ESPTime> sunset(ESPTime date, double elevation);
66 
67  double elevation();
68  double azimuth();
69 
70  protected:
71  internal::HorizontalCoordinate calc_coords_();
72  optional<ESPTime> calc_event_(bool rising, double zenith);
73  optional<ESPTime> calc_event_(ESPTime date, bool rising, double zenith);
74 
77 };
78 
79 class SunTrigger : public Trigger<>, public PollingComponent, public Parented<Sun> {
80  public:
82 
83  void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
84  void set_elevation(double elevation) { elevation_ = elevation; }
85 
86  void update() override {
87  double current = this->parent_->elevation();
88  if (std::isnan(current))
89  return;
90 
91  bool crossed;
92  if (this->sunrise_) {
93  crossed = this->last_elevation_ <= this->elevation_ && this->elevation_ < current;
94  } else {
95  crossed = this->last_elevation_ >= this->elevation_ && this->elevation_ > current;
96  }
97 
98  if (crossed && !std::isnan(this->last_elevation_)) {
99  this->trigger();
100  }
101  this->last_elevation_ = current;
102  }
103 
104  protected:
105  bool sunrise_;
106  double last_elevation_{NAN};
107  double elevation_;
108 };
109 
110 template<typename... Ts> class SunCondition : public Condition<Ts...>, public Parented<Sun> {
111  public:
112  TEMPLATABLE_VALUE(double, elevation);
113  void set_above(bool above) { above_ = above; }
114 
115  bool check(Ts... x) override {
116  double elevation = this->elevation_.value(x...);
117  double current = this->parent_->elevation();
118  if (this->above_) {
119  return current > elevation;
120  } else {
121  return current < elevation;
122  }
123  }
124 
125  protected:
126  bool above_;
127 };
128 
129 } // namespace sun
130 } // namespace esphome
internal::GeoLocation location_
Definition: sun.h:76
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
uint16_t x
Definition: tt21100.cpp:17
void set_sunrise(bool sunrise)
Definition: sun.h:83
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
void set_longitude(double longitude)
Definition: sun.h:60
void update() override
Definition: sun.h:86
This class simplifies creating components that periodically check a state.
Definition: component.h:283
time::RealTimeClock * get_time() const
Definition: sun.h:58
void set_time(time::RealTimeClock *time)
Definition: sun.h:57
Base class for all automation conditions.
Definition: automation.h:74
void set_elevation(double elevation)
Definition: sun.h:84
void set_above(bool above)
Definition: sun.h:113
bool check(Ts... x) override
Definition: sun.h:115
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
time::RealTimeClock * time_
Definition: sun.h:75
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521
void set_latitude(double latitude)
Definition: sun.h:59