Skip to content

Commit

Permalink
Reload sources after turn off
Browse files Browse the repository at this point in the history
  • Loading branch information
fwestenberg committed Jan 24, 2023
1 parent 4d68313 commit dc9b669
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
20 changes: 10 additions & 10 deletions devialet/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
NORMAL_INPUTS = {
"Airplay": "airplay2",
"Bluetooth": "bluetooth",
"Digital left": "digital_left",
"Digital right": "digital_right",
"Line": "line",
"Digital left": "digital_left", #Arch only
"Digital right": "digital_right", #Arch only
"Line": "line", #Arch only
"Online": "upnp",
"Optical": "optical",
"Optical left": "optical_left",
"Optical right": "optical_right",
"Optical jack": "opticaljack",
"Optical jack left": "opticaljack_left",
"Optical jack right": "opticaljack_right",
"Phono": "phono",
"Optical": "optical", #Phantom I, Dialog (Mono)
"Optical left": "optical_left", #Phantom I (Stereo)
"Optical right": "optical_right", #Phantom I (Stereo)
"Optical jack": "opticaljack", #Phantom II (Mono)
"Optical jack left": "opticaljack_left", #Phantom II (Stereo)
"Optical jack right": "opticaljack_right", #Phantom II (Stereo)
"Phono": "phono", #Arch only
"Raat": "raat",
"Spotify Connect": "spotifyconnect",
}
Expand Down
69 changes: 34 additions & 35 deletions devialet/devialet_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ def __init__(self, host, session):
self._device_role = ""
self._is_available = False

async def async_update(self):
async def async_update(self) -> bool | None:
"""Get the latest details from the device."""

if self._general_info is None:
self._general_info = await self.get_request(UrlSuffix.GET_GENERAL_INFO)

if self._general_info is None:
return False

if not self._is_available:
self._sources == None

if self._sources is None:
self._sources = await self.get_request(UrlSuffix.GET_SOURCES)
Expand All @@ -51,9 +53,7 @@ async def async_update(self):
return self._is_available

self._volume = await self.get_request(UrlSuffix.GET_VOLUME)

self._night_mode = await self.get_request(UrlSuffix.GET_NIGHT_MODE)

self._equalizer = await self.get_request(UrlSuffix.GET_EQUALIZER)

try:
Expand All @@ -70,97 +70,97 @@ async def async_update(self):
position = await self.get_request(UrlSuffix.GET_CURRENT_POSITION)
try:
self._current_position = position["position"]
self._position_updated_at = datetime.datetime.now()
self._position_updated_at = datetime.datetime.utcnow()
except (KeyError, TypeError):
self._current_position = None
self._position_updated_at = None

return True

@property
def is_available(self):
def is_available(self) -> bool | None:
"""Return available."""
return self._is_available

@property
def device_id(self):
def device_id(self) -> str | None:
"""Return the device id."""
try:
return self._general_info["deviceId"]
except (KeyError, TypeError):
return None

@property
def serial(self):
def serial(self) -> str | None:
"""Return the serial."""
try:
return self._general_info["serial"]
except (KeyError, TypeError):
return None

@property
def device_name(self):
def device_name(self) -> str | None:
"""Return the device name."""
try:
return self._general_info["deviceName"]
except (KeyError, TypeError):
return None

@property
def device_role(self):
def device_role(self) -> str | None:
"""Return the device role."""
try:
return self._general_info["role"]
except (KeyError, TypeError):
return None

@property
def model(self):
def model(self) -> str | None:
"""Return the device model."""
try:
return self._general_info["model"]
except (KeyError, TypeError):
return None

@property
def version(self):
def version(self) -> str | None:
"""Return the device version."""
try:
return self._general_info["release"]["version"]
except (KeyError, TypeError):
return None

@property
def source_state(self):
"""Return the general info."""
def source_state(self) -> any | None:
"""Return the source state object."""
return self._source_state

@property
def playing_state(self):
def playing_state(self) -> str | None:
"""Return the state of the device."""
try:
return self._source_state["playingState"]
except (KeyError, TypeError):
return None

