ESPHome  2024.12.2
sun_text_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/time.h"
5 
8 
9 namespace esphome {
10 namespace sun {
11 
13  public:
14  void set_parent(Sun *parent) { parent_ = parent; }
15  void set_elevation(double elevation) { elevation_ = elevation; }
16  void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
17  void set_format(const std::string &format) { format_ = format; }
18 
19  void update() override {
21  if (this->sunrise_) {
22  res = this->parent_->sunrise(this->elevation_);
23  } else {
24  res = this->parent_->sunset(this->elevation_);
25  }
26  if (!res) {
27  this->publish_state("");
28  return;
29  }
30 
31  this->publish_state(res->strftime(this->format_));
32  }
33 
34  void dump_config() override;
35 
36  protected:
37  std::string format_{};
39  double elevation_;
40  bool sunrise_;
41 };
42 
43 } // namespace sun
44 } // 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:283
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
optional< ESPTime > sunrise(double elevation)
Definition: sun.cpp:314
optional< ESPTime > sunset(double elevation)
Definition: sun.cpp:315
void set_parent(Sun *parent)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7