Use faster contains check in media_player (#106434)

pull/105820/head
J. Nick Koston 2023-12-26 13:18:22 -10:00 committed by GitHub
parent 9dde42a023
commit 4b19c28ad9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 24 deletions

View File

@ -905,87 +905,85 @@ class MediaPlayerEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
@property
def support_play(self) -> bool:
"""Boolean if play is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.PLAY)
return MediaPlayerEntityFeature.PLAY in self.supported_features
@final
@property
def support_pause(self) -> bool:
"""Boolean if pause is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.PAUSE)
return MediaPlayerEntityFeature.PAUSE in self.supported_features
@final
@property
def support_stop(self) -> bool:
"""Boolean if stop is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.STOP)
return MediaPlayerEntityFeature.STOP in self.supported_features
@final
@property
def support_seek(self) -> bool:
"""Boolean if seek is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.SEEK)
return MediaPlayerEntityFeature.SEEK in self.supported_features
@final
@property
def support_volume_set(self) -> bool:
"""Boolean if setting volume is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.VOLUME_SET)
return MediaPlayerEntityFeature.VOLUME_SET in self.supported_features
@final
@property
def support_volume_mute(self) -> bool:
"""Boolean if muting volume is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.VOLUME_MUTE)
return MediaPlayerEntityFeature.VOLUME_MUTE in self.supported_features
@final
@property
def support_previous_track(self) -> bool:
"""Boolean if previous track command supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.PREVIOUS_TRACK)
return MediaPlayerEntityFeature.PREVIOUS_TRACK in self.supported_features
@final
@property
def support_next_track(self) -> bool:
"""Boolean if next track command supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.NEXT_TRACK)
return MediaPlayerEntityFeature.NEXT_TRACK in self.supported_features
@final
@property
def support_play_media(self) -> bool:
"""Boolean if play media command supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.PLAY_MEDIA)
return MediaPlayerEntityFeature.PLAY_MEDIA in self.supported_features
@final
@property
def support_select_source(self) -> bool:
"""Boolean if select source command supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.SELECT_SOURCE)
return MediaPlayerEntityFeature.SELECT_SOURCE in self.supported_features
@final
@property
def support_select_sound_mode(self) -> bool:
"""Boolean if select sound mode command supported."""
return bool(
self.supported_features & MediaPlayerEntityFeature.SELECT_SOUND_MODE
)
return MediaPlayerEntityFeature.SELECT_SOUND_MODE in self.supported_features
@final
@property
def support_clear_playlist(self) -> bool:
"""Boolean if clear playlist command supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.CLEAR_PLAYLIST)
return MediaPlayerEntityFeature.CLEAR_PLAYLIST in self.supported_features
@final
@property
def support_shuffle_set(self) -> bool:
"""Boolean if shuffle is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.SHUFFLE_SET)
return MediaPlayerEntityFeature.SHUFFLE_SET in self.supported_features
@final
@property
def support_grouping(self) -> bool:
"""Boolean if player grouping is supported."""
return bool(self.supported_features & MediaPlayerEntityFeature.GROUPING)
return MediaPlayerEntityFeature.GROUPING in self.supported_features
async def async_toggle(self) -> None:
"""Toggle the power on the media player."""
@ -1014,7 +1012,7 @@ class MediaPlayerEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
if (
self.volume_level is not None
and self.volume_level < 1
and self.supported_features & MediaPlayerEntityFeature.VOLUME_SET
and MediaPlayerEntityFeature.VOLUME_SET in self.supported_features
):
await self.async_set_volume_level(
min(1, self.volume_level + self.volume_step)
@ -1032,7 +1030,7 @@ class MediaPlayerEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
if (
self.volume_level is not None
and self.volume_level > 0
and self.supported_features & MediaPlayerEntityFeature.VOLUME_SET
and MediaPlayerEntityFeature.VOLUME_SET in self.supported_features
):
await self.async_set_volume_level(
max(0, self.volume_level - self.volume_step)
@ -1077,14 +1075,14 @@ class MediaPlayerEntity(Entity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
data: dict[str, Any] = {}
supported_features = self.supported_features
if supported_features & MediaPlayerEntityFeature.SELECT_SOURCE and (
if (
source_list := self.source_list
):
) and MediaPlayerEntityFeature.SELECT_SOURCE in supported_features:
data[ATTR_INPUT_SOURCE_LIST] = source_list
if supported_features & MediaPlayerEntityFeature.SELECT_SOUND_MODE and (
if (
sound_mode_list := self.sound_mode_list
):
) and MediaPlayerEntityFeature.SELECT_SOUND_MODE in supported_features:
data[ATTR_SOUND_MODE_LIST] = sound_mode_list
return data
@ -1282,7 +1280,7 @@ async def websocket_browse_media(
connection.send_error(msg["id"], "entity_not_found", "Entity not found")
return
if not player.supported_features & MediaPlayerEntityFeature.BROWSE_MEDIA:
if MediaPlayerEntityFeature.BROWSE_MEDIA not in player.supported_features:
connection.send_message(
websocket_api.error_message(
msg["id"], ERR_NOT_SUPPORTED, "Player does not support browsing media"

View File

@ -159,7 +159,7 @@ class MockMediaPlayer(media_player.MediaPlayerEntity):
@property
def supported_features(self):
"""Flag media player features that are supported."""
return self._supported_features
return MediaPlayerEntityFeature(self._supported_features)
@property
def media_image_url(self):