@property
def volume_level(self):
def volume_level(self) -> float | None:
"""Volume level of the media player (0..1)."""
try:
return self._volume["volume"] * 0.01
except (KeyError, TypeError):
return None

@property
def is_volume_muted(self):
def is_volume_muted(self) -> bool | None:
"""Return boolean if volume is currently muted."""
try:
return self._source_state["muteState"] == "muted"
except (KeyError, TypeError):
return None

@property
def source_list(self):
def source_list(self) -> list | None:
"""Return the list of available input sources."""

if self._sources is None or len(self._source_list) > 0:
Expand All @@ -170,15 +170,14 @@ def source_list(self):
source_type = source["type"]
device_id = source["deviceId"]

if source_type == "optical" or source_type == "opticaljack":
if self.device_role in SPEAKER_POSITIONS and (source_type == "optical" or source_type == "opticaljack"):
# Stereo devices have the role FrontLeft or FrontRight. Add a suffix to the source to recognize the device.
position = ""

if self.device_role in SPEAKER_POSITIONS:
for role, position in SPEAKER_POSITIONS.items():
if (
device_id == self.device_id and role == self.device_role
) or (device_id != self.device_id and role != self.device_role):
source_type = source_type + "_" + position
for role, position in SPEAKER_POSITIONS.items():
if (
device_id == self.device_id and role == self.device_role
) or (device_id != self.device_id and role != self.device_role):
source_type = source_type + "_" + position

for pretty_name, name in NORMAL_INPUTS.items():
if name == source_type:
Expand All @@ -187,7 +186,7 @@ def source_list(self):
return sorted(self._source_list)

@property
def available_options(self):
def available_options(self) -> any | None:
"""Return the list of available options for this source."""
try:
return self._source_state["availableOptions"]
Expand All @@ -211,7 +210,7 @@ def media_album_name(self) -> str | None:
return None

@property
def media_title(self):
def media_title(self) -> str | None:
"""Return the current media info."""
try:
return self._source_state["metadata"]["title"]
Expand Down Expand Up @@ -242,15 +241,15 @@ def position_updated_at(self) -> datetime.datetime | None:
return self._position_updated_at

@property
def supported_features(self):
def supported_features(self) -> any | None:
"""Flag media player features that are supported."""
try:
return self._source_state["availableOptions"]
except (KeyError, TypeError):
return None

@property
def source(self):
def source(self) -> str | None:
"""Return the current input source."""
try:
source_type = self._source_state["source"]["type"]
Expand All @@ -271,23 +270,23 @@ def source(self):
return source_type

@property
def night_mode(self):
def night_mode(self) -> bool | None:
"""Return the current nightmode state."""
try:
return self._night_mode["nightMode"] == "on"
except (KeyError, TypeError):
return None

@property
def equalizer(self):
def equalizer(self) -> str | None:
"""Return the current equalizer preset."""
try:
if self._equalizer["enabled"]:
return self._equalizer["preset"]
except (KeyError, TypeError):
return None

async def async_get_diagnostics(self):
async def async_get_diagnostics(self) -> any | None:
"""Return the diagnostic data."""
return {
"is_available": self._is_available,
Expand Down Expand Up @@ -420,7 +419,7 @@ async def async_select_source(self, source: str) -> None:
str(UrlSuffix.SELECT_SOURCE).replace("%SOURCE_ID%", source_id), {}
)

async def get_request(self, suffix=str):
async def get_request(self, suffix=str) -> any | None:
"""Generic GET method."""

url = "http://" + self._host + str(suffix)
Expand Down Expand Up @@ -462,7 +461,7 @@ async def get_request(self, suffix=str):
LOGGER.debug("Devialet: unknown exception occurred")
return None

async def post_request(self, suffix=str, body=str):
async def post_request(self, suffix=str, body=str) -> any | None:
"""Generic POST method."""

url = "http://" + self._host + str(suffix)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='devialet',
packages=['devialet'],
version='1.3.2',
version='1.4.0',
license='MIT',
description='Devialet API',
author='fwestenberg',
Expand Down

0 comments on commit dc9b669

Please sign in to comment.