Fix Sonos const comparison (#50482)

* Fix Sonos const comparison

* Use constants for playback states
This commit is contained in:
jjlawren 2021-05-11 16:06:51 -05:00 committed by GitHub
parent 897dd012cd
commit afe02a4ad2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 6 deletions

View file

@ -33,6 +33,9 @@ SONOS_ALBUM_ARTIST = "album_artists"
SONOS_TRACKS = "tracks"
SONOS_COMPOSER = "composers"
SONOS_STATE_PLAYING = "PLAYING"
SONOS_STATE_TRANSITIONING = "TRANSITIONING"
EXPANDABLE_MEDIA_TYPES = [
MEDIA_TYPE_ALBUM,
MEDIA_TYPE_ARTIST,

View file

@ -58,6 +58,8 @@ from .const import (
MEDIA_TYPES_TO_SONOS,
PLAYABLE_MEDIA_TYPES,
SONOS_CREATE_MEDIA_PLAYER,
SONOS_STATE_PLAYING,
SONOS_STATE_TRANSITIONING,
SOURCE_LINEIN,
SOURCE_TV,
)
@ -281,7 +283,10 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
if self.media.title is None:
return STATE_IDLE
return STATE_PAUSED
if self.media.playback_status in ("PLAYING", "TRANSITIONING"):
if self.media.playback_status in (
SONOS_STATE_PLAYING,
SONOS_STATE_TRANSITIONING,
):
return STATE_PLAYING
return STATE_IDLE

View file

@ -20,7 +20,6 @@ from pysonos.snapshot import Snapshot
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import STATE_PLAYING
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as ent_reg
from homeassistant.helpers.dispatcher import (
@ -43,6 +42,8 @@ from .const import (
SONOS_ENTITY_UPDATE,
SONOS_GROUP_UPDATE,
SONOS_SEEN,
SONOS_STATE_PLAYING,
SONOS_STATE_TRANSITIONING,
SONOS_STATE_UPDATED,
SOURCE_LINEIN,
SOURCE_TV,
@ -578,7 +579,7 @@ class SonosSpeaker:
) -> list[list[SonosSpeaker]]:
"""Pause all current coordinators and restore groups."""
for speaker in (s for s in speakers if s.is_coordinator):
if speaker.media.playback_status == STATE_PLAYING:
if speaker.media.playback_status == SONOS_STATE_PLAYING:
hass.async_create_task(speaker.soco.pause())
groups = []
@ -715,7 +716,7 @@ class SonosSpeaker:
new_status = transport_info["current_transport_state"]
# Ignore transitions, we should get the target state soon
if new_status == "TRANSITIONING":
if new_status == SONOS_STATE_TRANSITIONING:
return
self.media.clear()
@ -782,7 +783,7 @@ class SonosSpeaker:
try:
uri_meta_data = variables["enqueued_transport_uri_meta_data"]
if isinstance(uri_meta_data, DidlAudioBroadcast) and (
self.media.playback_status != STATE_PLAYING
self.media.playback_status != SONOS_STATE_PLAYING
or self.soco.music_source_from_uri(self.media.title) == MUSIC_SRC_RADIO
or (
isinstance(self.media.title, str)
@ -814,7 +815,7 @@ class SonosSpeaker:
# position jumped?
if current_position is not None and self.media.position is not None:
if self.media.playback_status == STATE_PLAYING:
if self.media.playback_status == SONOS_STATE_PLAYING:
assert self.media.position_updated_at is not None
time_delta = dt_util.utcnow() - self.media.position_updated_at
time_diff = time_delta.total_seconds()