ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
media_player.cpp
Go to the documentation of this file.
1#include "media_player.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace media_player {
8
9static const char *const TAG = "media_player";
10
12 switch (state) {
14 return "ON";
16 return "OFF";
18 return "IDLE";
20 return "PLAYING";
22 return "PAUSED";
24 return "ANNOUNCING";
26 return "NONE";
27 default:
28 return "UNKNOWN";
29 }
30}
31
33 switch (command) {
35 return "PLAY";
37 return "PAUSE";
39 return "STOP";
41 return "MUTE";
43 return "UNMUTE";
45 return "TOGGLE";
47 return "VOLUME_UP";
49 return "VOLUME_DOWN";
51 return "ENQUEUE";
53 return "REPEAT_ONE";
55 return "REPEAT_OFF";
57 return "CLEAR_PLAYLIST";
59 return "TURN_ON";
61 return "TURN_OFF";
62 default:
63 return "UNKNOWN";
64 }
65}
66
68 if (this->media_url_.has_value()) {
69 if (this->command_.has_value() && this->command_.value() != MEDIA_PLAYER_COMMAND_ENQUEUE) {
70 // Don't remove an enqueue command
71 ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
72 this->command_.reset();
73 }
74 }
75 if (this->volume_.has_value()) {
76 if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
77 ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
78 this->volume_.reset();
79 }
80 }
81}
82
84 ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
85 this->validate_();
86 if (this->command_.has_value()) {
87 const char *command_s = media_player_command_to_string(this->command_.value());
88 ESP_LOGD(TAG, " Command: %s", command_s);
89 }
90 if (this->media_url_.has_value()) {
91 ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
92 }
93 if (this->volume_.has_value()) {
94 ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
95 }
96 if (this->announcement_.has_value()) {
97 ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
98 }
99 this->parent_->control(*this);
100}
101
103 this->command_ = command;
104 return *this;
105}
107 this->command_ = command;
108 return *this;
109}
110MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
111 if (str_equals_case_insensitive(command, "PLAY")) {
113 } else if (str_equals_case_insensitive(command, "PAUSE")) {
115 } else if (str_equals_case_insensitive(command, "STOP")) {
117 } else if (str_equals_case_insensitive(command, "MUTE")) {
119 } else if (str_equals_case_insensitive(command, "UNMUTE")) {
121 } else if (str_equals_case_insensitive(command, "TOGGLE")) {
123 } else if (str_equals_case_insensitive(command, "TURN_ON")) {
125 } else if (str_equals_case_insensitive(command, "TURN_OFF")) {
127 } else {
128 ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
129 }
130 return *this;
131}
132
133MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
134 this->media_url_ = media_url;
135 return *this;
136}
137
139 this->volume_ = volume;
140 return *this;
141}
142
144 this->announcement_ = announce;
145 return *this;
146}
147
148void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
149 this->state_callback_.add(std::move(callback));
150}
151
153 this->state_callback_.call();
154#if defined(USE_MEDIA_PLAYER) && defined(USE_CONTROLLER_REGISTRY)
156#endif
157}
158
159} // namespace media_player
160} // namespace esphome
static void notify_media_player_update(media_player::MediaPlayer *obj)
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
MediaPlayerCall & set_media_url(const std::string &url)
MediaPlayerCall & set_volume(float volume)
MediaPlayerCall & set_announcement(bool announce)
MediaPlayerCall & set_command(MediaPlayerCommand command)
optional< MediaPlayerCommand > command_
virtual void control(const MediaPlayerCall &call)=0
CallbackManager< void()> state_callback_
void add_on_state_callback(std::function< void()> &&callback)
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
bool state
Definition fan.h:0
const char * media_player_command_to_string(MediaPlayerCommand command)
const char * media_player_state_to_string(MediaPlayerState state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:161