Skip to content

Commit

Permalink
Merge pull request #13 from bluecurrent/websocket-refactor
Browse files Browse the repository at this point in the history
Websocket refactor
  • Loading branch information
Floris272 authored Feb 13, 2024
2 parents 70f6fd0 + 9754a92 commit c945686
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 530 deletions.
Binary file modified .coverage
Binary file not shown.
2 changes: 1 addition & 1 deletion .github/workflows/lint_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ sphinx:
# Optionally declare the Python requirements required to build your docs
python:
install:
- requirements: docs/requirements.txt
- requirements: requirements.txt
32 changes: 32 additions & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# readthedocs dependencies
alabaster==0.7.12
attrs==21.2.0
Babel==2.9.1
certifi==2021.10.8
charset-normalizer==2.0.7
colorama==0.4.4
docutils==0.17.1
idna==3.3
imagesize==1.3.0
Jinja2==3.0.3
markdown-it-py==1.1.0
MarkupSafe==2.1.4
matplotlib==3.8.2
mdit-py-plugins>=0.3.4
myst-parser==1
packaging==21.3
Pygments==2.10.0
pyparsing==3.0.6
pytz==2021.3
PyYAML==6.0
requests==2.26.0
snowballstemmer==2.2.0
Sphinx>=5
sphinx-rtd-theme==1.0.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
urllib3==1.26.7
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
[build-system]
requires = ["setuptools>=61.0"]
requires = ["setuptools>=69.0"]
build-backend = "setuptools.build_meta"

[project]
name = "bluecurrent-api"
version = "1.0.6"
version = "1.2.0"
authors = [
{ name="Floris272", email="[email protected]" },
]
description = "A wrapper for the Blue Current websocket api"
readme = "README.md"
license = {text = "MIT"}
requires-python = ">=3.9"
requires-python = ">=3.11"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
37 changes: 2 additions & 35 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
websockets
pytz
types-pytz

# dev dependencies
pylint
Expand All @@ -8,38 +9,4 @@ pytest-mock
pytest-cov
pytest-asyncio
coverage
build


# readthedocs dependencies
alabaster==0.7.12
attrs==21.2.0
Babel==2.9.1
certifi==2021.10.8
charset-normalizer==2.0.7
colorama==0.4.4
docutils==0.17.1
idna==3.3
imagesize==1.3.0
Jinja2==3.0.3
markdown-it-py==1.1.0
MarkupSafe==2.0.1
matplotlib==3.5.1
mdit-py-plugins==0.2.8
myst-parser==0.15.2
packaging==21.3
Pygments==2.10.0
pyparsing==3.0.6
pytz==2021.3
PyYAML==6.0
requests==2.26.0
snowballstemmer==2.2.0
Sphinx==4.3.0
sphinx-rtd-theme==1.0.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
urllib3==1.26.7
build
47 changes: 33 additions & 14 deletions src/bluecurrent_api/client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""Define an object to interact with the BlueCurrent websocket api."""
import logging
from datetime import timedelta
from typing import Any, Callable, Optional
from typing import Any, Optional
from collections.abc import Callable, Coroutine

from .utils import get_next_reset_delta
from .websocket import Websocket

LOGGER = logging.getLogger(__package__)
DELAY = 10


class Client:
"""Api Client for the BlueCurrent Websocket Api."""
Expand All @@ -13,37 +18,51 @@ def __init__(self) -> None:
"""Initialize the Client."""
self.websocket = Websocket()

def get_next_reset_delta(self) -> timedelta:
"""Returns the next reset delta"""
return get_next_reset_delta()
def is_connected(self) -> bool:
"""Return the connection status"""
return self.websocket.connected.is_set()

async def wait_for_response(self) -> None:
async def wait_for_charge_points(self) -> None:
"""Wait for next response."""
await self.websocket.get_receiver_event().wait()
await self.websocket.received_charge_points.wait()

async def validate_api_token(self, api_token: str) -> str:
"""Validate an api_token."""
"""Validate an api_token and return customer id."""
return await self.websocket.validate_api_token(api_token)

async def get_email(self) -> str:
"""Get user email."""
return await self.websocket.get_email()

async def connect(self, api_token: str) -> None:
"""Connect to the websocket."""
await self.websocket.connect(api_token)
async def _on_open(self) -> None:
"""Send requests when connected."""
await self.websocket.send_request(
{
"command": "HELLO",
"header": "homeassistant",
}
)
await self.get_charge_cards()
await self.get_charge_points()

async def start_loop(self, receiver: Callable[[dict[str, Any]], None]) -> None:
"""Start the receive loop."""
await self.websocket.loop(receiver)
def get_next_reset_delta(self) -> timedelta:
"""Returns the timedelta until the websocket limits are reset."""
return get_next_reset_delta()

async def connect(
self,
receiver: Callable[[dict[str, Any]], Coroutine[Any, Any, None]],
) -> None:
"""Connect to the websocket."""
await self.websocket.start(receiver, self._on_open)

async def disconnect(self) -> None:
"""Disconnect the websocket."""
await self.websocket.disconnect()

async def get_charge_cards(self) -> None:
"""Get the charge cards."""
await self.websocket.send_request({"command": "GET_CHARGE_CARDS"})
await self.websocket.send_request({"command": "GET_CHARGE_CARDS", "limit": 100})

async def get_charge_points(self) -> None:
"""Get the charge points."""
Expand Down
Empty file added src/bluecurrent_api/py.typed
Empty file.
Loading

0 comments on commit c945686

Please sign in to comment.