ESPHome
2024.10.2
esphome
esphome
components
media_player
media_player.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
esphome/core/entity_base.h
"
4
#include "
esphome/core/helpers.h
"
5
6
namespace
esphome
{
7
namespace
media_player {
8
9
enum
MediaPlayerState
: uint8_t {
10
MEDIA_PLAYER_STATE_NONE
= 0,
11
MEDIA_PLAYER_STATE_IDLE
= 1,
12
MEDIA_PLAYER_STATE_PLAYING
= 2,
13
MEDIA_PLAYER_STATE_PAUSED
= 3,
14
MEDIA_PLAYER_STATE_ANNOUNCING
= 4
15
};
16
const
char
*
media_player_state_to_string
(
MediaPlayerState
state
);
17
18
enum
MediaPlayerCommand
: uint8_t {
19
MEDIA_PLAYER_COMMAND_PLAY
= 0,
20
MEDIA_PLAYER_COMMAND_PAUSE
= 1,
21
MEDIA_PLAYER_COMMAND_STOP
= 2,
22
MEDIA_PLAYER_COMMAND_MUTE
= 3,
23
MEDIA_PLAYER_COMMAND_UNMUTE
= 4,
24
MEDIA_PLAYER_COMMAND_TOGGLE
= 5,
25
MEDIA_PLAYER_COMMAND_VOLUME_UP
= 6,
26
MEDIA_PLAYER_COMMAND_VOLUME_DOWN
= 7,
27
};
28
const
char
*
media_player_command_to_string
(
MediaPlayerCommand
command);
29
30
enum class
MediaPlayerFormatPurpose
: uint8_t {
31
PURPOSE_DEFAULT
= 0,
32
PURPOSE_ANNOUNCEMENT
= 1,
33
};
34
35
struct
MediaPlayerSupportedFormat
{
36
std::string
format
;
37
uint32_t
sample_rate
;
38
uint32_t
num_channels
;
39
MediaPlayerFormatPurpose
purpose
;
40
uint32_t
sample_bytes
;
41
};
42
43
class
MediaPlayer
;
44
45
class
MediaPlayerTraits
{
46
public
:
47
MediaPlayerTraits
() =
default
;
48
49
void
set_supports_pause
(
bool
supports_pause) { this->supports_pause_ = supports_pause; }
50
51
bool
get_supports_pause
()
const
{
return
this->supports_pause_; }
52
53
std::vector<MediaPlayerSupportedFormat> &
get_supported_formats
() {
return
this->supported_formats_; }
54
55
protected
:
56
bool
supports_pause_{
false
};
57
std::vector<MediaPlayerSupportedFormat> supported_formats_{};
58
};
59
60
class
MediaPlayerCall
{
61
public
:
62
MediaPlayerCall
(
MediaPlayer
*parent) : parent_(parent) {}
63
64
MediaPlayerCall
&set_command(
MediaPlayerCommand
command);
65
MediaPlayerCall
&set_command(
optional<MediaPlayerCommand>
command);
66
MediaPlayerCall
&set_command(
const
std::string &command);
67
68
MediaPlayerCall
&set_media_url(
const
std::string &url);
69
70
MediaPlayerCall
&set_volume(
float
volume);
71
MediaPlayerCall
&set_announcement(
bool
announce);
72
73
void
perform();
74
75
const
optional<MediaPlayerCommand>
&
get_command
()
const
{
return
command_; }
76
const
optional<std::string>
&
get_media_url
()
const
{
return
media_url_; }
77
const
optional<float>
&
get_volume
()
const
{
return
volume_; }
78
const
optional<bool>
&
get_announcement
()
const
{
return
announcement_; }
79
80
protected
:
81
void
validate_();
82
MediaPlayer
*
const
parent_
;
83
optional<MediaPlayerCommand>
command_
;
84
optional<std::string>
media_url_
;
85
optional<float>
volume_
;
86
optional<bool>
announcement_
;
87
};
88
89
class
MediaPlayer
:
public
EntityBase
{
90
public
:
91
MediaPlayerState
state
{
MEDIA_PLAYER_STATE_NONE
};
92
float
volume{1.0f};
93
94
MediaPlayerCall
make_call
() {
return
MediaPlayerCall
(
this
); }
95
96
void
publish_state();
97
98
void
add_on_state_callback(std::function<
void
()> &&callback);
99
100
virtual
bool
is_muted
()
const
{
return
false
; }
101
102
virtual
MediaPlayerTraits
get_traits() = 0;
103
104
protected
:
105
friend
MediaPlayerCall
;
106
107
virtual
void
control(
const
MediaPlayerCall
&call) = 0;
108
109
CallbackManager<void()>
state_callback_{};
110
};
111
112
}
// namespace media_player
113
}
// namespace esphome
esphome::media_player::MEDIA_PLAYER_COMMAND_STOP
Definition:
media_player.h:21
esphome::media_player::MediaPlayerSupportedFormat
Definition:
media_player.h:35
esphome::media_player::MEDIA_PLAYER_STATE_NONE
Definition:
media_player.h:10
esphome::media_player::MediaPlayerCall::volume_
optional< float > volume_
Definition:
media_player.h:85
esphome::media_player::MEDIA_PLAYER_COMMAND_PAUSE
Definition:
media_player.h:20
esphome::media_player::MEDIA_PLAYER_STATE_PLAYING
Definition:
media_player.h:12
esphome::media_player::MediaPlayerSupportedFormat::format
std::string format
Definition:
media_player.h:36
esphome::media_player::MediaPlayer::MediaPlayerCall
friend MediaPlayerCall
Definition:
media_player.h:105
esphome::media_player::MEDIA_PLAYER_COMMAND_TOGGLE
Definition:
media_player.h:24
esphome::media_player::MEDIA_PLAYER_COMMAND_MUTE
Definition:
media_player.h:22
entity_base.h
esphome::media_player::MediaPlayer::is_muted
virtual bool is_muted() const
Definition:
media_player.h:100
esphome::media_player::MediaPlayerCall::get_volume
const optional< float > & get_volume() const
Definition:
media_player.h:77
esphome::media_player::MediaPlayerFormatPurpose::PURPOSE_DEFAULT
esphome::media_player::media_player_state_to_string
const char * media_player_state_to_string(MediaPlayerState state)
Definition:
media_player.cpp:10
esphome::media_player::MediaPlayerCommand
MediaPlayerCommand
Definition:
media_player.h:18
esphome::media_player::MEDIA_PLAYER_STATE_PAUSED
Definition:
media_player.h:13
esphome::media_player::media_player_command_to_string
const char * media_player_command_to_string(MediaPlayerCommand command)
Definition:
media_player.cpp:26
esphome::media_player::MEDIA_PLAYER_COMMAND_UNMUTE
Definition:
media_player.h:23
esphome::EntityBase
Definition:
entity_base.h:16
esphome::media_player::MediaPlayer
Definition:
media_player.h:89
esphome::media_player::MEDIA_PLAYER_STATE_IDLE
Definition:
media_player.h:11
esphome::media_player::MediaPlayerTraits::get_supports_pause
bool get_supports_pause() const
Definition:
media_player.h:51
esphome::media_player::MediaPlayerCall::get_announcement
const optional< bool > & get_announcement() const
Definition:
media_player.h:78
esphome::media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN
Definition:
media_player.h:26
esphome::media_player::MediaPlayerFormatPurpose
MediaPlayerFormatPurpose
Definition:
media_player.h:30
esphome::media_player::MediaPlayerSupportedFormat::sample_bytes
uint32_t sample_bytes
Definition:
media_player.h:40
esphome::media_player::MediaPlayer::make_call
MediaPlayerCall make_call()
Definition:
media_player.h:94
esphome::media_player::MediaPlayerTraits::get_supported_formats
std::vector< MediaPlayerSupportedFormat > & get_supported_formats()
Definition:
media_player.h:53
esphome::media_player::MediaPlayerCall
Definition:
media_player.h:60
esphome::CallbackManager< void()>
esphome::media_player::MediaPlayerFormatPurpose::PURPOSE_ANNOUNCEMENT
esphome::media_player::MediaPlayerCall::get_command
const optional< MediaPlayerCommand > & get_command() const
Definition:
media_player.h:75
esphome::media_player::MediaPlayerCall::get_media_url
const optional< std::string > & get_media_url() const
Definition:
media_player.h:76
esphome::media_player::MediaPlayerCall::MediaPlayerCall
MediaPlayerCall(MediaPlayer *parent)
Definition:
media_player.h:62
esphome::optional< MediaPlayerCommand >
esphome::media_player::MEDIA_PLAYER_COMMAND_PLAY
Definition:
media_player.h:19
esphome::media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP
Definition:
media_player.h:25
esphome::media_player::MediaPlayerTraits
Definition:
media_player.h:45
esphome::media_player::MediaPlayerCall::parent_
MediaPlayer *const parent_
Definition:
media_player.h:82
esphome
Implementation of SPI Controller mode.
Definition:
a01nyub.cpp:7
esphome::media_player::MEDIA_PLAYER_STATE_ANNOUNCING
Definition:
media_player.h:14
esphome::media_player::MediaPlayerSupportedFormat::num_channels
uint32_t num_channels
Definition:
media_player.h:38
esphome::media_player::MediaPlayerSupportedFormat::sample_rate
uint32_t sample_rate
Definition:
media_player.h:37
helpers.h
esphome::media_player::MediaPlayerCall::announcement_
optional< bool > announcement_
Definition:
media_player.h:86
esphome::media_player::MediaPlayerTraits::set_supports_pause
void set_supports_pause(bool supports_pause)
Definition:
media_player.h:49
esphome::media_player::MediaPlayerSupportedFormat::purpose
MediaPlayerFormatPurpose purpose
Definition:
media_player.h:39
state
bool state
Definition:
fan.h:34
esphome::media_player::MediaPlayerCall::media_url_
optional< std::string > media_url_
Definition:
media_player.h:84
esphome::media_player::MediaPlayerCall::command_
optional< MediaPlayerCommand > command_
Definition:
media_player.h:83
esphome::media_player::MediaPlayerState
MediaPlayerState
Definition:
media_player.h:9
Generated by
1.8.13