Files
homeassistant-core/tests/components/wallbox/test_init.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

143 lines
4.1 KiB
Python
Raw Normal View History

2021-05-24 13:08:24 +02:00
"""Test Wallbox Init Component."""
2021-05-24 13:08:24 +02:00
import json
2021-10-27 19:53:14 +02:00
import requests_mock
from homeassistant.components.wallbox.const import (
CHARGER_MAX_CHARGING_CURRENT_KEY,
DOMAIN,
)
2021-10-27 19:53:14 +02:00
from homeassistant.config_entries import ConfigEntryState
2021-05-27 09:56:20 -04:00
from homeassistant.core import HomeAssistant
2021-05-24 13:08:24 +02:00
from . import (
2022-06-06 03:31:09 +02:00
authorisation_response,
2021-10-27 19:53:14 +02:00
setup_integration,
setup_integration_connection_error,
setup_integration_read_only,
test_response,
2021-10-27 19:53:14 +02:00
)
2021-05-24 13:08:24 +02:00
2023-04-20 20:42:22 +02:00
from tests.common import MockConfigEntry
2021-05-24 13:08:24 +02:00
2023-04-20 20:42:22 +02:00
async def test_wallbox_setup_unload_entry(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
2021-05-24 13:08:24 +02:00
"""Test Wallbox Unload."""
2023-04-20 20:42:22 +02:00
await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED
2021-05-24 13:08:24 +02:00
2021-06-27 21:06:25 +02:00
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
2021-10-27 19:53:14 +02:00
2023-04-20 20:42:22 +02:00
async def test_wallbox_unload_entry_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
2021-10-27 19:53:14 +02:00
"""Test Wallbox Unload Connection Error."""
2023-04-20 20:42:22 +02:00
await setup_integration_connection_error(hass, entry)
assert entry.state is ConfigEntryState.SETUP_ERROR
2021-10-27 19:53:14 +02:00
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
2021-10-27 19:53:14 +02:00
2023-10-22 16:22:35 +02:00
async def test_wallbox_refresh_failed_connection_error_auth(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test Wallbox setup with connection error."""
await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED
2023-10-22 16:22:35 +02:00
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=404,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=200,
)
wallbox = hass.data[DOMAIN][entry.entry_id]
await wallbox.async_refresh()
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
2023-10-22 16:22:35 +02:00
2023-04-20 20:42:22 +02:00
async def test_wallbox_refresh_failed_invalid_auth(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
2021-10-27 19:53:14 +02:00
"""Test Wallbox setup with authentication error."""
2023-04-20 20:42:22 +02:00
await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED
2021-10-27 19:53:14 +02:00
with requests_mock.Mocker() as mock_request:
mock_request.get(
2022-06-06 03:31:09 +02:00
"https://user-api.wall-box.com/users/signin",
2021-10-27 19:53:14 +02:00
json=authorisation_response,
status_code=403,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=json.loads(json.dumps({CHARGER_MAX_CHARGING_CURRENT_KEY: 20})),
2021-10-27 19:53:14 +02:00
status_code=403,
)
wallbox = hass.data[DOMAIN][entry.entry_id]
2021-10-27 19:53:14 +02:00
await wallbox.async_refresh()
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
2021-10-27 19:53:14 +02:00
2023-04-20 20:42:22 +02:00
async def test_wallbox_refresh_failed_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
2021-10-27 19:53:14 +02:00
"""Test Wallbox setup with connection error."""
2023-04-20 20:42:22 +02:00
await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED
2021-10-27 19:53:14 +02:00
with requests_mock.Mocker() as mock_request:
mock_request.get(
2022-06-06 03:31:09 +02:00
"https://user-api.wall-box.com/users/signin",
2021-10-27 19:53:14 +02:00
json=authorisation_response,
status_code=200,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=403,
)
wallbox = hass.data[DOMAIN][entry.entry_id]
2021-10-27 19:53:14 +02:00
await wallbox.async_refresh()
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
2021-10-27 19:53:14 +02:00
2023-04-20 20:42:22 +02:00
async def test_wallbox_refresh_failed_read_only(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
2021-10-27 19:53:14 +02:00
"""Test Wallbox setup for read-only user."""
2023-04-20 20:42:22 +02:00
await setup_integration_read_only(hass, entry)
assert entry.state is ConfigEntryState.LOADED
2021-10-27 19:53:14 +02:00
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED