Skip to content

Commit 662aa93

Browse files
committed
update tests and adjust functions
1 parent a7e73bc commit 662aa93

File tree

5 files changed

+156
-116
lines changed

5 files changed

+156
-116
lines changed

openevsehttp/__init__.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import asyncio
55
import datetime
66
import logging
7-
from typing import Optional
7+
from typing import Any, Optional
88

99
import aiohttp # type: ignore
1010
import requests # type: ignore
@@ -165,15 +165,16 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
165165
"""Connect to an OpenEVSE charger equipped with wifi or ethernet."""
166166
self._user = user
167167
self._pwd = pwd
168-
self.url = f"http://{host}"
168+
self.url = f"http://{host}/"
169169
self._status = None
170170
self._config = None
171171
self._override = None
172172
self._ws_listening = False
173+
self.websocket: Optional[OpenEVSEWebsocket] = None # type: ignore
173174

174175
def send_command(self, command: str) -> tuple | None:
175176
"""Send a RAPI command to the charger and parses the response."""
176-
url = f"{self.url}/r"
177+
url = f"{self.url}r"
177178
data = {"json": 1, "rapi": command}
178179

179180
_LOGGER.debug("Posting data: %s to %s", command, url)
@@ -194,10 +195,10 @@ def send_command(self, command: str) -> tuple | None:
194195
resp = value.json()
195196
return resp["cmd"], resp["ret"]
196197

197-
def update(self) -> None:
198+
async def update(self) -> None:
198199
"""Update the values."""
199200
if not self._ws_listening:
200-
urls = [f"{self.url}/status", f"{self.url}/config"]
201+
urls = [f"{self.url}status", f"{self.url}config"]
201202

202203
for url in urls:
203204
_LOGGER.debug("Updating data from %s", url)
@@ -216,10 +217,10 @@ def update(self) -> None:
216217
self._config = value.json()
217218

