Skip to content

Commit

Permalink
Log charge point id for api request
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Jul 13, 2023
1 parent 079aa56 commit fa98706
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions custom_components/monta/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import asyncio
import socket
import logging
import json
from datetime import timedelta

import aiohttp
Expand Down Expand Up @@ -82,12 +81,10 @@ def __init__(
async def async_request_token(self) -> any:
"""Obtain access token with clientId and secret."""

params = {"clientId": self._client_id, "clientSecret": self._client_secret}

response_json = await self._api_wrapper(
path="auth/token",
method="post",
data=params,
data={"clientId": self._client_id, "clientSecret": self._client_secret},
)

return response_json
Expand Down Expand Up @@ -136,28 +133,32 @@ async def async_start_charge(self, charge_point_id: int) -> any:
"""Start a charge."""
access_token = await self.async_get_access_token()

_LOGGER.debug("Trying to start a charge on: %s", charge_point_id)

response = await self._api_wrapper(
method="post",
path="charges",
headers={"authorization": f"Bearer {access_token}"},
data={"chargePointId": charge_point_id},
)

_LOGGER.debug("Started a charge: %s", json.dumps(response))
_LOGGER.debug("Started a charge on: %s", charge_point_id)

return response

async def async_stop_charge(self, charge_id: int) -> any:
"""Start a charge."""
access_token = await self.async_get_access_token()

_LOGGER.debug("Trying to stop a charge with id: %s", charge_id)

response = await self._api_wrapper(
method="post",
path=f"charges/{charge_id}/stop",
headers={"authorization": f"Bearer {access_token}"},
)

_LOGGER.debug("Stopped charge: %s", json.dumps(response))
_LOGGER.debug("Stopped charge for chargeId: %s <%s>", charge_id, response)

return response

Expand All @@ -168,15 +169,12 @@ async def async_get_access_token(self) -> str:
await self.async_load_preferences()

if self._is_access_token_valid():
_LOGGER.debug("Token still valid, using it")
_LOGGER.debug("Access Token still valid, using it")
return self._prefs[STORAGE_ACCESS_TOKEN]

if self._is_refresh_token_valid():
_LOGGER.debug("Refresh Token still valid, using it")
params = {"refreshToken": self._prefs[STORAGE_REFRESH_TOKEN]}
_LOGGER.debug(
"Requesting access token with: %s",
json.dumps(params),
)

response_json = await self._api_wrapper(
path="auth/refresh",
Expand All @@ -193,6 +191,7 @@ async def async_get_access_token(self) -> str:

return response_json["accessToken"]

_LOGGER.debug("No token is valid, Requesting a new tokens")
return await self.async_authenticate()

def _filter_private_information(self, data):
Expand Down Expand Up @@ -309,7 +308,6 @@ def _is_access_token_valid(self):
if isinstance(expire_time, str):
expire_time = dt_util.parse_datetime(self._prefs[STORAGE_ACCESS_EXPIRE_TIME])


preemptive_expire_time = expire_time - timedelta(
seconds=PREEMPTIVE_REFRESH_TTL_IN_SECONDS
)
Expand Down

0 comments on commit fa98706

Please sign in to comment.