Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions openevsehttp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def running(self):
break

except aiohttp.ClientResponseError as error:
if error.code == 401:
if error.status == 401:
_LOGGER.error("Credentials rejected: %s", error)
self._error_reason = ERROR_AUTH_FAILURE
else:
Expand Down Expand Up @@ -202,13 +202,14 @@ async def process_request(
if resp.status == 400:
_LOGGER.error("%s", message["msg"])
raise ParseJSONError
elif resp.status == 401:
_LOGGER.error("Authentication error: %s", resp.text())
if resp.status == 401:
error = await resp.text()
_LOGGER.error("Authentication error: %s", error)
raise AuthenticationError
elif resp.status == 404:
if resp.status == 404:
_LOGGER.error("%s", message["msg"])
raise UnknownError
elif resp.status == 405:
if resp.status == 405:
_LOGGER.error("%s", message["msg"])
elif resp.status == 500:
_LOGGER.error("%s", message["msg"])
Expand Down Expand Up @@ -249,10 +250,9 @@ async def update(self) -> None:
self.websocket = OpenEVSEWebsocket(
self.url, self._update_status, self._user, self._pwd
)
self.ws_start()

def ws_start(self):
"""Method to start the websocket listener."""
"""Start the websocket listener."""
if self._ws_listening:
raise AlreadyListening
self._start_listening()
Expand Down Expand Up @@ -301,7 +301,7 @@ def _update_status(self, msgtype, data, error):
self._status.update(data)

if self.callback is not None:
self.callback()
self.callback() # pylint: disable=not-callable

def ws_disconnect(self) -> None:
"""Disconnect the websocket listener."""
Expand Down Expand Up @@ -334,7 +334,9 @@ async def set_charge_mode(self, mode: str = "fast") -> None:
data = {"charge_mode": mode}

_LOGGER.debug("Setting charge mode to %s", mode)
response = self.process_request(url=url, method="post", data=data)
response = await self.process_request(
url=url, method="post", data=data
) # noqa: E501
if response["msg"] != "done":
_LOGGER.error("Problem issuing command: %s", response["msg"])
raise UnknownError
Expand Down Expand Up @@ -372,7 +374,9 @@ async def set_override(
}

_LOGGER.debug("Setting override config on %s", url)
response = await self.process_request(url=url, method="post", data=data)
response = await self.process_request(
url=url, method="post", data=data
) # noqa: E501
return response

async def toggle_override(self) -> None:
Expand Down
4 changes: 2 additions & 2 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pytest==6.2.4
pytest-cov==2.12.1
pytest-timeout==1.4.2
pytest-aiohttp
pytest-asyncio
requests_mock
aiohttp
aioresponses
aioresponses
188 changes: 49 additions & 139 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,164 +14,67 @@


@pytest.fixture(name="test_charger_auth")
def test_charger_auth(status_mock, config_mock):
def test_charger_auth(mock_aioclient):
"""Load the charger data."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
return openevsehttp.OpenEVSE(TEST_TLD, user="testuser", pwd="fakepassword")


@pytest.fixture(name="test_charger_auth_err")
def test_charger_auth_err(status_mock_err, config_mock_err):
def test_charger_auth_err(mock_aioclient):
"""Load the charger data."""
mock_aioclient.get(
TEST_URL_STATUS,
status=401,
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=401,
)
return openevsehttp.OpenEVSE(TEST_TLD, user="testuser", pwd="fakepassword")


@pytest.fixture(name="status_mock_err")
def mock_status_err():
"""Mock the status reply."""
with aioresponses() as client_mock:
client_mock.get(
TEST_URL_STATUS,
status=401,
)

yield client_mock


@pytest.fixture(name="config_mock_err")
def mock_config_err():
"""Mock the config reply."""
with aioresponses() as client_mock:
client_mock.get(
TEST_URL_CONFIG,
status=401,
)

yield client_mock


@pytest.fixture(name="test_charger")
def test_charger(status_mock, config_mock):
def test_charger(mock_aioclient):
"""Load the charger data."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)
return openevsehttp.OpenEVSE(TEST_TLD)


@pytest.fixture(name="test_charger_v2")
def test_charger_v2(status_mock_v2, config_mock_v2):
def test_charger_v2(mock_aioclient):
"""Load the charger data."""
mock_aioclient.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v2_json/status.json"),
)
mock_aioclient.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v2_json/config.json"),
)
return openevsehttp.OpenEVSE(TEST_TLD)


@pytest.fixture(name="status_mock")
def mock_status():
"""Mock the status reply."""
with aioresponses() as mock_status:
mock_status.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v4_json/status.json"),
)

yield mock_status


@pytest.fixture(name="config_mock")
def mock_config():
"""Mock the config reply."""
with aioresponses() as mock_config:
mock_config.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v4_json/config.json"),
)

yield mock_config


@pytest.fixture(name="status_mock_v2")
def mock_status_v2():
"""Mock the status reply."""
with aioresponses() as client_mock:
client_mock.get(
TEST_URL_STATUS,
status=200,
body=load_fixture("v2_json/status.json"),
)
yield client_mock


@pytest.fixture(name="config_mock_v2")
def mock_config_v2():
"""Mock the config reply."""
with aioresponses() as client_mock:
client_mock.get(
TEST_URL_CONFIG,
status=200,
body=load_fixture("v2_json/config.json"),
)
yield client_mock


@pytest.fixture(name="send_command_mock")
def mock_send_command():
"""Mock the command reply."""
with aioresponses() as client_mock:
value = {"cmd": "OK", "ret": "$OK^20"}
client_mock.post(
TEST_URL_RAPI,
status=200,
body=json.dumps(value),
)
yield client_mock


@pytest.fixture(name="send_command_parse_err")
def mock_send_command_parse_err():
"""Mock the command reply parse err."""
with aioresponses() as client_mock:
client_mock.post(
TEST_URL_RAPI,
status=400,
)
yield client_mock


@pytest.fixture(name="send_command_auth_err")
def mock_send_command_auth_err():
"""Mock the command reply auth err."""
with aioresponses() as client_mock:
client_mock.post(
TEST_URL_RAPI,
status=401,
)
yield client_mock


@pytest.fixture(name="send_command_mock_missing")
def mock_send_command_missing():
"""Mock the command reply."""
with aioresponses() as client_mock:
value = {"cmd": "OK", "what": "$NK^21"}
client_mock.post(
TEST_URL_RAPI,
status=200,
body=json.dumps(value),
)
yield client_mock


@pytest.fixture(name="send_command_mock_failed")
def mock_send_command_failed():
"""Mock the command reply."""
with aioresponses() as client_mock:
value = {"cmd": "OK", "ret": "$NK^21"}
client_mock.post(
TEST_URL_RAPI,
status=200,
body=json.dumps(value),
)
yield client_mock


@pytest.fixture
def aioclient_mock():
"""Fixture to mock aioclient calls."""
Expand All @@ -185,3 +88,10 @@ def aioclient_mock():
)

yield mock_aiohttp


@pytest.fixture
def mock_aioclient():
"""Fixture to mock aioclient calls."""
with aioresponses() as m:
yield m
Loading