Skip to content

Commit

Permalink
Move callback code outside lock (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsayre authored Jan 7, 2025
1 parent ea86b51 commit c13c6eb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyheos/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,17 @@ async def _command_impl() -> HeosMessage:
return response

# Run the within the lock
command_error: CommandFailedError | None = None
await self._command_lock.acquire()
try:
return await _command_impl()
except CommandFailedError as error:
await self._on_command_error(error)
command_error = error
raise # Re-raise to send the error to the caller.
finally:
self._command_lock.release()
if command_error:
await self._on_command_error(command_error)

async def connect(self) -> None:
"""Connect to the HEOS device."""
Expand Down
32 changes: 32 additions & 0 deletions tests/test_heos.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,38 @@ async def test_command_credential_error_dispatches_event(heos: Heos) -> None:
assert heos.signed_in_username is None # type: ignore[unreachable]


@calls_commands(
CallCommand(
"browse.browse_fail_user_not_logged_in",
{const.ATTR_SOURCE_ID: const.MUSIC_SOURCE_FAVORITES},
add_command_under_process=True,
),
CallCommand("browse.get_music_sources"),
)
async def test_command_credential_error_dispatches_event_call_other_command(
heos: Heos,
) -> None:
"""Test calling another command during the credential error in the callback"""
assert heos.is_signed_in
assert heos.signed_in_username is not None

callback_invoked = False

async def callback() -> None:
nonlocal callback_invoked
callback_invoked = True
assert not heos.is_signed_in
assert heos.signed_in_username is None
sources = await heos.get_music_sources(True)
assert sources

heos.add_on_user_credentials_invalid(callback)

with pytest.raises(CommandFailedError):
await heos.get_favorites()
assert callback_invoked


@calls_command("system.heart_beat")
async def test_background_heart_beat(mock_device: MockHeosDevice) -> None:
"""Test heart beat fires at interval."""
Expand Down

0 comments on commit c13c6eb

Please sign in to comment.