podpointclient 1.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- podpointclient/__init__.py +0 -0
- podpointclient/charge.py +130 -0
- podpointclient/charge_mode.py +7 -0
- podpointclient/charge_override.py +42 -0
- podpointclient/client.py +419 -0
- podpointclient/connectivity_status.py +206 -0
- podpointclient/endpoints.py +35 -0
- podpointclient/errors.py +42 -0
- podpointclient/factories.py +108 -0
- podpointclient/helpers/__init__.py +1 -0
- podpointclient/helpers/api_wrapper.py +197 -0
- podpointclient/helpers/auth.py +276 -0
- podpointclient/helpers/functions.py +42 -0
- podpointclient/helpers/session.py +58 -0
- podpointclient/pod.py +453 -0
- podpointclient/schedule.py +48 -0
- podpointclient/user.py +255 -0
- podpointclient/version.py +3 -0
- podpointclient-1.7.0.dist-info/METADATA +129 -0
- podpointclient-1.7.0.dist-info/RECORD +23 -0
- podpointclient-1.7.0.dist-info/WHEEL +5 -0
- podpointclient-1.7.0.dist-info/licenses/LICENSE +21 -0
- podpointclient-1.7.0.dist-info/top_level.txt +1 -0
|
File without changes
|
podpointclient/charge.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Charge class, represents a 'Charge' from the podpoint apis"""
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import Dict, Any, List
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from .helpers.functions import lazy_convert_to_datetime
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class ChargeDurationFormat:
|
|
10
|
+
"""Representation of Format within Duration within Charge from pod point"""
|
|
11
|
+
value: str = None
|
|
12
|
+
unit: str = None
|
|
13
|
+
|
|
14
|
+
def __str__(self) -> str:
|
|
15
|
+
return " ".join(list(filter(None, [self.value, self.unit])))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Charge:
|
|
19
|
+
"""Representation of a Charge from pod point"""
|
|
20
|
+
def __init__(self, data: Dict[str, Any]):
|
|
21
|
+
self.id: int = data.get('id', None)
|
|
22
|
+
self.kwh_used: float = data.get('kwh_used', 0.0)
|
|
23
|
+
self.duration: int = data.get('duration', 0)
|
|
24
|
+
self.starts_at: datetime = lazy_convert_to_datetime(data.get('starts_at', None))
|
|
25
|
+
self.ends_at: datetime = lazy_convert_to_datetime(data.get('ends_at', None))
|
|
26
|
+
self.energy_cost: int = data.get('energy_cost', 0)
|
|
27
|
+
|
|
28
|
+
charging_duration_data = data.get('charging_duration', {})
|
|
29
|
+
self.charging_duration = self.ChargingDuration(
|
|
30
|
+
raw = charging_duration_data.get('raw', None),
|
|
31
|
+
formatted = charging_duration_data.get('formatted', [])
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
billing_event_data = data.get('billing_event', {})
|
|
35
|
+
self.billing_event = self.BillingEvent(
|
|
36
|
+
id = billing_event_data.get('id', None),
|
|
37
|
+
amount = billing_event_data.get('amount', None),
|
|
38
|
+
currency = billing_event_data.get('currency', None),
|
|
39
|
+
exchange_rate = billing_event_data.get('exchange_rate', 0),
|
|
40
|
+
presentment_amount = billing_event_data.get('presentment_amount', None),
|
|
41
|
+
presentment_currency = billing_event_data.get('presentment_currency', None)
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
location_data = data.get('location', {})
|
|
45
|
+
self.location = self.Location(data=location_data)
|
|
46
|
+
|
|
47
|
+
pod_data = data.get('pod', {})
|
|
48
|
+
self.pod = self.Pod(id=pod_data.get('id', None))
|
|
49
|
+
|
|
50
|
+
organisation_data = data.get('organisation', {})
|
|
51
|
+
self.organisation = self.Organisation(
|
|
52
|
+
id = organisation_data.get('id', None),
|
|
53
|
+
name = organisation_data.get('name', None)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def home(self) -> bool:
|
|
58
|
+
"""Is this a 'home' charge?"""
|
|
59
|
+
return self.location.home
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass
|
|
63
|
+
class ChargingDuration:
|
|
64
|
+
"""Representation of a Duration within a Charge from pod point"""
|
|
65
|
+
raw: int = None
|
|
66
|
+
formatted: 'list[ChargeDurationFormat]' = field(default_factory=list)
|
|
67
|
+
|
|
68
|
+
def __init__(self, raw: int, formatted: List[Dict[str,str]]) -> None:
|
|
69
|
+
self.raw = raw
|
|
70
|
+
self.formatted: List[ChargeDurationFormat] = []
|
|
71
|
+
|
|
72
|
+
if formatted is not None and len(formatted) > 0:
|
|
73
|
+
for formatted_data in formatted:
|
|
74
|
+
self.formatted.append(
|
|
75
|
+
ChargeDurationFormat(
|
|
76
|
+
value = formatted_data.get("value", None),
|
|
77
|
+
unit = formatted_data.get("unit", None)
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def __str__(self) -> str:
|
|
82
|
+
def dt_to_str(date_time: datetime) -> str:
|
|
83
|
+
return str(date_time)
|
|
84
|
+
|
|
85
|
+
return " ".join(list(filter(None, map(dt_to_str ,self.formatted))))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass
|
|
89
|
+
class BillingEvent:
|
|
90
|
+
"""Represents a Billing Event from pod point"""
|
|
91
|
+
id: int = None
|
|
92
|
+
amount: Any = None
|
|
93
|
+
currency: Any = None
|
|
94
|
+
exchange_rate: int = 0
|
|
95
|
+
presentment_amount: Any = None
|
|
96
|
+
presentment_currency: Any = None
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@dataclass
|
|
100
|
+
class Location:
|
|
101
|
+
"""Represents a Location within a charge from pod point"""
|
|
102
|
+
def __init__(self, data: Dict[str, Any]):
|
|
103
|
+
self.id = data.get('id', None)
|
|
104
|
+
self.home = data.get('home', None)
|
|
105
|
+
self.timezone = data.get('timezone', None)
|
|
106
|
+
|
|
107
|
+
address_data = data.get('address', {})
|
|
108
|
+
self.address = self.Address(
|
|
109
|
+
id=address_data.get('id', None),
|
|
110
|
+
business_name=address_data.get('business_name', "")
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@dataclass
|
|
115
|
+
class Address:
|
|
116
|
+
"""Represents an address within a Location within a Charge from Pod Point"""
|
|
117
|
+
id: int = None
|
|
118
|
+
business_name: str = ""
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@dataclass
|
|
122
|
+
class Pod:
|
|
123
|
+
"""Represents a Pod within a Charge from pod point"""
|
|
124
|
+
id: int = None
|
|
125
|
+
|
|
126
|
+
@dataclass
|
|
127
|
+
class Organisation:
|
|
128
|
+
"""Repreents an Organisation within a Charge from pod point"""
|
|
129
|
+
id: int = None
|
|
130
|
+
name: str = None
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Charge Override class, represents a 'Charge Override' from the podpoint apis"""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from datetime import datetime, timedelta
|
|
5
|
+
from typing import Dict, Any, List
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from .helpers.functions import lazy_convert_to_datetime, lazy_iso_format_datetime
|
|
8
|
+
|
|
9
|
+
class ChargeOverride:
|
|
10
|
+
"""Representation of a Charge Override from pod point"""
|
|
11
|
+
def __init__(self, data: Dict[str, Any]):
|
|
12
|
+
self.ppid: int = data.get('ppid', None)
|
|
13
|
+
self.requested_at: datetime = lazy_convert_to_datetime(data.get('requested_at', None))
|
|
14
|
+
self.received_at: datetime = lazy_convert_to_datetime(data.get('received_at', None))
|
|
15
|
+
self.ends_at: datetime = lazy_convert_to_datetime(data.get('ends_at', None))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def dict(self) -> Dict[str, Any]:
|
|
20
|
+
return {
|
|
21
|
+
"ppid": self.ppid,
|
|
22
|
+
"requested_at": lazy_iso_format_datetime(self.requested_at),
|
|
23
|
+
"received_at": lazy_iso_format_datetime(self.received_at),
|
|
24
|
+
"ends_at": lazy_iso_format_datetime(self.ends_at)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
def to_json(self):
|
|
28
|
+
"""JSON representation of a ChargeOverride object"""
|
|
29
|
+
return json.dumps(self.dict, ensure_ascii=False)
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def active(self) -> bool:
|
|
33
|
+
"""Is the charge override active"""
|
|
34
|
+
return (self.ends_at is not None and self.ends_at > datetime.now(self.ends_at.tzinfo))
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def remaining_time(self) -> timedelta:
|
|
38
|
+
"""How long is left for the charge override"""
|
|
39
|
+
if self.active is False:
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
return self.ends_at - datetime.now(self.ends_at.tzinfo)
|
podpointclient/client.py
ADDED
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
"""PodPoint Basic API Client."""
|
|
2
|
+
import logging
|
|
3
|
+
from typing import Callable, Dict, Any, List, Union
|
|
4
|
+
from datetime import datetime, timedelta
|
|
5
|
+
|
|
6
|
+
import aiohttp
|
|
7
|
+
|
|
8
|
+
from .endpoints import API_BASE_URL, CHARGE_SCHEDULES, PODS, UNITS, USERS, CHARGES, FIRMWARE, AUTH, CHARGE_OVERRIDE, CHARGERS, CONNECTIVITY_STATUS, MOBILE_API_BASE_URL
|
|
9
|
+
from .helpers.auth import Auth
|
|
10
|
+
from .helpers.functions import auth_headers
|
|
11
|
+
from .helpers.api_wrapper import APIWrapper
|
|
12
|
+
from .factories import PodFactory, ScheduleFactory, ChargeFactory, FirmwareFactory, UserFactory, ChargeOverrideFactory, ConnectivityStatusFactory
|
|
13
|
+
from .pod import Pod, Firmware
|
|
14
|
+
from .charge import Charge
|
|
15
|
+
from .charge_mode import ChargeMode
|
|
16
|
+
from .charge_override import ChargeOverride
|
|
17
|
+
from .connectivity_status import ConnectivityStatus
|
|
18
|
+
from .schedule import Schedule
|
|
19
|
+
from .user import User
|
|
20
|
+
from .errors import ChargeOverrideValidationError
|
|
21
|
+
|
|
22
|
+
TIMEOUT = 10
|
|
23
|
+
|
|
24
|
+
_LOGGER: logging.Logger = logging.getLogger(__package__)
|
|
25
|
+
|
|
26
|
+
HEADERS = {"Content-type": "application/json; charset=UTF-8"}
|
|
27
|
+
DEFAULT_POD_INCLUDES = ["statuses", "price", "model",
|
|
28
|
+
"unit_connectors", "charge_schedules", "charge_override"]
|
|
29
|
+
DEFAULT_USER_INCLUDES = ["account", "vehicle", "vehicle.make", "unit.pod.unit_connectors", "unit.pod.statuses", "unit.pod.model", "unit.pod.charge_schedules", "unit.pod.charge_override"]
|
|
30
|
+
|
|
31
|
+
class PodPointClient:
|
|
32
|
+
"""API Client for communicating with Pod Point."""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
username: str,
|
|
37
|
+
password: str,
|
|
38
|
+
session: aiohttp.ClientSession = None,
|
|
39
|
+
include_timestamp: bool = False,
|
|
40
|
+
http_debug: bool = None,
|
|
41
|
+
refresh_token: str = None,
|
|
42
|
+
on_refresh_token: Callable[[str], None] = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
"""Pod Point API Client."""
|
|
45
|
+
self.email = username
|
|
46
|
+
self.password = password
|
|
47
|
+
self._session = session if session is not None else aiohttp.ClientSession()
|
|
48
|
+
self._http_debug = http_debug if http_debug is not None else False
|
|
49
|
+
self.auth = Auth(
|
|
50
|
+
email=self.email,
|
|
51
|
+
password=self.password,
|
|
52
|
+
session=self._session,
|
|
53
|
+
http_debug=self._http_debug,
|
|
54
|
+
refresh_token=refresh_token,
|
|
55
|
+
on_refresh_token=on_refresh_token,
|
|
56
|
+
)
|
|
57
|
+
self.api_wrapper = APIWrapper(session=self._session)
|
|
58
|
+
self.include_timestamp = include_timestamp
|
|
59
|
+
|
|
60
|
+
async def async_credentials_verified(self) -> bool:
|
|
61
|
+
"""Perform a minimum call to verify we have working credentials and can get one Pod"""
|
|
62
|
+
await self.auth.async_update_access_token()
|
|
63
|
+
|
|
64
|
+
pods = await self.async_get_pods(perpage=1, page=1, includes=[])
|
|
65
|
+
return len(pods) > 0
|
|
66
|
+
|
|
67
|
+
async def async_get_all_pods(
|
|
68
|
+
self,
|
|
69
|
+
perpage: Union[str, int] = 5,
|
|
70
|
+
includes: Union[List[str], None] = None
|
|
71
|
+
) -> List[Pod]:
|
|
72
|
+
"""Get all pods from the API"""
|
|
73
|
+
page = 1
|
|
74
|
+
pods: List[Pod] = []
|
|
75
|
+
|
|
76
|
+
more_pods = True
|
|
77
|
+
while more_pods:
|
|
78
|
+
new_pods: List[Pod] = await self.async_get_pods(
|
|
79
|
+
perpage=perpage,
|
|
80
|
+
page=page,
|
|
81
|
+
includes=includes
|
|
82
|
+
)
|
|
83
|
+
# Should be replaced by reading "meta > pagination > page_count" but
|
|
84
|
+
# would require a larger refactor
|
|
85
|
+
if len(new_pods) < perpage:
|
|
86
|
+
more_pods = False
|
|
87
|
+
|
|
88
|
+
pods.extend(new_pods)
|
|
89
|
+
page += 1
|
|
90
|
+
|
|
91
|
+
return pods
|
|
92
|
+
|
|
93
|
+
async def async_get_pods(
|
|
94
|
+
self,
|
|
95
|
+
perpage: Union[str, int] = 5,
|
|
96
|
+
page: Union[str, int] = 1,
|
|
97
|
+
includes: Union[List[str], None] = None
|
|
98
|
+
) -> List[Pod]:
|
|
99
|
+
"""Get pods from the API"""
|
|
100
|
+
await self.auth.async_update_access_token()
|
|
101
|
+
|
|
102
|
+
if includes is None:
|
|
103
|
+
includes = DEFAULT_POD_INCLUDES
|
|
104
|
+
|
|
105
|
+
params = {"perpage": perpage, "page": page}
|
|
106
|
+
if len(includes) > 0:
|
|
107
|
+
params["include"] = ",".join(includes)
|
|
108
|
+
|
|
109
|
+
response = await self.api_wrapper.get(
|
|
110
|
+
url=self._url_from_path(path=f"{USERS}/{self.auth.user_id}{PODS}"),
|
|
111
|
+
params=self._generate_complete_params(params=params),
|
|
112
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
json = await self._handle_json_response(response=response)
|
|
116
|
+
|
|
117
|
+
pods = PodFactory().build_pods(pods_response=json)
|
|
118
|
+
|
|
119
|
+
return pods
|
|
120
|
+
|
|
121
|
+
async def async_get_pod(self, pod_id: int) -> Pod:
|
|
122
|
+
"""Get specific pod from the API"""
|
|
123
|
+
pods = await self.async_get_all_pods()
|
|
124
|
+
return next((pod for pod in pods if pod.id == pod_id), None)
|
|
125
|
+
|
|
126
|
+
async def async_set_schedule(self, enabled: bool, pod: Pod) -> bool:
|
|
127
|
+
"""Send data from the API."""
|
|
128
|
+
await self.auth.async_update_access_token()
|
|
129
|
+
|
|
130
|
+
unit_id = pod.unit_id
|
|
131
|
+
|
|
132
|
+
_LOGGER.debug(
|
|
133
|
+
"Updating pod schedule for unit %s. Enabling schedule: %s",
|
|
134
|
+
unit_id,
|
|
135
|
+
enabled
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
response = await self.api_wrapper.put(
|
|
139
|
+
url=self._url_from_path(
|
|
140
|
+
path=f"{UNITS}/{unit_id}{CHARGE_SCHEDULES}"),
|
|
141
|
+
params=self._generate_complete_params(params=None),
|
|
142
|
+
headers=auth_headers(access_token=self.auth.access_token),
|
|
143
|
+
body=self._schedule_data(enabled=enabled, pod=pod)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# Quick exit if the response code is 201
|
|
147
|
+
if response.status == 201:
|
|
148
|
+
return True
|
|
149
|
+
|
|
150
|
+
text = await response.text()
|
|
151
|
+
_LOGGER.warning(
|
|
152
|
+
"Expected to recieve 201 status code when creating schedules. Got (%s) - %s",
|
|
153
|
+
response.status,
|
|
154
|
+
text
|
|
155
|
+
)
|
|
156
|
+
return False
|
|
157
|
+
|
|
158
|
+
async def async_get_all_charges(
|
|
159
|
+
self,
|
|
160
|
+
perpage: Union[str, int] = 50
|
|
161
|
+
) -> List[Charge]:
|
|
162
|
+
"""Get all charges from the API"""
|
|
163
|
+
page = 1
|
|
164
|
+
charges: List[Charge] = []
|
|
165
|
+
|
|
166
|
+
more_charges = True
|
|
167
|
+
while more_charges:
|
|
168
|
+
new_charges: List[Charge] = await self.async_get_charges(perpage=perpage, page=page)
|
|
169
|
+
# Should be replaced by reading "meta > pagination > page_count" but
|
|
170
|
+
# would require a larger refactor
|
|
171
|
+
if len(new_charges) < perpage:
|
|
172
|
+
more_charges = False
|
|
173
|
+
|
|
174
|
+
charges.extend(new_charges)
|
|
175
|
+
page += 1
|
|
176
|
+
|
|
177
|
+
return charges
|
|
178
|
+
|
|
179
|
+
async def async_get_charges(
|
|
180
|
+
self,
|
|
181
|
+
perpage: Union[str, int] = 5,
|
|
182
|
+
page: Union[str, int] = 1
|
|
183
|
+
) -> List[Charge]:
|
|
184
|
+
"""Get charges from the API."""
|
|
185
|
+
await self.auth.async_update_access_token()
|
|
186
|
+
|
|
187
|
+
response = await self.api_wrapper.get(
|
|
188
|
+
url=self._url_from_path(
|
|
189
|
+
path=f"{USERS}/{self.auth.user_id}{CHARGES}"),
|
|
190
|
+
params=self._generate_complete_params(
|
|
191
|
+
params={"perpage": perpage, "page": page}),
|
|
192
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
json = await self._handle_json_response(response=response)
|
|
196
|
+
|
|
197
|
+
charges = ChargeFactory().build_charges(charge_response=json)
|
|
198
|
+
|
|
199
|
+
return charges
|
|
200
|
+
|
|
201
|
+
async def async_get_firmware(self, pod: Pod) -> List[Firmware]:
|
|
202
|
+
"""Get firmware information for a given unit."""
|
|
203
|
+
await self.auth.async_update_access_token()
|
|
204
|
+
|
|
205
|
+
response = await self.api_wrapper.get(
|
|
206
|
+
url=self._url_from_path(
|
|
207
|
+
path=f"{UNITS}/{pod.unit_id}{FIRMWARE}"),
|
|
208
|
+
params=self._generate_complete_params(params=None),
|
|
209
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
json = await self._handle_json_response(response=response)
|
|
213
|
+
|
|
214
|
+
firmwares = FirmwareFactory().build_firmwares(firmware_response=json)
|
|
215
|
+
|
|
216
|
+
return firmwares
|
|
217
|
+
|
|
218
|
+
async def async_get_user(self, includes: Union[List[str], None] = None) -> User:
|
|
219
|
+
"""Get user from the API"""
|
|
220
|
+
await self.auth.async_update_access_token()
|
|
221
|
+
|
|
222
|
+
if includes is None:
|
|
223
|
+
includes = DEFAULT_USER_INCLUDES
|
|
224
|
+
|
|
225
|
+
params = {}
|
|
226
|
+
if len(includes) > 0:
|
|
227
|
+
params["include"] = ",".join(includes)
|
|
228
|
+
|
|
229
|
+
response = await self.api_wrapper.get(
|
|
230
|
+
url=self._url_from_path(path=f"{AUTH}"),
|
|
231
|
+
params=self._generate_complete_params(params=params),
|
|
232
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
json = await self._handle_json_response(response=response)
|
|
236
|
+
|
|
237
|
+
user = UserFactory().build_user(user_response=json)
|
|
238
|
+
|
|
239
|
+
return user
|
|
240
|
+
|
|
241
|
+
async def async_get_charge_override(self, pod: Pod) -> Union[None, ChargeOverride]:
|
|
242
|
+
await self.auth.async_update_access_token()
|
|
243
|
+
|
|
244
|
+
response = await self.api_wrapper.get(
|
|
245
|
+
url=self._url_from_path(
|
|
246
|
+
path=f"{UNITS}/{pod.unit_id}{CHARGE_OVERRIDE}"),
|
|
247
|
+
params=self._generate_complete_params(params=None),
|
|
248
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
# If there is no charge mode (smart mode), return None
|
|
252
|
+
if response.status == 204:
|
|
253
|
+
return None
|
|
254
|
+
|
|
255
|
+
json = await self._handle_json_response(response=response)
|
|
256
|
+
|
|
257
|
+
return ChargeOverrideFactory().build_charge_override(charge_override_response=json)
|
|
258
|
+
|
|
259
|
+
async def async_delete_charge_override(self, pod:Pod) -> bool:
|
|
260
|
+
await self.auth.async_update_access_token()
|
|
261
|
+
|
|
262
|
+
response = await self.api_wrapper.delete(
|
|
263
|
+
url=self._url_from_path(
|
|
264
|
+
path=f"{UNITS}/{pod.unit_id}{CHARGE_OVERRIDE}"),
|
|
265
|
+
params=self._generate_complete_params(params=None),
|
|
266
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
return response.status == 204
|
|
270
|
+
|
|
271
|
+
async def async_get_connectivity_status(self, pod:Pod) -> ConnectivityStatus:
|
|
272
|
+
await self.auth.async_update_access_token()
|
|
273
|
+
|
|
274
|
+
response = await self.api_wrapper.get(
|
|
275
|
+
url=self._url_from_path(
|
|
276
|
+
path=f"{CHARGERS}/{pod.ppid}{CONNECTIVITY_STATUS}",
|
|
277
|
+
base=MOBILE_API_BASE_URL
|
|
278
|
+
),
|
|
279
|
+
params=self._generate_complete_params(params=None),
|
|
280
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
json = await self._handle_json_response(response=response)
|
|
284
|
+
|
|
285
|
+
return ConnectivityStatusFactory().build_connectivity_status(connectivity_status_response=json)
|
|
286
|
+
|
|
287
|
+
async def async_set_charge_override(self, pod:Pod, hours:int=0, minutes:int=0, seconds:int=0) -> ChargeOverride:
|
|
288
|
+
await self.auth.async_update_access_token()
|
|
289
|
+
|
|
290
|
+
valid_hours = (hours is not None and type(hours) is int and hours >= 0)
|
|
291
|
+
valid_minutes = (minutes is not None and type(minutes) is int and minutes >= 0)
|
|
292
|
+
valid_seconds = (seconds is not None and type(seconds) is int and seconds >= 0)
|
|
293
|
+
valid = (
|
|
294
|
+
valid_hours
|
|
295
|
+
and valid_minutes
|
|
296
|
+
and valid_seconds
|
|
297
|
+
and (
|
|
298
|
+
hours > 0
|
|
299
|
+
or minutes > 0
|
|
300
|
+
or seconds > 0
|
|
301
|
+
)
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
if valid is False:
|
|
305
|
+
raise ChargeOverrideValidationError()
|
|
306
|
+
|
|
307
|
+
now = datetime.now().astimezone()
|
|
308
|
+
ends_at = now + timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
309
|
+
datetime_format_string = "%Y-%m-%dT%H:%M:%S%z"
|
|
310
|
+
|
|
311
|
+
body = {
|
|
312
|
+
"requested_at": now.strftime(datetime_format_string),
|
|
313
|
+
"ends_at": ends_at.strftime(datetime_format_string)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
response = await self.api_wrapper.put(
|
|
317
|
+
url=self._url_from_path(
|
|
318
|
+
path=f"{UNITS}/{pod.unit_id}{CHARGE_OVERRIDE}"),
|
|
319
|
+
params=self._generate_complete_params(params=None),
|
|
320
|
+
body=body,
|
|
321
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
json = await self._handle_json_response(response=response)
|
|
325
|
+
|
|
326
|
+
return ChargeOverrideFactory().build_charge_override(charge_override_response=json)
|
|
327
|
+
|
|
328
|
+
async def async_set_charge_mode_manual(self, pod) -> bool:
|
|
329
|
+
"""Set user's pod into 'manual' charge mode"""
|
|
330
|
+
await self.auth.async_update_access_token()
|
|
331
|
+
|
|
332
|
+
body = {
|
|
333
|
+
"requested_at": datetime.now().astimezone().strftime("%Y-%m-%dT%H:%M:%S%z") #2023-04-25T09:35:34+01:00
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
response = await self._async_set_charge_mode(pod, body)
|
|
337
|
+
|
|
338
|
+
expected_response = (
|
|
339
|
+
response.ppid == pod.ppid
|
|
340
|
+
and response.requested_at is not None
|
|
341
|
+
and response.received_at is not None
|
|
342
|
+
and response.ends_at is None)
|
|
343
|
+
|
|
344
|
+
return expected_response
|
|
345
|
+
|
|
346
|
+
async def async_set_charge_mode_smart(self, pod) -> bool:
|
|
347
|
+
"""Set the user's pod into 'smart' charge mode"""
|
|
348
|
+
await self.auth.async_update_access_token()
|
|
349
|
+
|
|
350
|
+
response = await self.api_wrapper.delete(
|
|
351
|
+
url=self._url_from_path(
|
|
352
|
+
path=f"{UNITS}/{pod.unit_id}{CHARGE_OVERRIDE}"
|
|
353
|
+
),
|
|
354
|
+
params=self._generate_complete_params(params=None),
|
|
355
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
return response.status == 204
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
async def _async_set_charge_mode(self, pod, body) -> ChargeMode:
|
|
362
|
+
"""Given a body object, set the charge mode for a user's pod"""
|
|
363
|
+
response = await self.api_wrapper.put(
|
|
364
|
+
url=self._url_from_path(
|
|
365
|
+
path=f"{UNITS}/{pod.unit_id}{CHARGE_OVERRIDE}"),
|
|
366
|
+
params=self._generate_complete_params(params=None),
|
|
367
|
+
body=body,
|
|
368
|
+
headers=auth_headers(access_token=self.auth.access_token)
|
|
369
|
+
)
|
|
370
|
+
|
|
371
|
+
json = await self._handle_json_response(response=response)
|
|
372
|
+
|
|
373
|
+
return ChargeOverrideFactory().build_charge_override(charge_override_response=json)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def _schedule_data(self, enabled: bool, pod: Pod = None) -> Dict[str, Any]:
|
|
377
|
+
"""Generate a new schedule body with all the enable attributes set to the `enabled` value"""
|
|
378
|
+
times = {}
|
|
379
|
+
day_offset = 0
|
|
380
|
+
if pod and pod.charge_schedules:
|
|
381
|
+
current = pod.charge_schedules[0]
|
|
382
|
+
if current.start_time and current.end_time:
|
|
383
|
+
times = {"start_time": current.start_time, "end_time": current.end_time}
|
|
384
|
+
if current.start_day and current.end_day:
|
|
385
|
+
# Keep overnight windows ending on the following day
|
|
386
|
+
day_offset = (current.end_day - current.start_day) % 7
|
|
387
|
+
schedules: List[Schedule] = ScheduleFactory(
|
|
388
|
+
).build_schedules(enabled=enabled, **times)
|
|
389
|
+
for schedule in schedules:
|
|
390
|
+
schedule.end_day = (schedule.start_day - 1 + day_offset) % 7 + 1
|
|
391
|
+
|
|
392
|
+
d_list = list(map(lambda schedule: schedule.dict, schedules))
|
|
393
|
+
|
|
394
|
+
return {"data": d_list}
|
|
395
|
+
|
|
396
|
+
def _url_from_path(self, path: str, base: str = API_BASE_URL) -> str:
|
|
397
|
+
"""Given a path, return a complete API URL"""
|
|
398
|
+
return f"{base}{path}"
|
|
399
|
+
|
|
400
|
+
def _generate_complete_params(self, params: Union[None, Dict[str, Any]]) -> Dict[str, any]:
|
|
401
|
+
"""Given a params object, add optional params if required"""
|
|
402
|
+
if not self.include_timestamp:
|
|
403
|
+
return params
|
|
404
|
+
|
|
405
|
+
if params is None:
|
|
406
|
+
params = {}
|
|
407
|
+
|
|
408
|
+
params["timestamp"] = datetime.now().astimezone().timestamp()
|
|
409
|
+
return params
|
|
410
|
+
|
|
411
|
+
async def _handle_json_response(self, response: aiohttp.ClientResponse) -> Dict[str, any]:
|
|
412
|
+
"""Given a Coroutine (assuming a response from ApiWrapper), await calling
|
|
413
|
+
json() and if needed, debug log the response"""
|
|
414
|
+
json = await response.json()
|
|
415
|
+
|
|
416
|
+
if self._http_debug:
|
|
417
|
+
_LOGGER.debug(json)
|
|
418
|
+
|
|
419
|
+
return json
|