async def get_album(self, item_id: int) -> FullAlbum: """Get album record by id.""" item_id = try_parse_int(item_id) # get from db for item in await self.get_albums("WHERE item_id = %d" % item_id): item.artist = (await self.get_artist_by_prov_id( item.artist.provider, item.artist.item_id) or item.artist) return item return None
async def remove_from_library(self, item_id: int, media_type: MediaType, provider: str): """Remove item from the library.""" async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn: item_id = try_parse_int(item_id) db_name = media_type.value + "s" sql_query = f"UPDATE {db_name} SET in_library=0 WHERE item_id=?;" await db_conn.execute(sql_query, (item_id, )) await db_conn.commit()
async def add_to_library(self, item_id: int, media_type: MediaType, provider: str): """Add an item to the library (item must already be present in the db!).""" async with aiosqlite.connect(self._dbfile, timeout=120) as db_conn: item_id = try_parse_int(item_id) db_name = media_type.value + "s" sql_query = f"UPDATE {db_name} SET in_library=1 WHERE item_id=?;" await db_conn.execute(sql_query, (item_id, )) await db_conn.commit()
async def cmd_volume_set(self, player_id: str, volume_level: int) -> None: """ Send volume level command to given player. :param player_id: player_id of the player to handle the command. :param volume_level: volume level to set (0..100). """ player = self.get_player(player_id) if not player: return player_config = self.mass.config.player_settings[player.player_id] volume_level = try_parse_int(volume_level) if volume_level < 0: volume_level = 0 elif volume_level > 100: volume_level = 100 # player control support if player_config.get(CONF_VOLUME_CONTROL): control = self.get_player_control( player_config[CONF_VOLUME_CONTROL]) if control: await control.set_state(volume_level) # just force full volume on actual player if volume is outsourced to volumecontrol await player.cmd_volume_set(100) # handle group volume elif player.is_group_player: cur_volume = player.volume_level new_volume = volume_level volume_dif = new_volume - cur_volume if cur_volume == 0: volume_dif_percent = 1 + (new_volume / 100) else: volume_dif_percent = volume_dif / cur_volume for child_player_id in player.group_childs: if child_player_id == player_id: continue child_player = self.get_player(child_player_id) if (child_player and child_player.available and child_player.player_state.powered): cur_child_volume = child_player.volume_level new_child_volume = cur_child_volume + (cur_child_volume * volume_dif_percent) await self.cmd_volume_set(child_player_id, new_child_volume) # regular volume command else: await player.cmd_volume_set(volume_level)
async def get_track(self, item_id: int) -> FullTrack: """Get full track record by id.""" item_id = try_parse_int(item_id) for item in await self.get_tracks("WHERE item_id = %d" % item_id): # include full album info item.albums = set( filter( None, [ await self.get_album_by_prov_id( album.provider, album.item_id) for album in item.albums ], )) item.album = next(iter(item.albums)) # include full artist info item.artists = { await self.get_artist_by_prov_id(artist.provider, artist.item_id) or artist for artist in item.artists } return item return None
async def get_artist(self, item_id: int) -> Artist: """Get artist record by id.""" item_id = try_parse_int(item_id) for item in await self.get_artists("WHERE item_id = %d" % item_id): return item return None
async def get_radio(self, item_id: int) -> Playlist: """Get radio record by id.""" item_id = try_parse_int(item_id) for item in await self.get_radios(f"WHERE item_id = {item_id}"): return item return None