1 3
import contextlib
2 3
import logging
3

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

8 3
logger = logging.getLogger(__name__)
9

10

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

27

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

32 3
    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 3
        if self._mixer is None:
40 3
            return None
41

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

47 3
        return None
48

49 3
    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 3
        validation.check_integer(volume, min=0, max=100)
59

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

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

68 3
        return False
69

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

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

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

84 3
        return None
85

86 3
    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 3
        validation.check_boolean(mute)
94 3
        if self._mixer is None:
95 3
            return False  # TODO: 2.0 return None
96

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

102 3
        return False
103

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

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

Read our documentation on viewing source code .

Loading