Skip to content

Commit

Permalink
Add on_open parameter to Client.connect
Browse files Browse the repository at this point in the history
  • Loading branch information
Floris272 committed Mar 22, 2024
1 parent 9961d1a commit 8e9c1da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "bluecurrent-api"
version = "1.2.2"
version = "1.2.3"
authors = [
{ name="Floris272", email="[email protected]" },
]
Expand Down
8 changes: 4 additions & 4 deletions src/bluecurrent_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ async def get_email(self) -> str:
"""Get user email."""
return await self.websocket.get_email()

async def _on_open(self) -> None:
async def _on_open(self, on_open: Callable[[], Coroutine[Any, Any, None]],) -> None:
"""Send requests when connected."""
await self.websocket.send_request(
{
"command": "HELLO",
"header": "homeassistant",
}
)
await self.get_charge_cards()
await self.get_charge_points()
await on_open()

def get_next_reset_delta(self) -> timedelta:
"""Returns the timedelta until the websocket limits are reset."""
Expand All @@ -52,9 +51,10 @@ def get_next_reset_delta(self) -> timedelta:
async def connect(
self,
receiver: Callable[[dict[str, Any]], Coroutine[Any, Any, None]],
on_open: Callable[[], Coroutine[Any, Any, None]],
) -> None:
"""Connect to the websocket."""
await self.websocket.start(receiver, self._on_open)
await self.websocket.start(receiver, lambda : self._on_open(on_open))

async def disconnect(self) -> None:
"""Disconnect the websocket."""
Expand Down
6 changes: 5 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ async def test_on_open(mocker: MockerFixture):
)
client = Client()

await client._on_open()
async def on_open():
await client.get_charge_cards()
await client.get_charge_points()

await client._on_open(on_open)
test_send_request.assert_has_calls(
[
mocker.call(
Expand Down

0 comments on commit 8e9c1da

Please sign in to comment.