ESPHome  2024.11.0
media_player.cpp
Go to the documentation of this file.
1 #include "media_player.h"
2 
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace media_player {
7 
8 static const char *const TAG = "media_player";
9 
11  switch (state) {
13  return "IDLE";
15  return "PLAYING";
17  return "PAUSED";
19  return "ANNOUNCING";
21  default:
22  return "UNKNOWN";
23  }
24 }
25 
27  switch (command) {
29  return "PLAY";
31  return "PAUSE";
33  return "STOP";
35  return "MUTE";
37  return "UNMUTE";
39  return "TOGGLE";
41  return "VOLUME_UP";
43  return "VOLUME_DOWN";
44  default:
45  return "UNKNOWN";
46  }
47 }
48 
50  if (this->media_url_.has_value()) {
51  if (this->command_.has_value()) {
52  ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
53  this->command_.reset();
54  }
55  }
56  if (this->volume_.has_value()) {
57  if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
58  ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
59  this->volume_.reset();
60  }
61  }
62 }
63 
65  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
66  this->validate_();
67  if (this->command_.has_value()) {
68  const char *command_s = media_player_command_to_string(this->command_.value());
69  ESP_LOGD(TAG, " Command: %s", command_s);
70  }
71  if (this->media_url_.has_value()) {
72  ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
73  }
74  if (this->volume_.has_value()) {
75  ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
76  }
77  if (this->announcement_.has_value()) {
78  ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
79  }
80  this->parent_->control(*this);
81 }
82 
84  this->command_ = command;
85  return *this;
86 }
88  this->command_ = command;
89  return *this;
90 }
91 MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
92  if (str_equals_case_insensitive(command, "PLAY")) {
94  } else if (str_equals_case_insensitive(command, "PAUSE")) {
96  } else if (str_equals_case_insensitive(command, "STOP")) {
98  } else if (str_equals_case_insensitive(command, "MUTE")) {
100  } else if (str_equals_case_insensitive(command, "UNMUTE")) {
102  } else if (str_equals_case_insensitive(command, "TOGGLE")) {
104  } else {
105  ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
106  }
107  return *this;
108 }
109 
110 MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
111  this->media_url_ = media_url;
112  return *this;
113 }
114 
116  this->volume_ = volume;
117  return *this;
118 }
119 
121  this->announcement_ = announce;
122  return *this;
123 }
124 
125 void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
126  this->state_callback_.add(std::move(callback));
127 }
128 
129 void MediaPlayer::publish_state() { this->state_callback_.call(); }
130 
131 } // namespace media_player
132 } // namespace esphome
value_type const & value() const
Definition: optional.h:89
MediaPlayerCall & set_command(MediaPlayerCommand command)
bool has_value() const
Definition: optional.h:87
const char * media_player_state_to_string(MediaPlayerState state)
const char * media_player_command_to_string(MediaPlayerCommand command)
void add_on_state_callback(std::function< void()> &&callback)
constexpr const char * c_str() const
Definition: string_ref.h:68
MediaPlayerCall & set_announcement(bool announce)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual void control(const MediaPlayerCall &call)=0
MediaPlayerCall & set_media_url(const std::string &url)
const StringRef & get_name() const
Definition: entity_base.cpp:10
MediaPlayerCall & set_volume(float volume)
bool state
Definition: fan.h:34
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition: helpers.cpp:259
optional< std::string > media_url_
Definition: media_player.h:84
optional< MediaPlayerCommand > command_
Definition: media_player.h:83