diff --git a/pyheos/heos.py b/pyheos/heos.py index 59d89b0..f4a8186 100644 --- a/pyheos/heos.py +++ b/pyheos/heos.py @@ -52,6 +52,9 @@ async def _handle_event(self, event: HeosResponse) -> bool: if event.command == const.EVENT_SOURCES_CHANGED \ and self._music_sources_loaded: await self.get_music_sources(refresh=True) + if event.command == const.EVENT_USER_CHANGED: + self._signed_in_username = event.get_message('un') \ + if event.has_message("signed_in") else None return True async def sign_in(self, username: str, password: str): diff --git a/tests/fixtures/event.user_changed.json b/tests/fixtures/event.user_changed.json deleted file mode 100644 index 72a1149..0000000 --- a/tests/fixtures/event.user_changed.json +++ /dev/null @@ -1 +0,0 @@ -{"heos": {"command": "event/user_changed", "message": "signed_out"}} \ No newline at end of file diff --git a/tests/fixtures/event.user_changed_signed_in.json b/tests/fixtures/event.user_changed_signed_in.json new file mode 100644 index 0000000..6bc8d9f --- /dev/null +++ b/tests/fixtures/event.user_changed_signed_in.json @@ -0,0 +1 @@ +{"heos": {"command": "event/user_changed", "message": "signed_in&un=example@example.com"}} \ No newline at end of file diff --git a/tests/fixtures/event.user_changed_signed_out.json b/tests/fixtures/event.user_changed_signed_out.json new file mode 100644 index 0000000..76459a9 --- /dev/null +++ b/tests/fixtures/event.user_changed_signed_out.json @@ -0,0 +1 @@ +{"heos": {"command": "event/user_changed", "message": "signed_out&un=Unknown"}} \ No newline at end of file diff --git a/tests/test_heos.py b/tests/test_heos.py index d502f79..35fddbd 100644 --- a/tests/test_heos.py +++ b/tests/test_heos.py @@ -624,7 +624,7 @@ async def handler(group_id: int, event: str): @pytest.mark.asyncio async def test_user_changed_event(mock_device, heos): - """Test user changed fires dispatcher.""" + """Test user changed fires dispatcher and updates logged in user.""" signal = asyncio.Event() async def handler(event: str): @@ -632,12 +632,20 @@ async def handler(event: str): signal.set() heos.dispatcher.connect(const.SIGNAL_CONTROLLER_EVENT, handler) - # Write event through mock device - event_to_raise = await get_fixture("event.user_changed") + # Test signed out event + event_to_raise = await get_fixture("event.user_changed_signed_out") await mock_device.write_event(event_to_raise) + await signal.wait() + assert not heos.is_signed_in + assert not heos.signed_in_username - # Wait until the signal is set + # Test signed in event + signal.clear() + event_to_raise = await get_fixture("event.user_changed_signed_in") + await mock_device.write_event(event_to_raise) await signal.wait() + assert heos.is_signed_in + assert heos.signed_in_username == "example@example.com" @pytest.mark.asyncio