Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into cli_remote_start_stop
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Jun 12, 2024
2 parents 36b999c + 82e4f40 commit 5d8b98c
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 265 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ name: Tests
on: [push, pull_request]

jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
test:
runs-on: ubuntu-latest
needs: ruff
strategy:
matrix:
python-version:
Expand Down Expand Up @@ -35,4 +41,4 @@ jobs:
run: poetry install
- name: Pytest
run: |
poetry run pytest --isort --black --pylama
poetry run pytest
12 changes: 5 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/psf/black
rev: 23.7.0
- id: check-json
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- id: ruff
- id: ruff-format
20 changes: 10 additions & 10 deletions chargeamps/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
from .utils import datetime_field


class ChargeAmpsClient(metaclass=ABCMeta):
class ChargeAmpsClient(metaclass=ABCMeta): # noqa
pass


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargePointConnector(object):
class ChargePointConnector:
charge_point_id: str
connector_id: int
type: str


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargePoint(object):
class ChargePoint:
id: str
name: str
password: str
Expand All @@ -37,15 +37,15 @@ class ChargePoint(object):

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargePointMeasurement(object):
class ChargePointMeasurement:
phase: str
current: float
voltage: float


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargePointConnectorStatus(object):
class ChargePointConnectorStatus:
charge_point_id: str
connector_id: int
total_consumption_kwh: float
Expand All @@ -58,23 +58,23 @@ class ChargePointConnectorStatus(object):

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargePointStatus(object):
class ChargePointStatus:
id: str
status: str
connector_statuses: List[ChargePointConnectorStatus]


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=False)
class ChargePointSettings(object):
class ChargePointSettings:
id: str
dimmer: str
down_light: bool


@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=False)
class ChargePointConnectorSettings(object):
class ChargePointConnectorSettings:
charge_point_id: str
connector_id: int
mode: str
Expand All @@ -85,7 +85,7 @@ class ChargePointConnectorSettings(object):

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class ChargingSession(object):
class ChargingSession:
id: str
charge_point_id: str
connector_id: int
Expand All @@ -97,7 +97,7 @@ class ChargingSession(object):

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass(frozen=True)
class StartAuth(object):
class StartAuth:
rfid_length: int
rfid_format: str
rfid: str
Expand Down
26 changes: 16 additions & 10 deletions chargeamps/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ async def get_chargepoint_id(client: ChargeAmpsClient, args: argparse.Namespace)
return chargepoints[0].id


async def command_list_chargepoints(client: ChargeAmpsClient, args: argparse.Namespace):
async def command_list_chargepoints(
client: ChargeAmpsClient, args: argparse.Namespace
) -> None:
res = []
for cp in await client.get_chargepoints():
res.append(cp.to_dict())
Expand All @@ -38,7 +40,7 @@ async def command_list_chargepoints(client: ChargeAmpsClient, args: argparse.Nam

async def command_get_chargepoint_status(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
cp = await client.get_chargepoint_status(charge_point_id)
if args.connector_id:
Expand All @@ -51,7 +53,7 @@ async def command_get_chargepoint_status(

async def command_get_chargepoint_sessions(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
if args.session:
session = await client.get_chargingsession(charge_point_id, args.session)
Expand All @@ -74,15 +76,15 @@ async def command_get_chargepoint_sessions(

async def command_get_chargepoint_settings(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
settings = await client.get_chargepoint_settings(charge_point_id)
print(json.dumps(settings.to_dict(), indent=4))


async def command_set_chargepoint_settings(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
settings = await client.get_chargepoint_settings(charge_point_id)
if args.dimmer:
Expand All @@ -96,7 +98,7 @@ async def command_set_chargepoint_settings(

async def command_get_connector_settings(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
if args.connector_id:
connector_ids = [args.connector_id]
Expand All @@ -114,7 +116,7 @@ async def command_get_connector_settings(

async def command_set_connector_settings(
client: ChargeAmpsClient, args: argparse.Namespace
):
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
connector_id = args.connector_id
settings = await client.get_chargepoint_connector_settings(
Expand All @@ -135,7 +137,9 @@ async def command_set_connector_settings(
print(json.dumps(settings.to_dict(), indent=4))


async def command_remote_start(client: ChargeAmpsClient, args: argparse.Namespace):
async def command_remote_start(
client: ChargeAmpsClient, args: argparse.Namespace
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
connector_id = args.connector_id
start_auth = StartAuth(
Expand All @@ -147,7 +151,9 @@ async def command_remote_start(client: ChargeAmpsClient, args: argparse.Namespac
await client.remote_start(charge_point_id, connector_id, start_auth)


async def command_remote_stop(client: ChargeAmpsClient, args: argparse.Namespace):
async def command_remote_stop(
client: ChargeAmpsClient, args: argparse.Namespace
) -> None:
charge_point_id = await get_chargepoint_id(client, args)
connector_id = args.connector_id
await client.remote_stop(charge_point_id, connector_id)
Expand All @@ -164,7 +170,7 @@ def add_arg_chargepoint(parser, required=False):
)


def add_arg_connector(parser, required=False):
def add_arg_connector(parser, required=False) -> None:
parser.add_argument(
"--connector",
dest="connector_id",
Expand Down
16 changes: 8 additions & 8 deletions chargeamps/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import asyncio # noqa
import time
from datetime import datetime
from typing import List, Optional
from typing import Optional
from urllib.parse import urljoin

import aiohttp
import jwt
from aiohttp import ClientResponse, ClientSession

from .base import (
ChargeAmpsClient,
Expand All @@ -34,7 +34,7 @@ def __init__(
self._email = email
self._password = password
self._api_key = api_key
self._session = aiohttp.ClientSession(raise_for_status=True)
self._session = ClientSession(raise_for_status=True)
self._headers = {}
self._base_url = api_base_url or API_BASE_URL
self._ssl = False
Expand All @@ -57,28 +57,28 @@ async def _ensure_token(self):
self._token_expire = token_payload.get("exp", 0)
self._headers["Authorization"] = f"Bearer {self._token}"

async def _post(self, path, **kwargs):
async def _post(self, path, **kwargs) -> ClientResponse:
await self._ensure_token()
headers = kwargs.pop("headers", self._headers)
return await self._session.post(
urljoin(self._base_url, path), ssl=self._ssl, headers=headers, **kwargs
)

async def _get(self, path, **kwargs):
async def _get(self, path, **kwargs) -> ClientResponse:
await self._ensure_token()
headers = kwargs.pop("headers", self._headers)
return await self._session.get(
urljoin(self._base_url, path), ssl=self._ssl, headers=headers, **kwargs
)

async def _put(self, path, **kwargs):
async def _put(self, path, **kwargs) -> ClientResponse:
await self._ensure_token()
headers = kwargs.pop("headers", self._headers)
return await self._session.put(
urljoin(self._base_url, path), ssl=self._ssl, headers=headers, **kwargs
)

async def get_chargepoints(self) -> List[ChargePoint]:
async def get_chargepoints(self) -> list[ChargePoint]:
"""Get all owned chargepoints"""
request_uri = f"/api/{API_VERSION}/chargepoints/owned"
response = await self._get(request_uri)
Expand All @@ -92,7 +92,7 @@ async def get_all_chargingsessions(
charge_point_id: str,
start_time: Optional[datetime] = None,
end_time: Optional[datetime] = None,
) -> List[ChargingSession]:
) -> list[ChargingSession]:
"""Get all charging sessions"""
query_params = {}
if start_time:
Expand Down
Loading

0 comments on commit 5d8b98c

Please sign in to comment.