1 """Support for Clementine Music Player as media player."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from clementineremote
import ClementineRemote
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as MEDIA_PLAYER_PLATFORM_SCHEMA,
14 MediaPlayerEntityFeature,
24 DEFAULT_NAME =
"Clementine Remote"
29 PLATFORM_SCHEMA = MEDIA_PLAYER_PLATFORM_SCHEMA.extend(
31 vol.Required(CONF_HOST): cv.string,
32 vol.Optional(CONF_ACCESS_TOKEN): cv.positive_int,
33 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
34 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
42 add_entities: AddEntitiesCallback,
43 discovery_info: DiscoveryInfoType |
None =
None,
45 """Set up the Clementine platform."""
47 host = config[CONF_HOST]
48 port = config[CONF_PORT]
49 token = config.get(CONF_ACCESS_TOKEN)
51 client = ClementineRemote(host, port, token, reconnect=
True)
57 """Representation of Clementine Player."""
59 _attr_media_content_type = MediaType.MUSIC
60 _attr_supported_features = (
61 MediaPlayerEntityFeature.PAUSE
62 | MediaPlayerEntityFeature.VOLUME_STEP
63 | MediaPlayerEntityFeature.PREVIOUS_TRACK
64 | MediaPlayerEntityFeature.VOLUME_SET
65 | MediaPlayerEntityFeature.NEXT_TRACK
66 | MediaPlayerEntityFeature.SELECT_SOURCE
67 | MediaPlayerEntityFeature.PLAY
71 """Initialize the Clementine device."""
76 """Retrieve the latest data from the Clementine Player."""
80 if client.state ==
"Playing":
82 elif client.state ==
"Paused":
83 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
84 elif client.state ==
"Disconnected":
87 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
89 if client.last_update
and (time.time() - client.last_update > 40):
92 volume =
float(client.volume)
if client.volume
else 0.0
94 if client.active_playlist_id
in client.playlists:
95 self.
_attr_source_attr_source = client.playlists[client.active_playlist_id][
"name"]
100 if client.current_track:
113 """Select input source."""
115 sources = [s
for s
in client.playlists.values()
if s[
"name"] == source]
116 if len(sources) == 1:
117 client.change_song(sources[0][
"id"], 0)
120 """Fetch media image of current playing image."""
121 if self.
_client_client.current_track:
122 image = bytes(self.
_client_client.current_track[
"art"])
123 return (image,
"image/png")
128 """Volume up the media player."""
129 newvolume =
min(self.
_client_client.volume + 4, 100)
130 self.
_client_client.set_volume(newvolume)
133 """Volume down media player."""
134 newvolume =
max(self.
_client_client.volume - 4, 0)
135 self.
_client_client.set_volume(newvolume)
138 """Send mute command."""
139 self.
_client_client.set_volume(0)
142 """Set volume level."""
143 self.
_client_client.set_volume(
int(100 * volume))
146 """Simulate play pause media player."""
153 """Send play command."""
154 self.
_attr_state_attr_state = MediaPlayerState.PLAYING
158 """Send media pause command to media player."""
159 self.
_attr_state_attr_state = MediaPlayerState.PAUSED
163 """Send next track command."""
167 """Send the previous track command."""
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)