Skip to content

Commit

Permalink
Rename UPnP media title function. Fix set UPnP unavailable.
Browse files Browse the repository at this point in the history
  • Loading branch information
fwestenberg committed Dec 28, 2024
1 parent 473ec45 commit e63c44d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
27 changes: 14 additions & 13 deletions devialet/devialet_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Support for Devialet Phantom speakers."""
"""Support for Devialet Phantom speakers1."""
from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -58,6 +58,9 @@ async def async_update(self) -> bool | None:
self._source_state = await self._async_get_request(UrlSuffix.GET_CURRENT_SOURCE)
# The source state call is enough to find out if the device is available (On or Off)
if not self._is_available:
# Set upnp to none, so discovery will find the new port when it's online again
self._upnp_device = None
self._dmr_device = None
return True

if self._sources is None:
Expand All @@ -67,9 +70,6 @@ async def async_update(self) -> bool | None:
self._night_mode = await self._async_get_request(UrlSuffix.GET_NIGHT_MODE)
self._equalizer = await self._async_get_request(UrlSuffix.GET_EQUALIZER)

if self.source == "upnp":
return True

try:
self._media_duration = self._source_state["metadata"]["duration"]
except (KeyError, TypeError):
Expand Down Expand Up @@ -559,7 +559,7 @@ async def async_discover_upnp_device(self) -> None:
source=("0.0.0.0", 0))
LOGGER.debug("Discovering UPnP device for %s", self._host)

async def async_play_url_source(self, media_url: str, media_title: str, meta_data: Union[None, str, Mapping] = None) -> bool:
async def async_play_url_source(self, media_url: str, media_title: str, meta_data: Union[None, str, Mapping] = None):
"""Play media uri over UPnP."""
if not self.upnp_available:
LOGGER.error("No UPnP location discovered")
Expand All @@ -573,11 +573,12 @@ async def async_play_url_source(self, media_url: str, media_title: str, meta_dat
LOGGER.debug("Action result: %s", str(result))
except UpnpActionResponseError as a:
LOGGER.error("Error playing %s: %s", media_title, a.error_desc)
return
except UpnpXmlParseError as x:
LOGGER.error("Error playing %s %s", media_title, x.text)
return

await self.async_upnp_play()
await self._async_get_upnp_metadata(media_url) # Devialet does not support UPnP metadata

async def async_upnp_play(self) -> None:
"""Send the play command over UPnP."""
Expand All @@ -596,25 +597,25 @@ async def async_upnp_play(self) -> None:
except UpnpXmlParseError:
return

async def _async_get_upnp_metadata(self, url: str) -> any | None:
async def async_get_upnp_media_title(self, url: str) -> str | None:
"""Call the media URL with the HEAD method to get ICY metadata."""
try:
async with self._session.head(
url=url, allow_redirects=False, timeout=2, headers={"Icy-MetaData": 1}
url=url, allow_redirects=False, timeout=2, headers={"Icy-MetaData": "1"}
) as response:
LOGGER.debug(
"Host %s: HTTP Response data: %s",
self._host,
response.headers,
)
title:str = response.headers.get("icy-name")
self._source_state = { "metadata": { "title": title } }
return title

except aiohttp.ClientConnectorError:
return None
return
except asyncio.TimeoutError:
return None
return
except TypeError:
return None
return
except Exception: # pylint: disable=bare-except
return None
return
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.5.3',
version='1.5.4',
license='MIT',
description='Devialet API',
long_description_content_type="text/markdown",
Expand Down

0 comments on commit e63c44d

Please sign in to comment.