From 2b9350ce7695317abf9022ebca95afc18f937803 Mon Sep 17 00:00:00 2001 From: foxsam21 Date: Wed, 2 Jun 2021 18:12:23 -0400 Subject: [PATCH] Added vol +/- control to dfplayer (#1856) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/dfplayer/__init__.py | 32 +++++++++++++++++++++++++ esphome/components/dfplayer/dfplayer.h | 2 ++ tests/test3.yaml | 8 +++++++ 3 files changed, 42 insertions(+) diff --git a/esphome/components/dfplayer/__init__.py b/esphome/components/dfplayer/__init__.py index 4c276db63f..6af83888ab 100644 --- a/esphome/components/dfplayer/__init__.py +++ b/esphome/components/dfplayer/__init__.py @@ -43,6 +43,8 @@ PreviousAction = dfplayer_ns.class_("PreviousAction", automation.Action) PlayFileAction = dfplayer_ns.class_("PlayFileAction", automation.Action) PlayFolderAction = dfplayer_ns.class_("PlayFolderAction", automation.Action) SetVolumeAction = dfplayer_ns.class_("SetVolumeAction", automation.Action) +VolumeUpAction = dfplayer_ns.class_("VolumeUpAction", automation.Action) +VolumeDownAction = dfplayer_ns.class_("VolumeDownAction", automation.Action) SetEqAction = dfplayer_ns.class_("SetEqAction", automation.Action) SleepAction = dfplayer_ns.class_("SleepAction", automation.Action) ResetAction = dfplayer_ns.class_("ResetAction", automation.Action) @@ -201,6 +203,36 @@ async def dfplayer_set_volume_to_code(config, action_id, template_arg, args): return var +@automation.register_action( + "dfplayer.volume_up", + VolumeUpAction, + cv.Schema( + { + cv.GenerateID(): cv.use_id(DFPlayer), + } + ), +) +async def dfplayer_volume_up_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var + + +@automation.register_action( + "dfplayer.volume_down", + VolumeDownAction, + cv.Schema( + { + cv.GenerateID(): cv.use_id(DFPlayer), + } + ), +) +async def dfplayer_volume_down_to_code(config, action_id, template_arg, args): + var = cg.new_Pvariable(action_id, template_arg) + await cg.register_parented(var, config[CONF_ID]) + return var + + @automation.register_action( "dfplayer.set_eq", SetEqAction, diff --git a/esphome/components/dfplayer/dfplayer.h b/esphome/components/dfplayer/dfplayer.h index cb9686bb64..6742591926 100644 --- a/esphome/components/dfplayer/dfplayer.h +++ b/esphome/components/dfplayer/dfplayer.h @@ -180,6 +180,8 @@ DFPLAYER_SIMPLE_ACTION(StartAction, start) DFPLAYER_SIMPLE_ACTION(PauseAction, pause) DFPLAYER_SIMPLE_ACTION(StopAction, stop) DFPLAYER_SIMPLE_ACTION(RandomAction, random) +DFPLAYER_SIMPLE_ACTION(VolumeUpAction, volume_up) +DFPLAYER_SIMPLE_ACTION(VolumeDownAction, volume_down) template class DFPlayerIsPlayingCondition : public Condition, public Parented { public: diff --git a/tests/test3.yaml b/tests/test3.yaml index dcffdb8504..8f6b33bd5d 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -141,6 +141,14 @@ api: then: - dfplayer.random + - service: dfplayer_volume_up + then: + - dfplayer.volume_up + + - service: dfplayer_volume_down + then: + - dfplayer.volume_down + - service: battery_level_percent variables: level_percent: int