Skip to content

Commit

Permalink
style(media_player): simplify dict checks
Browse files Browse the repository at this point in the history
Use dict.get instead of other key checks
  • Loading branch information
alandtse committed Jan 2, 2020
1 parent e0f7df1 commit 08f57ae
Showing 1 changed file with 28 additions and 93 deletions.
121 changes: 28 additions & 93 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -451,21 +450,20 @@ 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}
else:
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 (
Expand All @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"])
Expand Down

0 comments on commit 08f57ae

Please sign in to comment.