[modules/pulsectl] fix case when no devices are available

no devices lead to an exception that completely stopped bumblebee-status
from processing data.

handle this case more gracefully by defaulting to a volume of 0%. if
this proves to be an issue, we can still add error indicators later.

see #940
This commit is contained in:
tobi-wan-kenobi 2022-11-27 12:02:38 +01:00
parent 6a93238bda
commit 87a2890b48

View file

@ -51,7 +51,7 @@ class Module(core.module.Module):
self.background = True self.background = True
self.__type = type self.__type = type
self.__volume = "n/a" self.__volume = 0
self.__devicename = "n/a" self.__devicename = "n/a"
self.__muted = False self.__muted = False
self.__showbars = util.format.asbool(self.parameter("showbars", False)) self.__showbars = util.format.asbool(self.parameter("showbars", False))
@ -108,11 +108,15 @@ class Module(core.module.Module):
def toggle_mute(self, _): def toggle_mute(self, _):
with pulsectl.Pulse(self.id + "vol") as pulse: with pulsectl.Pulse(self.id + "vol") as pulse:
dev = self.get_device(pulse) dev = self.get_device(pulse)
if not dev:
return
pulse.mute(dev, not self.__muted) pulse.mute(dev, not self.__muted)
def change_volume(self, amount): def change_volume(self, amount):
with pulsectl.Pulse(self.id + "vol") as pulse: with pulsectl.Pulse(self.id + "vol") as pulse:
dev = self.get_device(pulse) dev = self.get_device(pulse)
if not dev:
return
vol = dev.volume vol = dev.volume
vol.value_flat += amount vol.value_flat += amount
if self.__limit > 0 and vol.value_flat > self.__limit/100: if self.__limit > 0 and vol.value_flat > self.__limit/100:
@ -132,16 +136,22 @@ class Module(core.module.Module):
for dev in devs: for dev in devs:
if dev.name == default: if dev.name == default:
return dev return dev
return devs[0] # fallback if len(devs) == 0:
return None
return devs[0] # fallback
def process(self, _): def process(self, _):
with pulsectl.Pulse(self.id + "proc") as pulse: with pulsectl.Pulse(self.id + "proc") as pulse:
dev = self.get_device(pulse) dev = self.get_device(pulse)
self.__volume = dev.volume.value_flat if not dev:
self.__muted = dev.mute self.__volume = 0
self.__devicename = dev.name self.__devicename = "n/a"
else:
self.__volume = dev.volume.value_flat
self.__muted = dev.mute
self.__devicename = dev.name
core.event.trigger("update", [self.id], redraw_only=True) core.event.trigger("update", [self.id], redraw_only=True)
core.event.trigger("draw") core.event.trigger("draw")