From e1ec6f4baa4e5d9b929b620addcaf2a7fe268c8e Mon Sep 17 00:00:00 2001
From: Przemek Grondek <przemek@grondek.pl>
Date: Mon, 7 Feb 2022 20:02:42 +0100
Subject: [PATCH] Deduplicate bathroom music automations sequence

---
 .../automations/bathroom-mirror-button.yaml   |  5 +-
 .../automations/bathroom-music-button.yaml    |  3 +-
 configuration/automations/bathroom-music.yaml | 25 ---------
 scripts/40.pause-all-media.yaml               | 53 +++++++++++++++++++
 4 files changed, 55 insertions(+), 31 deletions(-)

diff --git a/configuration/automations/bathroom-mirror-button.yaml b/configuration/automations/bathroom-mirror-button.yaml
index 7e8ba30..df59964 100644
--- a/configuration/automations/bathroom-mirror-button.yaml
+++ b/configuration/automations/bathroom-mirror-button.yaml
@@ -7,9 +7,7 @@
       type: remote_button_alt_short_press
       subtype: button_2
   action:
-    - service: media_player.media_play_pause
-      target:
-        entity_id: media_player.bathroom
+    - service: script.bathroom_music_toggle
   mode: single
 
 - id: bathroom-mirror-lights-off
@@ -47,7 +45,6 @@
               target:
                 entity_id: vacuum.robot_vacuum
 
-
 - id: bathroom-mirror-button-4
   alias: '[Bathroom] Mirror Button Automations'
   trigger:
diff --git a/configuration/automations/bathroom-music-button.yaml b/configuration/automations/bathroom-music-button.yaml
index 2b92d6f..18de44f 100644
--- a/configuration/automations/bathroom-music-button.yaml
+++ b/configuration/automations/bathroom-music-button.yaml
@@ -56,5 +56,4 @@
       type: remote_button_short_press
       subtype: remote_button_short_press
   action:
-    - service: media_player.media_play_pause
-      entity_id: media_player.bathroom
\ No newline at end of file
+    - service: script.bathroom_music_toggle
\ No newline at end of file
diff --git a/configuration/automations/bathroom-music.yaml b/configuration/automations/bathroom-music.yaml
index af22768..6f1d5e7 100644
--- a/configuration/automations/bathroom-music.yaml
+++ b/configuration/automations/bathroom-music.yaml
@@ -22,28 +22,3 @@
       data:
         media_content_id: https://open.spotify.com/playlist/71mXIu6HcdVj3fAMduaHop
         media_content_type: music
-
-- id: bathroom-music-off
-  alias: '[Bathroom] Music Off'
-  mode: restart
-  trigger:
-    - platform: state
-      entity_id: binary_sensor.shower_motion
-      for: '0:01:00'
-      from: 'on'
-      to: 'off'
-    - platform: state
-      entity_id: binary_sensor.bathroom_motion_occupancy
-      for: '0:01:00'
-      from: 'on'
-      to: 'off'
-  condition:
-    - condition: state
-      entity_id: binary_sensor.shower_motion
-      state: 'off'
-    - condition: state
-      entity_id: binary_sensor.bathroom_motion_occupancy
-      state: 'off'
-  action:
-    - service: media_player.media_pause
-      entity_id: media_player.bathroom
diff --git a/scripts/40.pause-all-media.yaml b/scripts/40.pause-all-media.yaml
index 4a296f2..6c893e4 100644
--- a/scripts/40.pause-all-media.yaml
+++ b/scripts/40.pause-all-media.yaml
@@ -1,3 +1,56 @@
+bathroom_music_toggle:
+  alias: '[Bathroom] Music toggle'
+  sequence:
+    - choose:
+        - conditions:
+            - condition: state
+              entity_id: media_player.bathroom
+              state: 'playing'
+          sequence:
+            - service: script.bathroom_music_off
+        - conditions:
+            - condition: state
+              entity_id: media_player.bathroom
+              state: 'paused'
+          sequence:
+            - service: script.bathroom_music_on
+      default: [ ]
+
+bathroom_music_off:
+  alias: '[Bathroom] Music turn off'
+  sequence:
+    - service: media_player.media_pause
+      target:
+        entity_id: media_player.bathroom
+
+bathroom_music_on:
+  alias: '[Bathroom] Music turn on'
+  sequence:
+    - service: media_player.media_play
+      target:
+        entity_id: media_player.bathroom
+    - choose:
+        - conditions:
+            - condition: state
+              entity_id: binary_sensor.shower_motion
+              state: 'on'
+          sequence:
+            - service: media_player.volume_set
+              data:
+                volume_level: 0.65
+              target:
+                entity_id: media_player.bathroom
+        - conditions:
+            - condition: state
+              entity_id: binary_sensor.shower_motion
+              state: 'off'
+          sequence:
+            - service: media_player.volume_set
+              data:
+                volume_level: 0.45
+              target:
+                entity_id: media_player.bathroom
+
 pause_all_players:
   alias: Pause all playing devices
   sequence: