2024/09/12

YTMusic & MA Playlist 무선버튼에 동시할당

 

HA 이전하고서 리소스에 여유가 생겨 Music assistant설치하고 YTMusic과 같이 쓸 수 있도록 이케아 볼륨리모컨 자동화 조정.
MA는 로컬을 포함한 여러 소스의 미디어를 재생할 수 있어 HACS에서 MA의 구성요소를 만들 수 있는 컨포넌트를 설치하면 무선버튼에 할당하는 등의 자동화에 활용할 수 있음.

MA Playlist 번호는 UI 자동화에서 playlist를 선택한 후 yaml 편집으로 변경하면 확인할 수 있고 input_select에 원하는 리스트를 만들어 자동화에 playlist 번호로 연결하면 됨.

볼륨리모컨 연결은 YTMusic과 MA를 동시에 듣는 경우는 없으니 둘중 하나가 실행중이면 거기에 해당 되는 player 액션만 실행하고 다른 액션은 script.nop으로 넘기면 됨.

ON/OFF 액션은 템플릿 스위치로 두 플레이어 중 하나라도 실행되어 있으면 ON, 그외는 OFF로 판단. ON실행시 YTMusic만 실행하고 OFF는 두 플레이어 모두 OFF. 더블클릭으로 두 플레이어 모두 OFF시 MA Playlist 실행하고 그 외는 실행되고 있는 플레이어에 next track명령.


switch.yaml
- platform: template
  switches:
    ytmusic:
      value_template: "{{ is_state('media_player.all', 'playing') or is_state('media_player.all_2', 'playing') }}"
      turn_on:
      - service: media_player.turn_on
        entity_id: media_player.ytube_music_player
      turn_off:
      - service: media_player.turn_off
        data:
          entity_id:
            - media_player.all
            - media_player.all_2

input_select.yaml
ma_playlist:
  name: MA Playlist
  options:
    - classic
    - inst
    - comp
    - fork
    - pops

automations.yaml
- id: 버튼-E1744-1
  alias: 버튼-E1744-1
  trigger:
  - entity_id: sensor.li_volume_remote_action
    platform: state
    to:
      - "rotate_right"
      - "rotate_left"
      - "rotate_stop"
      - "toggle"
      - "brightness_step_up"
      - "brightness_step_down"
  action:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.state == ''rotate_right'' }}'
        sequence:

          - entity_id: #통합 볼륨업
              - input_number.speaker_volume
            service_template: "{{ is_state('media_player.all', 'playing') |
              iif('input_number.increment', 'script.nop') }}"

          - data: #리시버 볼륨업
              entity_id: remote.rm4_mini
              device: audio
              command: volume_up
            service_template: "{{ (states('sensor.neo07_watt') | float(0) > 15) |
              iif('remote.send_command', 'script.nop') }}"

          - entity_id: #TV 볼륨업
              - input_number.speaker_ro991_volume
            service_template: "{{ (is_state('media_player.ro991', 'on') and is_state('switch.bluemoon', 'off')) |
              iif('input_number.increment', 'script.nop') }}"

          - data: #PC 볼륨업
              entity_id: remote.rm4_mini
              device: pc
              command: volume_up
            service_template: "{{ (is_state('media_player.ro991', 'on') and
              is_state('switch.bluemoon', 'on') and is_state('media_player.livingroom2', 'off') and
              (states('sensor.neo07_watt') | float(0) < 15)) |
              iif('remote.send_command', 'script.nop') }}"

      - conditions:
        - condition: template
          value_template: '{{ trigger.to_state.state == ''rotate_left'' }}'
        sequence:

          - entity_id: #통합 볼륨다운
              - input_number.speaker_volume
            service_template: "{{ is_state('media_player.all', 'playing') |
              iif('input_number.decrement', 'script.nop') }}"

          - data: #리시버 볼륨다운
              entity_id: remote.rm4_mini
              device: audio
              command: volume_down
            service_template: "{{ (states('sensor.neo07_watt') | float(0) > 15) |
              iif('remote.send_command', 'script.nop') }}"

          - entity_id: #TV 볼륨다운
              - input_number.speaker_ro991_volume
            service_template: "{{ (is_state('media_player.ro991', 'on') and is_state('switch.bluemoon', 'off')) |
              iif('input_number.decrement', 'script.nop') }}"

          - data: #PC 볼륨다운
              entity_id: remote.rm4_mini
              device: pc
              command: volume_down
            service_template: "{{ (is_state('media_player.ro991', 'on') and
              is_state('switch.bluemoon', 'on') and is_state('media_player.livingroom2', 'off') and
              (states('sensor.neo07_watt') | float(0) < 15)) |
              iif('remote.send_command', 'script.nop') }}"

      - conditions: #YTMusic ON/OFF
        - condition: template
          value_template: '{{ trigger.to_state.state == ''toggle'' }}'
        sequence:
          - service: switch.toggle
            entity_id: switch.ytmusic

      - conditions: #YTMusic Playlist/MA Playlist Next Track
        - condition: template
          value_template: '{{ trigger.to_state.state == ''brightness_step_up'' }}'
        sequence:
          - entity_id: media_player.ytube_music_player
            service_template: "{{ (is_state('media_player.all', 'playing') and 
              is_state('media_player.ytube_music_player', 'playing')) |
              iif('media_player.media_next_track', 'script.nop') }}"
          - entity_id: media_player.all_2
            service_template: "{{ (is_state('media_player.all_2', 'playing') and
              is_state('media_player.ytube_music_player', 'off')) |
              iif('media_player.media_next_track', 'script.ma_playlist') }}"

      - conditions: #YTMusic Playlist/MA Playlist Next Playlist
        - condition: template
          value_template: '{{ trigger.to_state.state == ''brightness_step_down'' }}'
        sequence:
          - entity_id: select.ytube_music_player_playlist
            service_template: "{{ (is_state('media_player.all', 'playing') and 
              is_state('media_player.ytube_music_player', 'playing')) |
              iif('select.select_next', 'script.nop') }}"
          - entity_id: input_select.ma_playlist
            service_template: "{{ (is_state('media_player.all_2', 'playing') and
              is_state('media_player.ytube_music_player', 'off')) |
              iif('input_select.select_next', 'script.nop') }}"
  mode: single

- id: 입력-MA/Playlist
  alias: 입력-MA/Playlist
  trigger:
  - entity_id: input_select.ma_playlist
    platform: state
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''classic'' }}'
      sequence:
      - action: media_player.play_media
        target:
          entity_id: media_player.all_2
        data:
          media_content_id: library://playlist/64
          media_content_type: playlist
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''inst'' }}'
      sequence:
      - action: media_player.play_media
        target:
          entity_id: media_player.all_2
        data:
          media_content_id: library://playlist/44
          media_content_type: playlist
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''comp'' }}'
      sequence:
      - action: media_player.play_media
        target:
          entity_id: media_player.all_2
        data:
          media_content_id: library://playlist/41
          media_content_type: playlist
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''fork'' }}'
      sequence:
      - action: media_player.play_media
        target:
          entity_id: media_player.all_2
        data:
          media_content_id: library://playlist/55
          media_content_type: playlist
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''pops'' }}'
      sequence:
      - action: media_player.play_media
        target:
          entity_id: media_player.all_2
        data:
          media_content_id: library://playlist/54
          media_content_type: playlist
  mode: single