From 08f57ae23245c6a866406a3420ca1c35f0decb16 Mon Sep 17 00:00:00 2001 From: "Alan D. Tse" Date: Thu, 2 Jan 2020 01:36:02 -0800 Subject: [PATCH] style(media_player): simplify dict checks Use dict.get instead of other key checks --- custom_components/alexa_media/media_player.py | 121 ++++-------------- 1 file changed, 28 insertions(+), 93 deletions(-) diff --git a/custom_components/alexa_media/media_player.py b/custom_components/alexa_media/media_player.py index b953faa8..62dff9d9 100644 --- a/custom_components/alexa_media/media_player.py +++ b/custom_components/alexa_media/media_player.py @@ -416,8 +416,7 @@ async def refresh(self, device=None): self.hass.data[DATA_ALEXAMEDIA]["accounts"][ self._login.email ]["entities"]["media_player"].get(x) - and - self.hass.data[DATA_ALEXAMEDIA]["accounts"][ + and self.hass.data[DATA_ALEXAMEDIA]["accounts"][ self._login.email ]["entities"]["media_player"][x].state == STATE_PLAYING @@ -451,8 +450,7 @@ async def refresh(self, device=None): parent_session["lemurVolume"]["memberVolume"][ self.device_serial_number ] - if parent_session["lemurVolume"] - and "memberVolume" in parent_session["lemurVolume"] + if parent_session.get("lemurVolume", {}).get("memberVolume") else session["volume"] ) session = {"playerInfo": session} @@ -460,12 +458,12 @@ async def refresh(self, device=None): self._playing_parent = None session = await self.alexa_api.get_state() await self._clear_media_details() - # update the session if it exists; not doing relogin here + # update the session if it exists if session: self._session = session - if self._session and "playerInfo" in self._session: + if self._session and self._session.get("playerInfo"): self._session = self._session["playerInfo"] - if self._session["transport"] is not None: + if self._session.get("transport"): self._shuffle = ( self._session["transport"]["shuffle"] == "SELECTED" if ( @@ -482,97 +480,37 @@ async def refresh(self, device=None): ) else None ) - if self._session["state"] is not None: + if self._session.get("state"): self._media_player_state = self._session["state"] - self._media_pos = ( - self._session["progress"]["mediaProgress"] - if ( - self._session["progress"] is not None - and "mediaProgress" in self._session["progress"] - ) - else None - ) - self._media_title = ( - self._session["infoText"]["title"] - if ( - self._session["infoText"] is not None - and "title" in self._session["infoText"] - ) - else None - ) - self._media_artist = ( - self._session["infoText"]["subText1"] - if ( - self._session["infoText"] is not None - and "subText1" in self._session["infoText"] - ) - else None - ) - self._media_album_name = ( - self._session["infoText"]["subText2"] - if ( - self._session["infoText"] is not None - and "subText2" in self._session["infoText"] - ) - else None + self._media_pos = self._session.get("progress", {}).get("mediaProgress") + self._media_title = self._session.get("infoText", {}).get("title") + self._media_artist = self._session.get("infoText", {}).get("subText1") + self._media_album_name = self._session.get("infoText", {}).get( + "subText2" ) - self._media_image_url = ( - self._session["mainArt"]["url"] - if ( - self._session["mainArt"] is not None - and "url" in self._session["mainArt"] - ) - else None - ) - self._media_duration = ( - self._session["progress"]["mediaLength"] - if ( - self._session["progress"] is not None - and "mediaLength" in self._session["progress"] - ) - else None + self._media_image_url = self._session.get("mainArt", {}).get("url") + self._media_duration = self._session.get("progress", {}).get( + "mediaLength" ) - if not self._session["lemurVolume"]: - self._media_is_muted = ( - self._session["volume"]["muted"] - if ( - self._session["volume"] is not None - and "muted" in self._session["volume"] - ) - else None - ) + if not self._session.get("lemurVolume"): + self._media_is_muted = self._session.get("volume", {}).get("muted") self._media_vol_level = ( self._session["volume"]["volume"] / 100 - if ( - self._session["volume"] is not None - and "volume" in self._session["volume"] - ) + if self._session.get("volume", {}).get("volume") else self._media_vol_level ) else: self._media_is_muted = ( - self._session["lemurVolume"]["compositeVolume"]["muted"] - if ( - self._session["lemurVolume"] - and "compositeVolume" in self._session["lemurVolume"] - and self._session["lemurVolume"]["compositeVolume"] - and "muted" - in self._session["lemurVolume"]["compositeVolume"] - ) - else None + self._session.get("lemurVolume", {}) + .get("compositeVolume", {}) + .get("muted") ) self._media_vol_level = ( self._session["lemurVolume"]["compositeVolume"]["volume"] / 100 if ( - self._session["lemurVolume"] - and "compositeVolume" in self._session["lemurVolume"] - and "volume" - in self._session["lemurVolume"]["compositeVolume"] - and ( - self._session["lemurVolume"]["compositeVolume"][ - "volume" - ] - ) + self._session.get("lemurVolume", {}) + .get("compositeVolume", {}) + .get("volume") ) else self._media_vol_level ) @@ -586,12 +524,9 @@ async def refresh(self, device=None): ), filter( lambda x: ( - x - in ( - self.hass.data[DATA_ALEXAMEDIA]["accounts"][ - self._login.email - ]["entities"]["media_player"] - ) + self.hass.data[DATA_ALEXAMEDIA]["accounts"][ + self._login.email + ]["entities"]["media_player"].get(x) and self.hass.data[DATA_ALEXAMEDIA]["accounts"][ self._login.email ]["entities"]["media_player"][x].available @@ -629,7 +564,7 @@ async def async_select_source(self, source): async def _get_source(self): source = "Local Speaker" - if self._bluetooth_state["pairedDeviceList"] is not None: + if self._bluetooth_state.get("pairedDeviceList"): for device in self._bluetooth_state["pairedDeviceList"]: if ( device["connected"] is True @@ -640,7 +575,7 @@ async def _get_source(self): async def _get_source_list(self): sources = [] - if self._bluetooth_state["pairedDeviceList"] is not None: + if self._bluetooth_state.get("pairedDeviceList"): for devices in self._bluetooth_state["pairedDeviceList"]: if devices["profiles"] and "A2DP-SOURCE" in devices["profiles"]: sources.append(devices["friendlyName"])