Or check how KDE's Solid framework or rather its Linux backend handle that

Cheers,
_