ESPHome 2026.12.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.
25StringRef SendspinTextSensor::extract_value_(const sendspin::TrackMetadata &metadata) const {
26 const auto text = [](const auto &field) {
27 return field.has_value() ? StringRef(field->data(), field->size()) : StringRef();
28 };
29 switch (this->metadata_type_) {
31 return text(metadata.title);
33 return text(metadata.artist);
35 return text(metadata.album);
37 return text(metadata.album_artist);
38 }
39 return StringRef();
40}
41
42// THREAD CONTEXT: Main loop. The registered metadata callback also fires on the main loop
43// (SendspinHub dispatches metadata from client_->loop()).
45 this->parent_->add_metadata_update_callback(
46 [this](const sendspin::TrackMetadata &metadata) { this->publish_if_changed_(this->extract_value_(metadata)); });
47}
48
49// Dedup to avoid frontend churn; TextSensor::publish_state already dedups the string assign but still notifies.
51 // The state starts empty, so a field that is already cleared when the first update arrives is suppressed here: the
52 // entity stays unknown rather than being dropped out of it for good by an empty publish. Later clears do publish the
53 // empty string and fire on_value with it.
54 if (this->get_raw_state() != value) {
55 this->publish_state(value.c_str(), value.size());
56 }
57}
58#endif // USE_SENDSPIN_METADATA
59
60void SendspinPairingCodeTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Pairing Code", this); }
61
62// THREAD CONTEXT: Main loop. The registered callbacks also fire on the main loop (SendspinHub dispatches them from
63// its loop()).
65 this->parent_->add_on_display_pairing_code_callback([this](const std::string &code) { this->publish_state(code); });
66 this->parent_->add_on_clear_pairing_code_callback([this]() { this->publish_state(""); });
67}
68
69} // namespace esphome::sendspin_
70
71#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
StringRef extract_value_(const sendspin::TrackMetadata &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:42
ESPHOME_LOG_TAG(TAG, "sendspin.image")
uint32_t value
Definition hal.cpp:59