Skip to content

Commit

Permalink
Merge pull request #71 from epenet/fix-connect-issue
Browse files Browse the repository at this point in the history
Fix issue with extra events in open routine
  • Loading branch information
xchwarze authored Mar 7, 2022
2 parents dce89ec + fcbbba6 commit 00b7b85
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
15 changes: 11 additions & 4 deletions samsungtvws/async_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from . import connection, exceptions, helper
from .command import SamsungTVCommand, SamsungTVSleepCommand
from .event import MS_CHANNEL_CONNECT_EVENT
from .event import ED_EDENTV_UPDATE_EVENT, MS_CHANNEL_CONNECT_EVENT

_LOGGING = logging.getLogger(__name__)

Expand Down Expand Up @@ -68,13 +68,20 @@ async def open(self) -> WebSocketClientProtocol:
connect_kwargs["ssl"] = ssl_context
connection = await connect(url, open_timeout=self.timeout, **connect_kwargs)

response = helper.process_api_response(await connection.recv())
self._check_for_token(response)
event: Optional[str] = None
while event is None or event == ED_EDENTV_UPDATE_EVENT:
data = await connection.recv()
response = helper.process_api_response(data)
event = response.get("event", "*")
assert event
self._websocket_event(event, response)

if response["event"] != MS_CHANNEL_CONNECT_EVENT:
if event != MS_CHANNEL_CONNECT_EVENT:
await self.close()
raise exceptions.ConnectionFailure(response)

self._check_for_token(response)

self.connection = connection
return connection

Expand Down
15 changes: 11 additions & 4 deletions samsungtvws/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from . import exceptions, helper
from .command import SamsungTVCommand, SamsungTVSleepCommand
from .event import MS_CHANNEL_CONNECT_EVENT, MS_ERROR_EVENT
from .event import ED_EDENTV_UPDATE_EVENT, MS_CHANNEL_CONNECT_EVENT, MS_ERROR_EVENT

_LOGGING = logging.getLogger(__name__)

Expand Down Expand Up @@ -163,13 +163,20 @@ def open(self) -> websocket.WebSocket:
connection="Connection: Upgrade",
)

response = helper.process_api_response(connection.recv())
self._check_for_token(response)
event: Optional[str] = None
while event is None or event == ED_EDENTV_UPDATE_EVENT:
data = connection.recv()
response = helper.process_api_response(data)
event = response.get("event", "*")
assert event
self._websocket_event(event, response)

if response["event"] != MS_CHANNEL_CONNECT_EVENT:
if event != MS_CHANNEL_CONNECT_EVENT:
self.close()
raise exceptions.ConnectionFailure(response)

self._check_for_token(response)

self.connection = connection
return connection

Expand Down
12 changes: 12 additions & 0 deletions tests/test_async_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ async def test_connect(async_connection: Mock) -> None:
assert tv.token == 123456789


@pytest.mark.asyncio
async def test_connect_with_extra_event(async_connection: Mock) -> None:
"""Ensure simple data can be parsed."""
async_connection.recv = Mock(
side_effect=[ED_EDENTV_UPDATE_FUTURE, MS_CHANNEL_CONNECT_FUTURE]
)
async_connection.send = Mock(return_value=NONE_FUTURE)
tv = SamsungTVWSAsyncRemote("127.0.0.1")
await tv.start_listening()
assert tv.token == 123456789


@pytest.mark.asyncio
async def test_connection_failure(async_connection: Mock) -> None:
"""Ensure simple data can be parsed."""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .const import (
ED_APPS_LAUNCH_SAMPLE,
ED_EDENTV_UPDATE_SAMPLE,
ED_INSTALLED_APP_SAMPLE,
MS_CHANNEL_CONNECT_SAMPLE,
MS_ERROR_SAMPLE,
Expand All @@ -22,6 +23,16 @@ def test_connect(connection: Mock) -> None:
assert tv.token == 123456789


def test_connect_with_extra_event(connection: Mock) -> None:
"""Ensure simple data can be parsed."""
connection.recv = Mock(
side_effect=[ED_EDENTV_UPDATE_SAMPLE, MS_CHANNEL_CONNECT_SAMPLE]
)
tv = SamsungTVWS("127.0.0.1")
tv.open()
assert tv.token == 123456789


def test_connection_failure(connection: Mock) -> None:
"""Ensure simple data can be parsed."""
connection.recv = Mock(side_effect=[MS_ERROR_SAMPLE])
Expand Down

0 comments on commit 00b7b85

Please sign in to comment.