ESPHome  2023.5.5
sun_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace sun {
9 
11  public:
12  void set_parent(Sun *parent) { parent_ = parent; }
13  void set_elevation(double elevation) { elevation_ = elevation; }
14  void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
15  void set_format(const std::string &format) { format_ = format; }
16 
17  void update() override {
19  if (this->sunrise_) {
20  res = this->parent_->sunrise(this->elevation_);
21  } else {
22  res = this->parent_->sunset(this->elevation_);
23  }
24  if (!res) {
25  this->publish_state("");
26  return;
27  }
28 
29  this->publish_state(res->strftime(this->format_));
30  }
31 
32  void dump_config() override;
33 
34  protected:
35  std::string format_{};
37  double elevation_;
38  bool sunrise_;
39 };
40 
41 } // namespace sun
42 } // namespace esphome
void set_sunrise(bool sunrise)
void set_elevation(double elevation)
void set_format(const std::string &format)
This class simplifies creating components that periodically check a state.
Definition: component.h:282
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
optional< time::ESPTime > sunset(double elevation)
Definition: sun.cpp:315
void set_parent(Sun *parent)
optional< time::ESPTime > sunrise(double elevation)
Definition: sun.cpp:314
Definition: a4988.cpp:4