# Depends on Sonos integration (config flow): https://www.home-assistant.io/integrations/sonos

binary_sensor:
  - platform: template
    sensors:
      sonos_recoverable:
        friendly_name: Sonos recoverable
        value_template: >-

          {% set content_id = state_attr('media_player.master_bedroom', 'media_content_id') %}
          {% if content_id == None
              or content_id.startswith('x-sonos-spotify:spotify')
              or content_id.startswith('x-sonosapi-hls-static') %}
            True
          {% else %}
            False
          {% endif %}

      sonos_is_bedroom_master:
        friendly_name: Sonos is Bedroom master
        value_template: >-
          {% set group = state_attr('media_player.master_bedroom', 'group_members') %}
          {{ group|length > 1 and group[0] == 'media_player.master_bedroom' }}

      sonos_is_living_room_master:
        friendly_name: Sonos is Living room master
        value_template: >-
          {% set group = state_attr('media_player.master_bedroom', 'group_members') %}
          {{ group|length > 1 and group[0] == 'media_player.living_room' }}

      sonos_is_kitchen_grouped:
        friendly_name: Sonos Kitchen is grouped
        value_template: "{{ state_attr('media_player.kitchen', 'group_members')|length > 1 }}"

      sonos_is_bathroom_grouped:
        friendly_name: Sonos Bathroom is grouped
        value_template: "{{ state_attr('media_player.bathroom', 'group_members')|length > 1 }}"

script:
  sonos_join_bedroom:
    alias: Sonos · Join to Bedroom
    sequence:
      - condition: state
        entity_id: input_select.bedroom_mode
        state: "day"
      - service: media_player.join
        entity_id: media_player.master_bedroom
        data:
          group_members:
            - media_player.master_bedroom
            - media_player.living_room
            - media_player.kitchen
            - media_player.bathroom

  sonos_join_tv:
    alias: Sonos · Join to TV
    sequence:
      - service: media_player.join
        entity_id: media_player.living_room
        data:
          group_members:
            - media_player.master_bedroom
            - media_player.living_room
            - media_player.kitchen
            - media_player.bathroom
      - service: media_player.select_source
        entity_id: media_player.living_room
        data:
          source: TV

  sonos_kitchen_unjoin:
    alias: Sonos · Kitchen · Unjoin
    sequence:
      - service: media_player.unjoin
        entity_id: media_player.kitchen

  sonos_kitchen_join:
    alias: Sonos · Kitchen · Join to master
    use_blueprint:
      path: sonos_join.yaml
      input:
        join_player: media_player.kitchen

  sonos_bathroom_unjoin:
    alias: Sonos · Bathroom · Unjoin
    sequence:
      - service: media_player.unjoin
        entity_id: media_player.bathroom

  sonos_bathroom_join:
    alias: Sonos · Bathroom · Join to master
    use_blueprint:
      path: sonos_join.yaml
      input:
        join_player: media_player.bathroom

  sonos_bathroom_up:
    alias: Sonos · Bathroom up
    sequence:
      - service: media_player.volume_set
        entity_id: media_player.bathroom
        data:
          volume_level: 0.5

  sonos_bathroom_down:
    alias: Sonos · Bathroom down
    sequence:
      - service: media_player.volume_set
        entity_id: media_player.bathroom
        data:
          volume_level: 0.2

  sonos_volume_10:
    alias: Sonos · Volume 10
    sequence:
      - service: media_player.volume_set
        entity_id:
          - media_player.master_bedroom
          - media_player.bathroom
          - media_player.kitchen
          - media_player.living_room
        data:
          volume_level: 0.1

  sonos_volume_20:
    alias: Sonos · Volume 20
    sequence:
      - service: media_player.volume_set
        entity_id:
          - media_player.master_bedroom
          - media_player.bathroom
          - media_player.kitchen
          - media_player.living_room
        data:
          volume_level: 0.2

automation:
  - alias: Media · Sonos · Put Beam in night mode
    trigger:
      platform: time
      at: "22:00:00"
    action:
      - service: switch.turn_on
        entity_id: switch.sonos_living_room_night_sound
      - service: logbook.log
        data:
          name: "🔊 Media Module · "
          message: "Sonos Mode · Putting Beam in night mode"
          domain: media_player

  - alias: Media · Sonos · Put Beam in day mode
    trigger:
      platform: time
      at: "08:00:00"
    action:
      - service: switch.turn_off
        entity_id: switch.sonos_living_room_night_sound
      - service: logbook.log
        data:
          name: "🔊 Media Module · "
          message: "Sonos Mode · Putting Beam in day mode"
          domain: media_player

homeassistant:
  customize:
    media_player.master_bedroom:
      friendly_name: Bedroom Sonos
      icon: mdi:speaker
    media_player.living_room:
      friendly_name: Living room Sonos
      icon: mdi:speaker
    media_player.kitchen:
      friendly_name: Kitchen Sonos
      icon: mdi:speaker
    media_player.bathroom:
      friendly_name: Bathroom Sonos
      icon: mdi:speaker
