ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
sendspin_text_sensor.cpp
Go to the documentation of this file.
2
3#if defined(USE_ESP_IDF) && defined(USE_TEXT_SENSOR)
4
5#ifdef USE_SENDSPIN_METADATA
6#include <sendspin/metadata_role.h>
7#endif
8
9#include <string>
10
11namespace esphome::sendspin_ {
12
13ESPHOME_LOG_TAG(TAG, "sendspin.text_sensor");
14
15#ifdef USE_SENDSPIN_METADATA
16void SendspinTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Sendspin", this); }
17
18// A field is nullopt when the server has not provided it or has explicitly cleared it. Both mean there is nothing to
19// show, so return the empty string and let the caller publish it; returning early would leave the previous track's
20// value on display.
21//
22// The empty string is not the same as unknown. A text sensor reports unknown through the API's missing_state flag,
23// which follows has_state(), and has_state() is only ever set, never cleared. Once a real value has been published,
24// an empty state is the closest we can get. The numeric sensors publish NAN, which does read as unknown.
25const char *SendspinTextSensor::extract_value_(const sendspin::ServerMetadataStateObject &metadata) const {
26 switch (this->metadata_type_) {
28 return metadata.title.has_value() ? metadata.title.value().c_str() : "";
30 return metadata.artist.has_value() ? metadata.artist.value().c_str() : "";
32 return metadata.album.has_value() ? metadata.album.value().c_str() : "";
34 return metadata.album_artist.has_value() ? metadata.album_artist.value().c_str() : "";
35 }
36 return "";
37}
38
39// THREAD CONTEXT: Main loop. The registered metadata callback also fires on the main loop
40// (SendspinHub dispatches metadata from client_->loop()).
42 this->parent_->add_metadata_update_callback([this](const sendspin::ServerMetadataStateObject &metadata) {
43 this->publish_if_changed_(this->extract_value_(metadata));
44 });
45}
46
47// Dedup to avoid frontend churn; TextSensor::publish_state already dedups the string assign but still notifies.
49 // The state starts empty, so a field that is already cleared when the first update arrives is suppressed here: the
50 // entity stays unknown rather than being dropped out of it for good by an empty publish. Later clears do publish the
51 // empty string and fire on_value with it.
52 if (this->get_raw_state() != value) {
53 this->publish_state(value);
54 }
55}
56#endif // USE_SENDSPIN_METADATA
57
58void SendspinPairingCodeTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Pairing Code", this); }
59
60// THREAD CONTEXT: Main loop. The registered callbacks also fire on the main loop (SendspinHub dispatches them from
61// its loop()).
63 this->parent_->add_on_display_pairing_code_callback([this](const std::string &code) { this->publish_state(code); });
64 this->parent_->add_on_clear_pairing_code_callback([this]() { this->publish_state(""); });
65}
66
67} // namespace esphome::sendspin_
68
69#endif
const char * extract_value_(const sendspin::ServerMetadataStateObject &metadata) const
const std::string & get_raw_state() const
Returns the raw (pre-filter) state.
void publish_state(const std::string &state)
Definition text_sensor.h:41
ESPHOME_LOG_TAG(TAG, "sendspin.image")