mopidy / mopidy
1 2
import contextlib
2 2
import logging
3

4 2
from mopidy import exceptions
5 2
from mopidy.internal import validation
6 2
from mopidy.internal.models import MixerState
7

8 2
logger = logging.getLogger(__name__)
9

10

11 2
@contextlib.contextmanager
12 1
def _mixer_error_handling(mixer):
13 2
    try:
14 2
        yield
15 2
    except exceptions.ValidationError as e:
16 2
        logger.error(
17
            "%s mixer returned bad data: %s",
18
            mixer.actor_ref.actor_class.__name__,
19
            e,
20
        )
21 2
    except Exception:
22 2
        logger.exception(
23
            "%s mixer caused an exception.",
24
            mixer.actor_ref.actor_class.__name__,
25
        )
26

27

28 2
class MixerController:
29 2
    def __init__(self, mixer):
30 2
        self._mixer = mixer
31

32 2
    def get_volume(self):
33
        """Get the volume.
34

35
        Integer in range [0..100] or :class:`None` if unknown.
36

37
        The volume scale is linear.
38
        """
39 2
        if self._mixer is None:
40 2
            return None
41

42 2
        with _mixer_error_handling(self._mixer):
43 2
            volume = self._mixer.get_volume().get()
44 2
            volume is None or validation.check_integer(volume, min=0, max=100)
45 2
            return volume
46

47 2
        return None
48

49 2
    def set_volume(self, volume):
50
        """Set the volume.
51

52
        The volume is defined as an integer in range [0..100].
53

54
        The volume scale is linear.
55

56
        Returns :class:`True` if call is successful, otherwise :class:`False`.
57
        """
58 2
        validation.check_integer(volume, min=0, max=100)
59

60 2
        if self._mixer is None:
61 2
            return False  # TODO: 2.0 return None
62

63 2
        with _mixer_error_handling(self._mixer):
64 2
            result = self._mixer.set_volume(volume).get()
65 2
            validation.check_instance(result, bool)
66 2
            return result
67

68 2
        return False
69

70 2
    def get_mute(self):
71
        """Get mute state.
72

73
        :class:`True` if muted, :class:`False` unmuted, :class:`None` if
74
        unknown.
75
        """
76 2
        if self._mixer is None:
77 2
            return None
78

79 2
        with _mixer_error_handling(self._mixer):
80 2
            mute = self._mixer.get_mute().get()
81 2
            mute is None or validation.check_instance(mute, bool)
82 2
            return mute
83

84 2
        return None
85

86 2
    def set_mute(self, mute):
87
        """Set mute state.
88

89
        :class:`True` to mute, :class:`False` to unmute.
90

91
        Returns :class:`True` if call is successful, otherwise :class:`False`.
92
        """
93 2
        validation.check_boolean(mute)
94 2
        if self._mixer is None:
95 2
            return False  # TODO: 2.0 return None
96

97 2
        with _mixer_error_handling(self._mixer):
98 2
            result = self._mixer.set_mute(bool(mute)).get()
99 2
            validation.check_instance(result, bool)
100 2
            return result
101

102 2
        return False
103

104 2
    def _save_state(self):
105 2
        return MixerState(volume=self.get_volume(), mute=self.get_mute())
106

107 2
    def _load_state(self, state, coverage):
108 2
        if state and "mixer" in coverage:
109 2
            self.set_mute(state.mute)
110 2
            if state.volume:
111 2
                self.set_volume(state.volume)

Read our documentation on viewing source code .

Loading