218219
# Start Websocket listening
219-
websocket = OpenEVSEWebsocket(
220+
self.websocket = OpenEVSEWebsocket(
220221
self.url, self._update_status, self._user, self._pwd
221222
)
222-
websocket.listen()
223+
await self.websocket.listen()
223224
self._ws_listening = True
224225
else:
225226
url = f"{self.url}/config"
@@ -235,7 +236,7 @@ def update(self) -> None:
235236

236237
self._config = value.json()
237238

238-
def _update_status(self, msgtype, data, error) -> None:
239+
def _update_status(self, msgtype, data, error):
239240
"""Update data from websocket listener."""
240241
if msgtype == SIGNAL_CONNECTION_STATE:
241242
if data == STATE_CONNECTED:
@@ -246,7 +247,8 @@ def _update_status(self, msgtype, data, error) -> None:
246247
self.url,
247248
)
248249
self._ws_listening = False
249-
# Stopped websockets without errors are expected during shutdown and ignored
250+
# Stopped websockets without errors are expected during shutdown
251+
# and ignored
250252
elif data == STATE_STOPPED and error:
251253
_LOGGER.error(
252254
"Websocket to %s failed, aborting [Error: %s]",
@@ -256,7 +258,21 @@ def _update_status(self, msgtype, data, error) -> None:
256258
self._ws_listening = False
257259

258260
elif msgtype == "data":
259-
self._status = data.json()
261+
update_data = data.json()
262+
263+
for key, value in update_data:
264+
self._status[key] = value
265+
266+
def ws_disconnect(self) -> None:
267+
"""Disconnect the websocket listener."""
268+
assert self.websocket
269+
self.websocket.close()
270+
271+
@property
272+
def ws_state(self) -> Any:
273+
"""Disconnect the websocket listener."""
274+
assert self.websocket
275+
return self.websocket.state
260276

261277
def get_override(self) -> None:
262278
"""Get the manual override status."""

requirements_test.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pytest==6.2.4
22
pytest-cov==2.12.1
33
pytest-timeout==1.4.2
4+
pytest-aiohttp
45
requests_mock
5-
aiohttp
6+
aiohttp
7+
aioresponses

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
long_description_content_type="text/markdown",
2121
packages=find_packages(exclude=["test.*", "tests"]),
2222
python_requires=">=3.8",
23-
install_requires=["requests"],
23+
install_requires=["aiohttp", "requests"],
2424
entry_points={},
2525
include_package_data=True,
2626
zip_safe=False,

tests/conftest.py

+37-19
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
import openevsehttp
66
from tests.common import load_fixture
77

8+
from aioresponses import aioresponses
9+
10+
TEST_URL_STATUS = "http://openevse.test.tld/status"
11+
TEST_URL_CONFIG = "http://openevse.test.tld/config"
12+
TEST_URL_RAPI = "http://openevse.test.tld/r"
13+
TEST_TLD = "openevse.test.tld"
14+
815

916
@pytest.fixture(name="test_charger_auth")
1017
def test_charger_auth(status_mock, config_mock):
1118
"""Load the charger data."""
12-
return openevsehttp.OpenEVSE(
13-
"openevse.test.tld", user="testuser", pwd="fakepassword"
14-
)
19+
return openevsehttp.OpenEVSE(TEST_TLD, user="testuser", pwd="fakepassword")
1520

1621

1722
@pytest.fixture(name="test_charger_auth_err")
1823
def test_charger_auth_err(status_mock_err, config_mock_err):
1924
"""Load the charger data."""
20-
return openevsehttp.OpenEVSE(
21-
"openevse.test.tld", user="testuser", pwd="fakepassword"
22-
)
25+
return openevsehttp.OpenEVSE(TEST_TLD, user="testuser", pwd="fakepassword")
2326

2427

2528
@pytest.fixture(name="status_mock_err")
2629
def mock_status_err(requests_mock):
2730
"""Mock the status reply."""
2831
requests_mock.get(
29-
"http://openevse.test.tld/status",
32+
TEST_URL_STATUS,
3033
status_code=401,
3134
)
3235

@@ -35,28 +38,28 @@ def mock_status_err(requests_mock):
3538
def mock_config_err(requests_mock):
3639
"""Mock the config reply."""
3740
requests_mock.get(
38-
"http://openevse.test.tld/config",
41+
TEST_URL_CONFIG,
3942
status_code=401,
4043
)
4144

4245

4346
@pytest.fixture(name="test_charger")
4447
def test_charger(status_mock, config_mock):
4548
"""Load the charger data."""
46-
return openevsehttp.OpenEVSE("openevse.test.tld")
49+
return openevsehttp.OpenEVSE(TEST_TLD)
4750

4851

4952
@pytest.fixture(name="test_charger_v2")
5053
def test_charger_v2(status_mock_v2, config_mock_v2):
5154
"""Load the charger data."""
52-
return openevsehttp.OpenEVSE("openevse.test.tld")
55+
return openevsehttp.OpenEVSE(TEST_TLD)
5356

5457

5558
@pytest.fixture(name="status_mock")
5659
def mock_status(requests_mock):
5760
"""Mock the status reply."""
5861
requests_mock.get(
59-
"http://openevse.test.tld/status",
62+
TEST_URL_STATUS,
6063
text=load_fixture("v4_json/status.json"),
6164
)
6265

@@ -65,7 +68,7 @@ def mock_status(requests_mock):
6568
def mock_config(requests_mock):
6669
"""Mock the config reply."""
6770
requests_mock.get(
68-
"http://openevse.test.tld/config",
71+
TEST_URL_CONFIG,
6972
text=load_fixture("v4_json/config.json"),
7073
)
7174

@@ -74,7 +77,7 @@ def mock_config(requests_mock):
7477
def mock_status_v2(requests_mock):
7578
"""Mock the status reply."""
7679
requests_mock.get(
77-
"http://openevse.test.tld/status",
80+
TEST_URL_STATUS,
7881
text=load_fixture("v2_json/status.json"),
7982
)
8083

@@ -83,7 +86,7 @@ def mock_status_v2(requests_mock):
8386
def mock_config_v2(requests_mock):
8487
"""Mock the config reply."""
8588
requests_mock.get(
86-
"http://openevse.test.tld/config",
89+
TEST_URL_CONFIG,
8790
text=load_fixture("v2_json/config.json"),
8891
)
8992

@@ -93,7 +96,7 @@ def mock_send_command(requests_mock):
9396
"""Mock the command reply."""
9497
value = {"cmd": "OK", "ret": "$OK^20"}
9598
requests_mock.post(
96-
"http://openevse.test.tld/r",
99+
TEST_URL_RAPI,
97100
text=json.dumps(value),
98101
)
99102

@@ -102,7 +105,7 @@ def mock_send_command(requests_mock):
102105
def mock_send_command_parse_err(requests_mock):
103106
"""Mock the command reply parse err."""
104107
requests_mock.post(
105-
"http://openevse.test.tld/r",
108+
TEST_URL_RAPI,
106109
status_code=400,
107110
)
108111

@@ -111,7 +114,7 @@ def mock_send_command_parse_err(requests_mock):
111114
def mock_send_command_auth_err(requests_mock):
112115
"""Mock the command reply auth err."""
113116
requests_mock.post(
114-
"http://openevse.test.tld/r",
117+
TEST_URL_RAPI,
115118
status_code=401,
116119
)
117120

@@ -121,7 +124,7 @@ def mock_send_command_missing(requests_mock):
121124
"""Mock the command reply."""
122125
value = {"cmd": "OK", "what": "$NK^21"}
123126
requests_mock.post(
124-
"http://openevse.test.tld/r",
127+
TEST_URL_RAPI,
125128
text=json.dumps(value),
126129
)
127130

@@ -131,6 +134,21 @@ def mock_send_command_failed(requests_mock):
131134
"""Mock the command reply."""
132135
value = {"cmd": "OK", "ret": "$NK^21"}
133136
requests_mock.post(
134-
"http://openevse.test.tld/r",
137+
TEST_URL_RAPI,
135138
text=json.dumps(value),
136139
)
140+
141+
142+
@pytest.fixture
143+
def aioclient_mock():
144+
"""Fixture to mock aioclient calls."""
145+
with aioresponses() as mock_aiohttp:
146+
mock_headers = {"content-type": "application/json"}
147+
mock_aiohttp.get(
148+
"ws://openevse.test.tld/ws",
149+
status=200,
150+
headers=mock_headers,
151+
body={},
152+
)
153+
154+
yield mock_aiohttp

0 commit comments

Comments
 (0)