mirror of
https://github.com/esphome/esphome.git
synced 2024-12-18 15:57:58 +01:00
Implement the media player actions (#3534)
This commit is contained in:
parent
03944e6cd8
commit
d63e14a4b6
@ -43,6 +43,14 @@ void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) {
|
||||
case media_player::MEDIA_PLAYER_COMMAND_UNMUTE:
|
||||
this->unmute_();
|
||||
break;
|
||||
case media_player::MEDIA_PLAYER_COMMAND_TOGGLE:
|
||||
this->audio_->pauseResume();
|
||||
if (this->audio_->isRunning()) {
|
||||
this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
|
||||
} else {
|
||||
this->state = media_player::MEDIA_PLAYER_STATE_PAUSED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
this->publish_state();
|
||||
|
@ -20,6 +20,9 @@ MediaPlayer = media_player_ns.class_("MediaPlayer")
|
||||
PlayAction = media_player_ns.class_(
|
||||
"PlayAction", automation.Action, cg.Parented.template(MediaPlayer)
|
||||
)
|
||||
ToggleAction = media_player_ns.class_(
|
||||
"ToggleAction", automation.Action, cg.Parented.template(MediaPlayer)
|
||||
)
|
||||
PauseAction = media_player_ns.class_(
|
||||
"PauseAction", automation.Action, cg.Parented.template(MediaPlayer)
|
||||
)
|
||||
@ -42,12 +45,15 @@ async def register_media_player(var, config):
|
||||
MEDIA_PLAYER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.Schema({}))
|
||||
|
||||
|
||||
MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id()(
|
||||
MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id(
|
||||
{cv.Required(CONF_ID): cv.use_id(MediaPlayer)}
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA)
|
||||
@automation.register_action(
|
||||
"media_player.toggle", ToggleAction, MEDIA_PLAYER_ACTION_SCHEMA
|
||||
)
|
||||
@automation.register_action(
|
||||
"media_player.pause", PauseAction, MEDIA_PLAYER_ACTION_SCHEMA
|
||||
)
|
||||
|
35
esphome/components/media_player/automation.h
Normal file
35
esphome/components/media_player/automation.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "media_player.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
namespace media_player {
|
||||
|
||||
template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<MediaPlayer> {
|
||||
void play(Ts... x) override {
|
||||
this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PLAY).perform();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class ToggleAction : public Action<Ts...>, public Parented<MediaPlayer> {
|
||||
void play(Ts... x) override {
|
||||
this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_TOGGLE).perform();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class PauseAction : public Action<Ts...>, public Parented<MediaPlayer> {
|
||||
void play(Ts... x) override {
|
||||
this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_PAUSE).perform();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<MediaPlayer> {
|
||||
void play(Ts... x) override {
|
||||
this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP).perform();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace media_player
|
||||
} // namespace esphome
|
@ -33,6 +33,8 @@ const char *media_player_command_to_string(MediaPlayerCommand command) {
|
||||
return "MUTE";
|
||||
case MEDIA_PLAYER_COMMAND_UNMUTE:
|
||||
return "UNMUTE";
|
||||
case MEDIA_PLAYER_COMMAND_TOGGLE:
|
||||
return "TOGGLE";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
@ -88,6 +90,8 @@ MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_MUTE);
|
||||
} else if (str_equals_case_insensitive(command, "UNMUTE")) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_UNMUTE);
|
||||
} else if (str_equals_case_insensitive(command, "TOGGLE")) {
|
||||
this->set_command(MEDIA_PLAYER_COMMAND_TOGGLE);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
|
||||
}
|
||||
|
@ -19,7 +19,8 @@ enum MediaPlayerCommand : uint8_t {
|
||||
MEDIA_PLAYER_COMMAND_PAUSE = 1,
|
||||
MEDIA_PLAYER_COMMAND_STOP = 2,
|
||||
MEDIA_PLAYER_COMMAND_MUTE = 3,
|
||||
MEDIA_PLAYER_COMMAND_UNMUTE = 4
|
||||
MEDIA_PLAYER_COMMAND_UNMUTE = 4,
|
||||
MEDIA_PLAYER_COMMAND_TOGGLE = 5
|
||||
};
|
||||
const char *media_player_command_to_string(MediaPlayerCommand command);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user