Skip to content

Commit

Permalink
Update media_player.py
Browse files Browse the repository at this point in the history
Timeout on connection
  • Loading branch information
dbuezas authored Mar 16, 2024
1 parent 8922547 commit a28d09b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions custom_components/sox/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ async def async_play_media(self, media_type, media_id, **kwargs):

async def _async_send(self, media_id):
try:
reader, writer = await asyncio.open_connection(self._host, self._port)
reader, writer = await asyncio.wait_for(asyncio.open_connection(self._host, self._port), timeout=5)
# Set a timeout for operations
writer.write(f"{media_id};{self._volume};".encode())
await writer.drain()
Expand All @@ -192,8 +192,9 @@ async def _async_send(self, media_id):
_LOGGER.debug("Async SoX connection error: %s", err)
self._is_connected = False
finally:
writer.close()
await writer.wait_closed()
if writer:
writer.close()
await writer.wait_closed()

async def async_volume_up(self):
"""Service to send the MPD the command for volume up."""
Expand Down

0 comments on commit a28d09b

Please sign in to comment.