Use faster contains check in media_player (#106434)

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

View File

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