ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
media_player.h
Go to the documentation of this file.
1#pragma once
2
5
6namespace esphome {
7namespace media_player {
8
9enum MediaPlayerEntityFeature : uint32_t {
10 PAUSE = 1 << 0,
11 SEEK = 1 << 1,
12 VOLUME_SET = 1 << 2,
13 VOLUME_MUTE = 1 << 3,
15 NEXT_TRACK = 1 << 5,
16
17 TURN_ON = 1 << 7,
18 TURN_OFF = 1 << 8,
19 PLAY_MEDIA = 1 << 9,
20 VOLUME_STEP = 1 << 10,
21 SELECT_SOURCE = 1 << 11,
22 STOP = 1 << 12,
23 CLEAR_PLAYLIST = 1 << 13,
24 PLAY = 1 << 14,
25 SHUFFLE_SET = 1 << 15,
27 BROWSE_MEDIA = 1 << 17,
28 REPEAT_SET = 1 << 18,
29 GROUPING = 1 << 19,
30 MEDIA_ANNOUNCE = 1 << 20,
31 MEDIA_ENQUEUE = 1 << 21,
32 SEARCH_MEDIA = 1 << 22,
33};
34
45
69
70enum class MediaPlayerFormatPurpose : uint8_t {
73};
74
82
83// Base features always reported for all media players
84static constexpr uint32_t BASE_MEDIA_PLAYER_FEATURES =
88
89class MediaPlayer;
90
92 public:
93 MediaPlayerTraits() = default;
94
95 uint32_t get_feature_flags() const { return this->feature_flags_; }
96 void add_feature_flags(uint32_t feature_flags) { this->feature_flags_ |= feature_flags; }
97 void clear_feature_flags(uint32_t feature_flags) { this->feature_flags_ &= ~feature_flags; }
98 // Returns true only if all specified flags are set
99 bool has_feature_flags(uint32_t feature_flags) const {
100 return (this->feature_flags_ & feature_flags) == feature_flags;
101 }
102
103 std::vector<MediaPlayerSupportedFormat> &get_supported_formats() { return this->supported_formats_; }
104
105 // Legacy setters/getters are kept for backward compatibility
106 void set_supports_pause(bool supports_pause);
108
109 void set_supports_turn_off_on(bool supports_turn_off_on);
113
114 protected:
115 std::vector<MediaPlayerSupportedFormat> supported_formats_{};
116 uint32_t feature_flags_{BASE_MEDIA_PLAYER_FEATURES};
117};
118
120 public:
121 MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
122
125 MediaPlayerCall &set_command(const char *command);
126 MediaPlayerCall &set_command(const std::string &command) { return this->set_command(command.c_str()); }
127
128 MediaPlayerCall &set_media_url(const std::string &url);
129
130 MediaPlayerCall &set_volume(float volume);
131 MediaPlayerCall &set_announcement(bool announce);
132
133 void perform();
134
135 const optional<MediaPlayerCommand> &get_command() const { return this->command_; }
136 const optional<std::string> &get_media_url() const { return this->media_url_; }
137 const optional<float> &get_volume() const { return this->volume_; }
138 const optional<bool> &get_announcement() const { return this->announcement_; }
139
140 protected:
141 void validate_();
147};
148
149class MediaPlayer : public EntityBase {
150 public:
152 float volume{1.0f};
153
155
156 void publish_state();
157
158 void add_on_state_callback(std::function<void()> &&callback);
159
160 virtual bool is_muted() const { return false; }
161
163
164 protected:
166
167 virtual void control(const MediaPlayerCall &call) = 0;
168
170};
171
172} // namespace media_player
173} // namespace esphome
const optional< bool > & get_announcement() const
MediaPlayerCall & set_media_url(const std::string &url)
MediaPlayerCall & set_volume(float volume)
const optional< std::string > & get_media_url() const
const optional< MediaPlayerCommand > & get_command() const
MediaPlayerCall & set_command(const std::string &command)
const optional< float > & get_volume() const
MediaPlayerCall & set_announcement(bool announce)
MediaPlayerCall & set_command(MediaPlayerCommand command)
optional< MediaPlayerCommand > command_
virtual void control(const MediaPlayerCall &call)=0
LazyCallbackManager< void()> state_callback_
virtual MediaPlayerTraits get_traits()=0
void add_on_state_callback(std::function< void()> &&callback)
std::vector< MediaPlayerSupportedFormat > supported_formats_
void set_supports_turn_off_on(bool supports_turn_off_on)
void clear_feature_flags(uint32_t feature_flags)
bool has_feature_flags(uint32_t feature_flags) const
std::vector< MediaPlayerSupportedFormat > & get_supported_formats()
void set_supports_pause(bool supports_pause)
void add_feature_flags(uint32_t feature_flags)
bool state
Definition fan.h:2
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