Skip to content

Commit

Permalink
Clean up test for Wallbox integration (#125433)
Browse files Browse the repository at this point in the history
feat: Update API requests in wallbox integration tests
  • Loading branch information
hesselonline authored Sep 7, 2024
1 parent 066503b commit 6e38cf8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 51 deletions.
7 changes: 3 additions & 4 deletions tests/components/wallbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Tests for the Wallbox integration."""

from http import HTTPStatus
import json

import requests_mock

Expand Down Expand Up @@ -121,7 +120,7 @@ async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)

Expand All @@ -144,7 +143,7 @@ async def setup_integration_bidir(hass: HomeAssistant, entry: MockConfigEntry) -
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)

Expand All @@ -169,7 +168,7 @@ async def setup_integration_connection_error(
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.FORBIDDEN,
)

Expand Down
10 changes: 9 additions & 1 deletion tests/components/wallbox/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,15 @@ async def test_form_reauth_invalid(hass: HomeAssistant, entry: MockConfigEntry)
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
text='{"jwt":"fakekeyhere","refresh_token": "refresh_fakekeyhere","user_id":12345,"ttl":145656758,"refresh_token_ttl":145756758,"error":false,"status":200}',
json={
"jwt": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
"user_id": 12345,
"ttl": 145656758,
"refresh_token_ttl": 145756758,
"error": False,
"status": 200,
},
status_code=200,
)
mock_request.get(
Expand Down
4 changes: 1 addition & 3 deletions tests/components/wallbox/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Wallbox Init Component."""

import json

import requests_mock

from homeassistant.components.wallbox.const import (
Expand Down Expand Up @@ -90,7 +88,7 @@ async def test_wallbox_refresh_failed_invalid_auth(
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=403,
)

Expand Down
14 changes: 2 additions & 12 deletions tests/components/wallbox/test_lock.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Wallbox Lock component."""

import json

import pytest
import requests_mock

Expand Down Expand Up @@ -38,7 +36,7 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_LOCKED_UNLOCKED_KEY: False})),
json={CHARGER_LOCKED_UNLOCKED_KEY: False},
status_code=200,
)

Expand All @@ -60,8 +58,6 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -
blocking=True,
)

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_lock_class_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
Expand All @@ -78,7 +74,7 @@ async def test_wallbox_lock_class_connection_error(
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_LOCKED_UNLOCKED_KEY: False})),
json={CHARGER_LOCKED_UNLOCKED_KEY: False},
status_code=404,
)

Expand All @@ -101,8 +97,6 @@ async def test_wallbox_lock_class_connection_error(
blocking=True,
)

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_lock_class_authentication_error(
hass: HomeAssistant, entry: MockConfigEntry
Expand All @@ -115,8 +109,6 @@ async def test_wallbox_lock_class_authentication_error(

assert state is None

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_lock_class_platform_not_ready(
hass: HomeAssistant, entry: MockConfigEntry
Expand All @@ -128,5 +120,3 @@ async def test_wallbox_lock_class_platform_not_ready(
state = hass.states.get(MOCK_LOCK_ENTITY_ID)

assert state is None

await hass.config_entries.async_unload(entry.entry_id)
23 changes: 5 additions & 18 deletions tests/components/wallbox/test_number.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Wallbox Switch component."""

import json

import pytest
import requests_mock

Expand Down Expand Up @@ -47,7 +45,7 @@ async def test_wallbox_number_class(
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=200,
)
state = hass.states.get(MOCK_NUMBER_ENTITY_ID)
Expand All @@ -63,7 +61,6 @@ async def test_wallbox_number_class(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_bidir(
Expand All @@ -76,7 +73,6 @@ async def test_wallbox_number_class_bidir(
state = hass.states.get(MOCK_NUMBER_ENTITY_ID)
assert state.attributes["min"] == -25
assert state.attributes["max"] == 25
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_energy_class(
Expand All @@ -95,7 +91,7 @@ async def test_wallbox_number_energy_class(

mock_request.post(
"https://api.wall-box.com/chargers/config/12345",
json=json.loads(json.dumps({CHARGER_ENERGY_PRICE_KEY: 1.1})),
json={CHARGER_ENERGY_PRICE_KEY: 1.1},
status_code=200,
)

Expand All @@ -108,7 +104,6 @@ async def test_wallbox_number_energy_class(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_connection_error(
Expand All @@ -126,7 +121,7 @@ async def test_wallbox_number_class_connection_error(
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=404,
)

Expand All @@ -140,7 +135,6 @@ async def test_wallbox_number_class_connection_error(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_energy_price_connection_error(
Expand All @@ -158,7 +152,7 @@ async def test_wallbox_number_class_energy_price_connection_error(
)
mock_request.post(
"https://api.wall-box.com/chargers/config/12345",
json=json.loads(json.dumps({CHARGER_ENERGY_PRICE_KEY: 1.1})),
json={CHARGER_ENERGY_PRICE_KEY: 1.1},
status_code=404,
)

Expand All @@ -172,7 +166,6 @@ async def test_wallbox_number_class_energy_price_connection_error(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_energy_price_auth_error(
Expand All @@ -190,7 +183,7 @@ async def test_wallbox_number_class_energy_price_auth_error(
)
mock_request.post(
"https://api.wall-box.com/chargers/config/12345",
json=json.loads(json.dumps({CHARGER_ENERGY_PRICE_KEY: 1.1})),
json={CHARGER_ENERGY_PRICE_KEY: 1.1},
status_code=403,
)

Expand All @@ -204,7 +197,6 @@ async def test_wallbox_number_class_energy_price_auth_error(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_platform_not_ready(
Expand All @@ -218,8 +210,6 @@ async def test_wallbox_number_class_platform_not_ready(

assert state is None

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_icp_energy(
hass: HomeAssistant, entry: MockConfigEntry
Expand Down Expand Up @@ -250,7 +240,6 @@ async def test_wallbox_number_class_icp_energy(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_icp_energy_auth_error(
Expand Down Expand Up @@ -282,7 +271,6 @@ async def test_wallbox_number_class_icp_energy_auth_error(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_number_class_icp_energy_connection_error(
Expand Down Expand Up @@ -314,4 +302,3 @@ async def test_wallbox_number_class_icp_energy_connection_error(
},
blocking=True,
)
await hass.config_entries.async_unload(entry.entry_id)
2 changes: 0 additions & 2 deletions tests/components/wallbox/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ async def test_wallbox_sensor_class(
# Test round with precision '0' works
state = hass.states.get(MOCK_SENSOR_MAX_AVAILABLE_POWER)
assert state.state == "25.0"

await hass.config_entries.async_unload(entry.entry_id)
14 changes: 3 additions & 11 deletions tests/components/wallbox/test_switch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Test Wallbox Lock component."""

import json

import pytest
import requests_mock

Expand Down Expand Up @@ -36,7 +34,7 @@ async def test_wallbox_switch_class(
)
mock_request.post(
"https://api.wall-box.com/v3/chargers/12345/remote-action",
json=json.loads(json.dumps({CHARGER_STATUS_ID_KEY: 193})),
json={CHARGER_STATUS_ID_KEY: 193},
status_code=200,
)

Expand All @@ -58,8 +56,6 @@ async def test_wallbox_switch_class(
blocking=True,
)

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_switch_class_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
Expand All @@ -76,7 +72,7 @@ async def test_wallbox_switch_class_connection_error(
)
mock_request.post(
"https://api.wall-box.com/v3/chargers/12345/remote-action",
json=json.loads(json.dumps({CHARGER_STATUS_ID_KEY: 193})),
json={CHARGER_STATUS_ID_KEY: 193},
status_code=404,
)

Expand All @@ -99,8 +95,6 @@ async def test_wallbox_switch_class_connection_error(
blocking=True,
)

await hass.config_entries.async_unload(entry.entry_id)


async def test_wallbox_switch_class_authentication_error(
hass: HomeAssistant, entry: MockConfigEntry
Expand All @@ -117,7 +111,7 @@ async def test_wallbox_switch_class_authentication_error(
)
mock_request.post(
"https://api.wall-box.com/v3/chargers/12345/remote-action",
json=json.loads(json.dumps({CHARGER_STATUS_ID_KEY: 193})),
json={CHARGER_STATUS_ID_KEY: 193},
status_code=403,
)

Expand All @@ -139,5 +133,3 @@ async def test_wallbox_switch_class_authentication_error(
},
blocking=True,
)

await hass.config_entries.async_unload(entry.entry_id)

0 comments on commit 6e38cf8

Please sign in to comment.