script:
  routine_bedtime:
    alias: Routine · Bedtime
    sequence:
      # Set master bedroom state
      - service: light.turn_on
        entity_id: light.bedside_light
      - service: fan.turn_on
        entity_id: fan.philips_airpurifier
      - service: sonos.unjoin
        entity_id:
          - media_player.living_room
          - media_player.kitchen
          - media_player.bathroom
      - service: media_player.volume_set
        entity_id: media_player.master_bedroom
        data:
          volume_level: 0.15

      # Turn off everything else
      - service: media_player.media_stop
        entity_id:
          - media_player.living_room
          - media_player.kitchen
          - media_player.bathroom
      - service: media_player.turn_off
        entity_id:
          - media_player.nvidia_shield
          - media_player.living_room_tv
      - service: light.turn_off
        entity_id: light.living_room_all_lights

automation:
  - alias: Routine · Bedtime · Toggle on bedside switch hold
    trigger:
      - platform: event
        event_type: zha_event
        event_data:
          device_ieee: !secret master_bedroom_bedside_switch_ieee
          command: hold
    action:
      - service: script.routine_bedtime
