ESPHome 2025.8.0b1
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
63
64enum class MediaPlayerFormatPurpose : uint8_t {
67};
68
76
77class MediaPlayer;
78
80 public:
81 MediaPlayerTraits() = default;
82
83 void set_supports_pause(bool supports_pause) { this->supports_pause_ = supports_pause; }
84 bool get_supports_pause() const { return this->supports_pause_; }
85
86 void set_supports_turn_off_on(bool supports_turn_off_on) { this->supports_turn_off_on_ = supports_turn_off_on; }
87 bool get_supports_turn_off_on() const { return this->supports_turn_off_on_; }
88
89 std::vector<MediaPlayerSupportedFormat> &get_supported_formats() { return this->supported_formats_; }
90
104
105 protected:
106 std::vector<MediaPlayerSupportedFormat> supported_formats_{};
107 bool supports_pause_{false};
109};
110
112 public:
113 MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
114
117 MediaPlayerCall &set_command(const std::string &command);
118
119 MediaPlayerCall &set_media_url(const std::string &url);
120
121 MediaPlayerCall &set_volume(float volume);
122 MediaPlayerCall &set_announcement(bool announce);
123
124 void perform();
125
126 const optional<MediaPlayerCommand> &get_command() const { return this->command_; }
127 const optional<std::string> &get_media_url() const { return this->media_url_; }
128 const optional<float> &get_volume() const { return this->volume_; }
129 const optional<bool> &get_announcement() const { return this->announcement_; }
130
131 protected:
132 void validate_();
138};
139
140class MediaPlayer : public EntityBase {
141 public:
143 float volume{1.0f};
144
146
147 void publish_state();
148
149 void add_on_state_callback(std::function<void()> &&callback);
150
151 virtual bool is_muted() const { return false; }
152
154
155 protected:
157
158 virtual void control(const MediaPlayerCall &call) = 0;
159
161};
162
163} // namespace media_player
164} // 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
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
CallbackManager< 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)
std::vector< MediaPlayerSupportedFormat > & get_supported_formats()
void set_supports_pause(bool supports_pause)
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