diff --git a/src/technove/technove.py b/src/technove/technove.py index 91ba06f..191bb9b 100644 --- a/src/technove/technove.py +++ b/src/technove/technove.py @@ -161,14 +161,14 @@ async def set_auto_charge(self, *, enabled: bool) -> None: "/station/set/automatic", method="POST", data={"activated": enabled} ) - async def set_charging_enabled(self, *, can_charge: bool) -> None: + async def set_charging_enabled(self, *, enabled: bool) -> None: """Set whether the charging station is allowed to provide power or not. This can only be set if the auto_charge feature is not enabled. Args: ---- - can_charge: True to allow a plugged-in vehicle to charge, otherwise false. + enabled: True to allow a plugged-in vehicle to charge, otherwise false. Raises: ------ @@ -177,7 +177,7 @@ async def set_charging_enabled(self, *, can_charge: bool) -> None: if self.station and self.station.info.auto_charge: msg = "Cannot start or stop charging when auto-charge is enabled." raise TechnoVEError(msg) - action = "start" if can_charge else "stop" + action = "start" if enabled else "stop" await self.request(f"/station/control/{action}") async def close(self) -> None: diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..3f70c51 --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,17 @@ +"""Tests for `technove.TechnoVE`.""" + + +import pytest + +from technove import Status, TechnoVEError + + +def test_status_build() -> None: + """Test status build with a known status code.""" + assert Status.build(67) == Status.PLUGGED_CHARGING + + +def test_status_build_unknown() -> None: + """Test status build with an unknown status code.""" + with pytest.raises(TechnoVEError): + Status.build(42) diff --git a/tests/test_technove.py b/tests/test_technove.py index f6162f4..e267169 100644 --- a/tests/test_technove.py +++ b/tests/test_technove.py @@ -30,7 +30,7 @@ async def test_json_request(aresponses: ResponsesMockServer) -> None: @pytest.mark.asyncio -async def test_internal_session(aresponses: ResponsesMockServer) -> None: +async def test_json_request_internal_session(aresponses: ResponsesMockServer) -> None: """Test JSON response is handled correctly.""" aresponses.add( "example.com", @@ -47,6 +47,25 @@ async def test_internal_session(aresponses: ResponsesMockServer) -> None: assert response["status"] == "ok" +@pytest.mark.asyncio +async def test_text_request(aresponses: ResponsesMockServer) -> None: + """Test plain text response is handled correctly.""" + aresponses.add( + "example.com", + "/", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "text/plain"}, + text="ok", + ), + ) + async with aiohttp.ClientSession() as session: + technove = TechnoVE("example.com", session=session) + response = await technove.request("/") + assert response == "ok" + + @pytest.mark.asyncio async def test_post_request(aresponses: ResponsesMockServer) -> None: """Test POST requests are handled correctly.""" @@ -159,8 +178,8 @@ async def test_http_error500(aresponses: ResponsesMockServer) -> None: @pytest.mark.asyncio -async def test_empty_full_responses(aresponses: ResponsesMockServer) -> None: - """Test failure handling of full data request TechnoVE device state.""" +async def test_update_empty_responses(aresponses: ResponsesMockServer) -> None: + """Test failure handling of data request TechnoVE device state.""" aresponses.add( "example.com", "/station/get/info", @@ -171,6 +190,15 @@ async def test_empty_full_responses(aresponses: ResponsesMockServer) -> None: text="{}", ), ) + async with aiohttp.ClientSession() as session: + technove = TechnoVE("example.com", session=session) + with pytest.raises(TechnoVEError): + await technove.update() + + +@pytest.mark.asyncio +async def test_update_partial_responses(aresponses: ResponsesMockServer) -> None: + """Test handling of data request TechnoVE device state.""" aresponses.add( "example.com", "/station/get/info", @@ -178,13 +206,63 @@ async def test_empty_full_responses(aresponses: ResponsesMockServer) -> None: aresponses.Response( status=200, headers={"Content-Type": "application/json"}, - text="{}", + text='{"name":"testing"}', ), ) async with aiohttp.ClientSession() as session: technove = TechnoVE("example.com", session=session) - with pytest.raises(TechnoVEError): - await technove.update() + station = await technove.update() + assert station.info.name == "testing" + + +@pytest.mark.asyncio +async def test_set_auto_charge(aresponses: ResponsesMockServer) -> None: + """Test that enabling auto_charge calls the right API.""" + aresponses.add( + "example.com", + "/station/set/automatic", + "POST", + aresponses.Response( + status=200, + headers={"Content-Type": "plain/text"}, + text="ok", + ), + ) + async with aiohttp.ClientSession() as session: + technove = TechnoVE("example.com", session=session) + await technove.set_auto_charge(enabled=True) + aresponses.assert_plan_strictly_followed() + + +@pytest.mark.asyncio +async def test_set_charging_enabled(aresponses: ResponsesMockServer) -> None: + """Test that changing charging_enabled calls the right API.""" + aresponses.add( + "example.com", + "/station/control/start", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "plain/text"}, + text="ok", + ), + ) + aresponses.add( + "example.com", + "/station/control/stop", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "plain/text"}, + text="ok", + ), + ) + async with aiohttp.ClientSession() as session: + technove = TechnoVE("example.com", session=session) + technove.station = Station({"auto_charge": False}) + await technove.set_charging_enabled(enabled=True) + await technove.set_charging_enabled(enabled=False) + aresponses.assert_plan_strictly_followed() @pytest.mark.asyncio @@ -193,4 +271,4 @@ async def test_set_charging_enabled_auto_charge() -> None: technove = TechnoVE("example.com") technove.station = Station({"auto_charge": True}) with pytest.raises(TechnoVEError): - await technove.set_charging_enabled(can_charge=True) + await technove.set_charging_enabled(enabled=True)