ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
sendspin_text_sensor.cpp
Go to the documentation of this file.
2
3#if defined(USE_ESP32) && defined(USE_SENDSPIN_METADATA) && defined(USE_TEXT_SENSOR)
4
5#include <sendspin/metadata_role.h>
6
7#include <string>
8
9namespace esphome::sendspin_ {
10
11static const char *const TAG = "sendspin.text_sensor";
12
13void SendspinTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Sendspin", this); }
14
15const char *SendspinTextSensor::extract_value_(const sendspin::ServerMetadataStateObject &metadata) const {
16 switch (this->metadata_type_) {
18 if (metadata.title.has_value())
19 return metadata.title.value().c_str();
20 return nullptr;
22 if (metadata.artist.has_value())
23 return metadata.artist.value().c_str();
24 return nullptr;
26 if (metadata.album.has_value())
27 return metadata.album.value().c_str();
28 return nullptr;
30 if (metadata.album_artist.has_value())
31 return metadata.album_artist.value().c_str();
32 return nullptr;
33 }
34 return nullptr;
35}
36
37// THREAD CONTEXT: Main loop. The registered metadata callback also fires on the main loop
38// (SendspinHub dispatches metadata from client_->loop()).
40 this->parent_->add_metadata_update_callback([this](const sendspin::ServerMetadataStateObject &metadata) {
41 if (const char *value = this->extract_value_(metadata)) {
42 this->publish_if_changed_(value);
43 }
44 });
45}
46
47// Dedup to avoid frontend churn; TextSensor::publish_state already dedups the string assign but still notifies.
49 if (this->get_raw_state() != value) {
50 this->publish_state(value);
51 }
52}
53
54} // namespace esphome::sendspin_
55
56#endif
const char * extract_value_(const sendspin::ServerMetadataStateObject &metadata) const
const std::string & get_raw_state() const
Getter-syntax for .raw_state.
void publish_state(const std::string &state)