ESPHome 2026.6.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::sun {
10
12 public:
13 void set_parent(Sun *parent) { parent_ = parent; }
14 void set_elevation(double elevation) { elevation_ = elevation; }
15 void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
16 void set_format(const char *format) { this->format_ = format; }
18 void set_format(const std::string &format) = delete;
19
20 void update() override {
21 optional<ESPTime> res;
22 if (this->sunrise_) {
23 res = this->parent_->sunrise(this->elevation_);
24 } else {
25 res = this->parent_->sunset(this->elevation_);
26 }
27 if (!res) {
28 this->publish_state("");
29 return;
30 }
31
33 size_t len = res->strftime_to(buf, this->format_);
34 this->publish_state(buf, len);
35 }
36
37 void dump_config() override;
38
39 protected:
40 const char *format_{nullptr};
42 double elevation_;
44};
45
46} // namespace esphome::sun
This class simplifies creating components that periodically check a state.
Definition component.h:585
optional< ESPTime > sunset(double elevation)
Definition sun.cpp:318
optional< ESPTime > sunrise(double elevation)
Definition sun.cpp:317
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)
const char int const __FlashStringHelper * format
Definition log.h:74
const void size_t len
Definition hal.h:64
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:25