Fix wallbox tests (#91752)

This commit is contained in:
epenet 2023-04-20 20:42:22 +02:00 committed by GitHub
parent fd3aa5338c
commit 62f76a81bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 115 additions and 73 deletions

View file

@ -22,10 +22,7 @@ from homeassistant.components.wallbox.const import (
CHARGER_SERIAL_NUMBER_KEY,
CHARGER_SOFTWARE_KEY,
CHARGER_STATUS_ID_KEY,
CONF_STATION,
DOMAIN,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from .const import ERROR, STATUS, TTL, USER_ID
@ -88,22 +85,9 @@ authorisation_response_unauthorised = json.loads(
)
)
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_USERNAME: "test_username",
CONF_PASSWORD: "test_password",
CONF_STATION: "12345",
},
entry_id="testEntry",
)
async def setup_integration(hass: HomeAssistant) -> None:
async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test wallbox sensor class setup."""
entry.add_to_hass(hass)
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
@ -121,15 +105,14 @@ async def setup_integration(hass: HomeAssistant) -> None:
status_code=HTTPStatus.OK,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_connection_error(hass: HomeAssistant) -> None:
async def setup_integration_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class setup with a connection error."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
@ -147,13 +130,13 @@ async def setup_integration_connection_error(hass: HomeAssistant) -> None:
status_code=HTTPStatus.FORBIDDEN,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_read_only(hass: HomeAssistant) -> None:
async def setup_integration_read_only(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class setup for read only."""
with requests_mock.Mocker() as mock_request:
@ -173,13 +156,13 @@ async def setup_integration_read_only(hass: HomeAssistant) -> None:
status_code=HTTPStatus.FORBIDDEN,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_platform_not_ready(hass: HomeAssistant) -> None:
async def setup_integration_platform_not_ready(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class setup for read only."""
with requests_mock.Mocker() as mock_request:
@ -199,7 +182,5 @@ async def setup_integration_platform_not_ready(hass: HomeAssistant) -> None:
status_code=HTTPStatus.NOT_FOUND,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

View file

@ -0,0 +1,24 @@
"""Test fixtures for the Wallbox integration."""
import pytest
from homeassistant.components.wallbox.const import CONF_STATION, DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.fixture
def entry(hass: HomeAssistant) -> MockConfigEntry:
"""Return mock config entry."""
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_USERNAME: "test_username",
CONF_PASSWORD: "test_password",
CONF_STATION: "12345",
},
entry_id="testEntry",
)
entry.add_to_hass(hass)
return entry

View file

@ -21,10 +21,11 @@ from homeassistant.core import HomeAssistant
from . import (
authorisation_response,
authorisation_response_unauthorised,
entry,
setup_integration,
)
from tests.common import MockConfigEntry
test_response = json.loads(
json.dumps(
{
@ -139,9 +140,9 @@ async def test_form_validate_input(hass: HomeAssistant) -> None:
assert result2["data"]["station"] == "12345"
async def test_form_reauth(hass: HomeAssistant) -> None:
async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test we handle reauth flow."""
await setup_integration(hass)
await setup_integration(hass, entry)
assert entry.state == config_entries.ConfigEntryState.LOADED
with requests_mock.Mocker() as mock_request:
@ -179,9 +180,9 @@ async def test_form_reauth(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
async def test_form_reauth_invalid(hass: HomeAssistant) -> None:
async def test_form_reauth_invalid(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test we handle reauth invalid flow."""
await setup_integration(hass)
await setup_integration(hass, entry)
assert entry.state == config_entries.ConfigEntryState.LOADED
with requests_mock.Mocker() as mock_request:

View file

@ -3,45 +3,51 @@ import json
import requests_mock
from homeassistant.components.wallbox import CHARGER_MAX_CHARGING_CURRENT_KEY
from homeassistant.components.wallbox import CHARGER_MAX_CHARGING_CURRENT_KEY, DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from . import (
DOMAIN,
authorisation_response,
entry,
setup_integration,
setup_integration_connection_error,
setup_integration_read_only,
test_response,
)
from tests.common import MockConfigEntry
async def test_wallbox_setup_unload_entry(hass: HomeAssistant) -> None:
async def test_wallbox_setup_unload_entry(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox Unload."""
await setup_integration(hass)
await setup_integration(hass, entry)
assert entry.state == ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state == ConfigEntryState.NOT_LOADED
async def test_wallbox_unload_entry_connection_error(hass: HomeAssistant) -> None:
async def test_wallbox_unload_entry_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox Unload Connection Error."""
await setup_integration_connection_error(hass)
await setup_integration_connection_error(hass, entry)
assert entry.state == ConfigEntryState.SETUP_ERROR
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state == ConfigEntryState.NOT_LOADED
async def test_wallbox_refresh_failed_invalid_auth(hass: HomeAssistant) -> None:
async def test_wallbox_refresh_failed_invalid_auth(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox setup with authentication error."""
await setup_integration(hass)
await setup_integration(hass, entry)
assert entry.state == ConfigEntryState.LOADED
with requests_mock.Mocker() as mock_request:
@ -64,10 +70,12 @@ async def test_wallbox_refresh_failed_invalid_auth(hass: HomeAssistant) -> None:
assert entry.state == ConfigEntryState.NOT_LOADED
async def test_wallbox_refresh_failed_connection_error(hass: HomeAssistant) -> None:
async def test_wallbox_refresh_failed_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox setup with connection error."""
await setup_integration(hass)
await setup_integration(hass, entry)
assert entry.state == ConfigEntryState.LOADED
with requests_mock.Mocker() as mock_request:
@ -90,10 +98,12 @@ async def test_wallbox_refresh_failed_connection_error(hass: HomeAssistant) -> N
assert entry.state == ConfigEntryState.NOT_LOADED
async def test_wallbox_refresh_failed_read_only(hass: HomeAssistant) -> None:
async def test_wallbox_refresh_failed_read_only(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox setup for read-only user."""
await setup_integration_read_only(hass)
await setup_integration_read_only(hass, entry)
assert entry.state == ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id)

View file

@ -11,18 +11,19 @@ from homeassistant.core import HomeAssistant
from . import (
authorisation_response,
entry,
setup_integration,
setup_integration_platform_not_ready,
setup_integration_read_only,
)
from .const import MOCK_LOCK_ENTITY_ID
from tests.common import MockConfigEntry
async def test_wallbox_lock_class(hass: HomeAssistant) -> None:
async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test wallbox lock class."""
await setup_integration(hass)
await setup_integration(hass, entry)
state = hass.states.get(MOCK_LOCK_ENTITY_ID)
assert state
@ -61,10 +62,12 @@ async def test_wallbox_lock_class(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_lock_class_connection_error(hass: HomeAssistant) -> None:
async def test_wallbox_lock_class_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox lock class connection error."""
await setup_integration(hass)
await setup_integration(hass, entry)
with requests_mock.Mocker() as mock_request:
mock_request.get(
@ -100,10 +103,12 @@ async def test_wallbox_lock_class_connection_error(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_lock_class_authentication_error(hass: HomeAssistant) -> None:
async def test_wallbox_lock_class_authentication_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox lock not loaded on authentication error."""
await setup_integration_read_only(hass)
await setup_integration_read_only(hass, entry)
state = hass.states.get(MOCK_LOCK_ENTITY_ID)
@ -112,10 +117,12 @@ async def test_wallbox_lock_class_authentication_error(hass: HomeAssistant) -> N
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_lock_class_platform_not_ready(hass: HomeAssistant) -> None:
async def test_wallbox_lock_class_platform_not_ready(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox lock not loaded on authentication error."""
await setup_integration_platform_not_ready(hass)
await setup_integration_platform_not_ready(hass, entry)
state = hass.states.get(MOCK_LOCK_ENTITY_ID)

View file

@ -11,17 +11,20 @@ from homeassistant.core import HomeAssistant
from . import (
authorisation_response,
entry,
setup_integration,
setup_integration_platform_not_ready,
)
from .const import MOCK_NUMBER_ENTITY_ID
from tests.common import MockConfigEntry
async def test_wallbox_number_class(hass: HomeAssistant) -> None:
async def test_wallbox_number_class(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass)
await setup_integration(hass, entry)
with requests_mock.Mocker() as mock_request:
mock_request.get(
@ -47,10 +50,12 @@ async def test_wallbox_number_class(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_number_class_connection_error(hass: HomeAssistant) -> None:
async def test_wallbox_number_class_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass)
await setup_integration(hass, entry)
with requests_mock.Mocker() as mock_request:
mock_request.get(
@ -77,10 +82,12 @@ async def test_wallbox_number_class_connection_error(hass: HomeAssistant) -> Non
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_number_class_platform_not_ready(hass: HomeAssistant) -> None:
async def test_wallbox_number_class_platform_not_ready(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox lock not loaded on authentication error."""
await setup_integration_platform_not_ready(hass)
await setup_integration_platform_not_ready(hass, entry)
state = hass.states.get(MOCK_NUMBER_ENTITY_ID)

View file

@ -2,18 +2,22 @@
from homeassistant.const import CONF_ICON, CONF_UNIT_OF_MEASUREMENT, UnitOfPower
from homeassistant.core import HomeAssistant
from . import entry, setup_integration
from . import setup_integration
from .const import (
MOCK_SENSOR_CHARGING_POWER_ID,
MOCK_SENSOR_CHARGING_SPEED_ID,
MOCK_SENSOR_MAX_AVAILABLE_POWER,
)
from tests.common import MockConfigEntry
async def test_wallbox_sensor_class(hass: HomeAssistant) -> None:
async def test_wallbox_sensor_class(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass)
await setup_integration(hass, entry)
state = hass.states.get(MOCK_SENSOR_CHARGING_POWER_ID)
assert state.attributes[CONF_UNIT_OF_MEASUREMENT] == UnitOfPower.KILO_WATT

View file

@ -10,14 +10,18 @@ from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from . import authorisation_response, entry, setup_integration
from . import authorisation_response, setup_integration
from .const import MOCK_SWITCH_ENTITY_ID
from tests.common import MockConfigEntry
async def test_wallbox_switch_class(hass: HomeAssistant) -> None:
async def test_wallbox_switch_class(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox switch class."""
await setup_integration(hass)
await setup_integration(hass, entry)
state = hass.states.get(MOCK_SWITCH_ENTITY_ID)
assert state
@ -56,10 +60,12 @@ async def test_wallbox_switch_class(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_switch_class_connection_error(hass: HomeAssistant) -> None:
async def test_wallbox_switch_class_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox switch class connection error."""
await setup_integration(hass)
await setup_integration(hass, entry)
with requests_mock.Mocker() as mock_request:
mock_request.get(
@ -95,10 +101,12 @@ async def test_wallbox_switch_class_connection_error(hass: HomeAssistant) -> Non
await hass.config_entries.async_unload(entry.entry_id)
async def test_wallbox_switch_class_authentication_error(hass: HomeAssistant) -> None:
async def test_wallbox_switch_class_authentication_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox switch class connection error."""
await setup_integration(hass)
await setup_integration(hass, entry)
with requests_mock.Mocker() as mock_request:
mock_request.get(