ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
sun_text_sensor.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/time.h"
5
8
9namespace esphome {
10namespace 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 char *format) { this->format_ = format; }
19 void set_format(const std::string &format) = delete;
20
21 void update() override {
23 if (this->sunrise_) {
24 res = this->parent_->sunrise(this->elevation_);
25 } else {
26 res = this->parent_->sunset(this->elevation_);
27 }
28 if (!res) {
29 this->publish_state("");
30 return;
31 }
32
34 size_t len = res->strftime_to(buf, this->format_);
35 this->publish_state(buf, len);
36 }
37
38 void dump_config() override;
39
40 protected:
41 const char *format_{nullptr};
43 double elevation_;
45};
46
47} // namespace sun
48} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:527
optional< ESPTime > sunset(double elevation)
Definition sun.cpp:319
optional< ESPTime > sunrise(double elevation)
Definition sun.cpp:318
void set_format(const char *format)
void set_sunrise(bool sunrise)
void set_format(const std::string &format)=delete
Prevent accidental use of std::string which would dangle.
void set_elevation(double elevation)
void publish_state(const std::string &state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:817
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:23