From b0c0258e70c5aaef74aa84cbb987d3ef4d899e7b Mon Sep 17 00:00:00 2001 From: Sergey Dudanov Date: Mon, 20 Jun 2022 04:17:20 +0400 Subject: [PATCH] Media Player: added play_media action (#3579) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/media_player/__init__.py | 31 ++++++++++++++++++++ esphome/components/media_player/automation.h | 19 ++++++++++-- tests/test4.yaml | 6 ++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/esphome/components/media_player/__init__.py b/esphome/components/media_player/__init__.py index 3456417370..80f5fc558a 100644 --- a/esphome/components/media_player/__init__.py +++ b/esphome/components/media_player/__init__.py @@ -20,6 +20,9 @@ MediaPlayer = media_player_ns.class_("MediaPlayer") PlayAction = media_player_ns.class_( "PlayAction", automation.Action, cg.Parented.template(MediaPlayer) ) +PlayMediaAction = media_player_ns.class_( + "PlayMediaAction", automation.Action, cg.Parented.template(MediaPlayer) +) ToggleAction = media_player_ns.class_( "ToggleAction", automation.Action, cg.Parented.template(MediaPlayer) ) @@ -44,11 +47,14 @@ CONF_VOLUME = "volume" CONF_ON_IDLE = "on_idle" CONF_ON_PLAY = "on_play" CONF_ON_PAUSE = "on_pause" +CONF_MEDIA_URL = "media_url" StateTrigger = media_player_ns.class_("StateTrigger", automation.Trigger.template()) IdleTrigger = media_player_ns.class_("IdleTrigger", automation.Trigger.template()) PlayTrigger = media_player_ns.class_("PlayTrigger", automation.Trigger.template()) PauseTrigger = media_player_ns.class_("PauseTrigger", automation.Trigger.template()) +IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition) +IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition) async def setup_media_player_core_(var, config): @@ -103,6 +109,25 @@ MEDIA_PLAYER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend( MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(MediaPlayer)}) +@automation.register_action( + "media_player.play_media", + PlayMediaAction, + cv.maybe_simple_value( + { + cv.GenerateID(): cv.use_id(MediaPlayer), + cv.Required(CONF_MEDIA_URL): cv.templatable(cv.url), + }, + key=CONF_MEDIA_URL, + ), +) +async def media_player_play_media_action(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + media_url = await cg.templatable(config[CONF_MEDIA_URL], args, cg.std_string) + cg.add(var.set_media_url(media_url)) + return var + + @automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA) @automation.register_action( "media_player.toggle", ToggleAction, MEDIA_PLAYER_ACTION_SCHEMA @@ -117,6 +142,12 @@ MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(MediaPl @automation.register_action( "media_player.volume_down", VolumeDownAction, MEDIA_PLAYER_ACTION_SCHEMA ) +@automation.register_condition( + "media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA +) +@automation.register_condition( + "media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA +) async def media_player_action(config, action_id, template_arg, args): var = cg.new_Pvariable(action_id, template_arg) await cg.register_parented(var, config[CONF_ID]) diff --git a/esphome/components/media_player/automation.h b/esphome/components/media_player/automation.h index c6deba7dc6..261e93775c 100644 --- a/esphome/components/media_player/automation.h +++ b/esphome/components/media_player/automation.h @@ -17,7 +17,7 @@ namespace media_player { #define MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(TRIGGER_CLASS, TRIGGER_STATE) \ class TRIGGER_CLASS : public Trigger<> { \ public: \ - TRIGGER_CLASS(MediaPlayer *player) { \ + explicit TRIGGER_CLASS(MediaPlayer *player) { \ player->add_on_state_callback([this, player]() { \ if (player->state == MediaPlayerState::MEDIA_PLAYER_STATE_##TRIGGER_STATE) \ this->trigger(); \ @@ -32,6 +32,11 @@ MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ToggleAction, TOGGLE) MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeUpAction, VOLUME_UP) MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeDownAction, VOLUME_DOWN) +template class PlayMediaAction : public Action, public Parented { + TEMPLATABLE_VALUE(std::string, media_url) + void play(Ts... x) override { this->parent_->make_call().set_media_url(this->media_url_.value(x...)).perform(); } +}; + template class VolumeSetAction : public Action, public Parented { TEMPLATABLE_VALUE(float, volume) void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); } @@ -39,7 +44,7 @@ template class VolumeSetAction : public Action, public Pa class StateTrigger : public Trigger<> { public: - StateTrigger(MediaPlayer *player) { + explicit StateTrigger(MediaPlayer *player) { player->add_on_state_callback([this]() { this->trigger(); }); } }; @@ -48,5 +53,15 @@ MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(IdleTrigger, IDLE) MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PlayTrigger, PLAYING) MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PauseTrigger, PAUSED) +template class IsIdleCondition : public Condition, public Parented { + public: + bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; } +}; + +template class IsPlayingCondition : public Condition, public Parented { + public: + bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; } +}; + } // namespace media_player } // namespace esphome diff --git a/tests/test4.yaml b/tests/test4.yaml index c82673b196..0dfbeed550 100644 --- a/tests/test4.yaml +++ b/tests/test4.yaml @@ -615,12 +615,18 @@ media_player: mute_pin: GPIO14 on_state: - media_player.play: + - media_player.play_media: http://localhost/media.mp3 + - media_player.play_media: !lambda 'return "http://localhost/media.mp3";' on_idle: - media_player.pause: on_play: - media_player.stop: on_pause: - media_player.toggle: + - wait_until: + media_player.is_idle: + - wait_until: + media_player.is_playing: - media_player.volume_up: - media_player.volume_down: - media_player.volume_set: 50%