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

group:
  all_sonos:
    name: All Sonos
    entities:
      - media_player.master_bedroom
      - media_player.living_room
      - media_player.kitchen
      - media_player.bathroom

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', 'sonos_group') %}
          {{ 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', 'sonos_group') %}
          {{ group|length > 1 and group[0] == 'media_player.living_room' }}

script:
  sonos_join_bedroom:
    alias: Sonos · Join to Bedroom
    sequence:
      - service: sonos.join
        entity_id: group.all_sonos
        data:
          master: media_player.master_bedroom
  sonos_join_tv:
    alias: Sonos · Join to TV
    sequence:
      - service: sonos.join
        entity_id: group.all_sonos
        data:
          master: media_player.living_room
      - service: media_player.select_source
        entity_id: media_player.living_room
        data:
          source: TV
  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: sonos.set_option
        entity_id: media_player.living_room
        data:
          night_sound: true
      - 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: sonos.set_option
        entity_id: media_player.living_room
        data:
          night_sound: false
      - 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
