1 """Support for interacting with and controlling the cmus music player."""
3 from __future__
import annotations
8 from pycmus
import exceptions, remote
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as MEDIA_PLAYER_PLATFORM_SCHEMA,
14 MediaPlayerEntityFeature,
24 _LOGGER = logging.getLogger(__name__)
29 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
31 vol.Inclusive(CONF_HOST,
"remote"): cv.string,
32 vol.Inclusive(CONF_PASSWORD,
"remote"): cv.string,
33 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
34 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
42 add_entities: AddEntitiesCallback,
43 discover_info: DiscoveryInfoType |
None =
None,
45 """Set up the CMUS platform."""
47 host = config.get(CONF_HOST)
48 password = config.get(CONF_PASSWORD)
49 port = config[CONF_PORT]
50 name = config[CONF_NAME]
52 cmus_remote =
CmusRemote(server=host, port=port, password=password)
55 if cmus_remote.cmus
is None:
62 """Representation of a cmus connection."""
65 """Initialize the cmus remote."""
73 """Connect to the cmus server."""
76 self.
cmuscmus = remote.PyCmus(
79 except exceptions.InvalidPassword:
80 _LOGGER.error(
"The provided password was rejected by cmus")
84 """Representation of a running cmus."""
86 _attr_media_content_type = MediaType.MUSIC
87 _attr_supported_features = (
88 MediaPlayerEntityFeature.PAUSE
89 | MediaPlayerEntityFeature.VOLUME_SET
90 | MediaPlayerEntityFeature.TURN_OFF
91 | MediaPlayerEntityFeature.TURN_ON
92 | MediaPlayerEntityFeature.PREVIOUS_TRACK
93 | MediaPlayerEntityFeature.NEXT_TRACK
94 | MediaPlayerEntityFeature.PLAY_MEDIA
95 | MediaPlayerEntityFeature.SEEK
96 | MediaPlayerEntityFeature.PLAY
100 """Initialize the CMUS device."""
104 auto_name = f
"cmus-{server}"
106 auto_name =
"cmus-local"
111 """Get the latest data and update the state."""
113 status = self.
_remote_remote.cmus.get_status_dict()
114 except BrokenPipeError:
116 except exceptions.ConfigurationError:
117 _LOGGER.warning(
"A configuration error occurred")
120 self.
statusstatus = status
121 if self.
statusstatus.
get(
"status") ==
"playing":
123 elif self.
statusstatus.
get(
"status") ==
"paused":
124 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
134 left = self.
statusstatus[
"set"].
get(
"vol_left")[0]
135 right = self.
statusstatus[
"set"].
get(
"vol_right")[0]
137 volume =
float(left + right) / 2
143 _LOGGER.warning(
"Received no status from cmus")
146 """Service to send the CMUS the command to stop playing."""
147 self.
_remote_remote.cmus.player_stop()
150 """Service to send the CMUS the command to start playing."""
151 self.
_remote_remote.cmus.player_play()
154 """Set volume level, range 0..1."""
155 self.
_remote_remote.cmus.set_volume(
int(volume * 100))
158 """Set the volume up."""
159 left = self.
statusstatus[
"set"].
get(
"vol_left")
160 right = self.
statusstatus[
"set"].
get(
"vol_right")
162 current_volume =
float(left + right) / 2
164 current_volume = left
166 if current_volume <= 100:
167 self.
_remote_remote.cmus.set_volume(
int(current_volume) + 5)
170 """Set the volume down."""
171 left = self.
statusstatus[
"set"].
get(
"vol_left")
172 right = self.
statusstatus[
"set"].
get(
"vol_right")
174 current_volume =
float(left + right) / 2
176 current_volume = left
178 if current_volume <= 100:
179 self.
_remote_remote.cmus.set_volume(
int(current_volume) - 5)
182 self, media_type: MediaType | str, media_id: str, **kwargs: Any
184 """Send the play command."""
185 if media_type
in {MediaType.MUSIC, MediaType.PLAYLIST}:
186 self.
_remote_remote.cmus.player_play_file(media_id)
189 "Invalid media type %s. Only %s and %s are supported",
196 """Send the pause command."""
197 self.
_remote_remote.cmus.player_pause()
200 """Send next track command."""
201 self.
_remote_remote.cmus.player_next()
204 """Send next track command."""
205 self.
_remote_remote.cmus.player_prev()
208 """Send seek command."""
209 self.
_remote_remote.cmus.seek(position)
212 """Send the play command."""
213 self.
_remote_remote.cmus.player_play()
216 """Send the stop command."""
217 self.
_remote_remote.cmus.stop()
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
web.Response get(self, web.Request request, str config_key)