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