There is this "weird" problem: you can not play BluRays that are not mounted and you can not play DVDs that are mounted... So, I created this UDEV script to check if a BluRay or DVD is inserted and mount the disk if necessary.

First of all, you need to configure systemd-udevd to allow mounting into the correct device tree:

mmkdir -p /etc/systemd/system/systemd-udevd.service.d/
vim /etc/systemd/system/systemd-udevd.service.d/myoverride.conf

with this content:

[Service]
MountFlags=shared

Now you can create the udev rule vim /etc/udev/rules.d/82-cdrom.rules:

ATTRS{type}=="5", KERNEL=="sr0", SUBSYSTEM=="block", ENV{ID_PATH}=="pci-0000:00:1f.2-ata-3", ACTION=="change", RUN+="/home/kodi/kodi-play-disc.sh

I also used this useful script to detect if the tray of the drive is open. So download and compile it.

Here is the kodi-play-disc.sh:

/home/kodi/trayopen /dev/sr0
if [ $? -eq 0 ]; then
    # try umount
    grep -q cdrom /proc/mounts
    if [ $? -eq 0 ]; then
        umount /media/cdrom0
        logger "unmounted device..."
    fi
    logger "tray is open"
    exit
fi

logger "BD/DVD detection script started"

# wait a little bit until disk is loaded
sleep 5

blkid /dev/sr0 | grep -q UUID
if [ $? -eq 1 ]; then
    logger "No disk inserted... exit right away"
    exit
fi

# Need to mount first:
mount /media/cdrom0

if [ -f /media/cdrom0/BDMV/index.bdmv ]; then
    logger "this looks like a bluray, keep mounted"
else
    logger "this looks like a dvd, unmounting"
    sleep 2
    umount /media/cdrom0
    logger "umount returned $?"
fi

#logger send play to kodi
#kodi-send --host=127.0.0.1 --port=9777 --action="PlayDVD"
logger "DISC DETECTION exited"

you can also comment in the kodi-send line to play the disc directly.

The sleeps are required so the drive has time to spin up and so on. Otherwise the script will exit too early.