ESPHome  2024.5.5
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";
40  default:
41  return "UNKNOWN";
42  }
43 }
44 
46  if (this->media_url_.has_value()) {
47  if (this->command_.has_value()) {
48  ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
49  this->command_.reset();
50  }
51  }
52  if (this->volume_.has_value()) {
53  if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
54  ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
55  this->volume_.reset();
56  }
57  }
58 }
59 
61  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
62  this->validate_();
63  if (this->command_.has_value()) {
64  const char *command_s = media_player_command_to_string(this->command_.value());
65  ESP_LOGD(TAG, " Command: %s", command_s);
66  }
67  if (this->media_url_.has_value()) {
68  ESP_LOGD(TAG, " Media URL: %s", this->media_url_.value().c_str());
69  }
70  if (this->volume_.has_value()) {
71  ESP_LOGD(TAG, " Volume: %.2f", this->volume_.value());
72  }
73  if (this->announcement_.has_value()) {
74  ESP_LOGD(TAG, " Announcement: %s", this->announcement_.value() ? "yes" : "no");
75  }
76  this->parent_->control(*this);
77 }
78 
80  this->command_ = command;
81  return *this;
82 }
84  this->command_ = command;
85  return *this;
86 }
87 MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
88  if (str_equals_case_insensitive(command, "PLAY")) {
90  } else if (str_equals_case_insensitive(command, "PAUSE")) {
92  } else if (str_equals_case_insensitive(command, "STOP")) {
94  } else if (str_equals_case_insensitive(command, "MUTE")) {
96  } else if (str_equals_case_insensitive(command, "UNMUTE")) {
98  } else if (str_equals_case_insensitive(command, "TOGGLE")) {
100  } else {
101  ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
102  }
103  return *this;
104 }
105 
106 MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
107  this->media_url_ = media_url;
108  return *this;
109 }
110 
112  this->volume_ = volume;
113  return *this;
114 }
115 
117  this->announcement_ = announce;
118  return *this;
119 }
120 
121 void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
122  this->state_callback_.add(std::move(callback));
123 }
124 
125 void MediaPlayer::publish_state() { this->state_callback_.call(); }
126 
127 } // namespace media_player
128 } // 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)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:256
optional< std::string > media_url_
Definition: media_player.h:68
optional< MediaPlayerCommand > command_
Definition: media_player.h:67