ESPHome  2025.2.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";
45  return "ENQUEUE";
47  return "REPEAT_ONE";
49  return "REPEAT_OFF";
51  return "CLEAR_PLAYLIST";
52  default:
53  return "UNKNOWN";
54  }
55 }
56 
58  if (this->media_url_.has_value()) {
59  if (this->command_.has_value()) {
60  ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
61  this->command_.reset();
62  }
63  }
64  if (this->volume_.has_value()) {
65  if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
66  ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
67  this->volume_.reset();
68  }
69  }
70 }
71 
73  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
74  this->validate_();
75  if (this->command_.has_value()) {
76  const char *command_s = media_player_command_to_string(this->command_.value());
77  ESP_LOGD(TAG, " Command: %s", command_s);
78  }
79  if (this->media_url_.has_value()) {
80  ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
81  }
82  if (this->volume_.has_value()) {
83  ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
84  }
85  if (this->announcement_.has_value()) {
86  ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
87  }
88  this->parent_->control(*this);
89 }
90 
92  this->command_ = command;
93  return *this;
94 }
96  this->command_ = command;
97  return *this;
98 }
99 MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
100  if (str_equals_case_insensitive(command, "PLAY")) {
102  } else if (str_equals_case_insensitive(command, "PAUSE")) {
104  } else if (str_equals_case_insensitive(command, "STOP")) {
106  } else if (str_equals_case_insensitive(command, "MUTE")) {
108  } else if (str_equals_case_insensitive(command, "UNMUTE")) {
110  } else if (str_equals_case_insensitive(command, "TOGGLE")) {
112  } else {
113  ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
114  }
115  return *this;
116 }
117 
118 MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
119  this->media_url_ = media_url;
120  return *this;
121 }
122 
124  this->volume_ = volume;
125  return *this;
126 }
127 
129  this->announcement_ = announce;
130  return *this;
131 }
132 
133 void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
134  this->state_callback_.add(std::move(callback));
135 }
136 
137 void MediaPlayer::publish_state() { this->state_callback_.call(); }
138 
139 } // namespace media_player
140 } // 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:263
optional< std::string > media_url_
Definition: media_player.h:88
optional< MediaPlayerCommand > command_
Definition: media_player.h:87