Wallbox, Improve test setup (#148036)

This commit is contained in:
Hessel 2025-07-04 21:56:44 +02:00 committed by GitHub
parent b6b6de24ac
commit bfccee17ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 567 additions and 1054 deletions

View File

@ -1,387 +1 @@
"""Tests for the Wallbox integration.""" """Tests for the Wallbox integration."""
from http import HTTPStatus
import requests
import requests_mock
from homeassistant.components.wallbox.const import (
CHARGER_ADDED_ENERGY_KEY,
CHARGER_ADDED_RANGE_KEY,
CHARGER_CHARGING_POWER_KEY,
CHARGER_CHARGING_SPEED_KEY,
CHARGER_CURRENCY_KEY,
CHARGER_CURRENT_VERSION_KEY,
CHARGER_DATA_KEY,
CHARGER_ECO_SMART_KEY,
CHARGER_ECO_SMART_MODE_KEY,
CHARGER_ECO_SMART_STATUS_KEY,
CHARGER_ENERGY_PRICE_KEY,
CHARGER_FEATURES_KEY,
CHARGER_LOCKED_UNLOCKED_KEY,
CHARGER_MAX_AVAILABLE_POWER_KEY,
CHARGER_MAX_CHARGING_CURRENT_KEY,
CHARGER_MAX_ICP_CURRENT_KEY,
CHARGER_NAME_KEY,
CHARGER_PART_NUMBER_KEY,
CHARGER_PLAN_KEY,
CHARGER_POWER_BOOST_KEY,
CHARGER_SERIAL_NUMBER_KEY,
CHARGER_SOFTWARE_KEY,
CHARGER_STATUS_ID_KEY,
)
from homeassistant.core import HomeAssistant
from .const import ERROR, REFRESH_TOKEN_TTL, STATUS, TTL, USER_ID
from tests.common import MockConfigEntry
test_response = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: False,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_bidir = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: False,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_eco_mode = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: True,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_full_solar = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: True,
CHARGER_ECO_SMART_MODE_KEY: 1,
},
},
}
test_response_no_power_boost = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []},
},
}
http_404_error = requests.exceptions.HTTPError()
http_404_error.response = requests.Response()
http_404_error.response.status_code = HTTPStatus.NOT_FOUND
http_429_error = requests.exceptions.HTTPError()
http_429_error.response = requests.Response()
http_429_error.response.status_code = HTTPStatus.TOO_MANY_REQUESTS
authorisation_response = {
"data": {
"attributes": {
"token": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
USER_ID: 12345,
TTL: 145656758,
REFRESH_TOKEN_TTL: 145756758,
ERROR: "false",
STATUS: 200,
}
}
}
authorisation_response_unauthorised = {
"data": {
"attributes": {
"token": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
USER_ID: 12345,
TTL: 145656758,
REFRESH_TOKEN_TTL: 145756758,
ERROR: "false",
STATUS: 404,
}
}
}
invalid_reauth_response = {
"jwt": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
"user_id": 12345,
"ttl": 145656758,
"refresh_token_ttl": 145756758,
"error": False,
"status": 200,
}
http_403_error = requests.exceptions.HTTPError()
http_403_error.response = requests.Response()
http_403_error.response.status_code = HTTPStatus.FORBIDDEN
http_404_error = requests.exceptions.HTTPError()
http_404_error.response = requests.Response()
http_404_error.response.status_code = HTTPStatus.NOT_FOUND
async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test wallbox sensor class setup."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_no_eco_mode(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class setup."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response_no_power_boost,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_select(
hass: HomeAssistant, entry: MockConfigEntry, response
) -> None:
"""Test wallbox sensor class setup."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=response,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
async def setup_integration_bidir(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test wallbox sensor class setup."""
with requests_mock.Mocker() as mock_request:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response_bidir,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.OK,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
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",
json=authorisation_response,
status_code=HTTPStatus.FORBIDDEN,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=HTTPStatus.FORBIDDEN,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20},
status_code=HTTPStatus.FORBIDDEN,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
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:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=test_response,
status_code=HTTPStatus.FORBIDDEN,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
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:
mock_request.get(
"https://user-api.wall-box.com/users/signin",
json=authorisation_response,
status_code=HTTPStatus.OK,
)
mock_request.get(
"https://api.wall-box.com/chargers/status/12345",
json=test_response,
status_code=HTTPStatus.OK,
)
mock_request.put(
"https://api.wall-box.com/v2/charger/12345",
json=test_response,
status_code=HTTPStatus.NOT_FOUND,
)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

View File

@ -1,13 +1,220 @@
"""Test fixtures for the Wallbox integration.""" """Test fixtures for the Wallbox integration."""
import pytest from http import HTTPStatus
from unittest.mock import MagicMock, Mock, patch
from homeassistant.components.wallbox.const import CONF_STATION, DOMAIN import pytest
import requests
from homeassistant.components.wallbox.const import (
CHARGER_ADDED_ENERGY_KEY,
CHARGER_ADDED_RANGE_KEY,
CHARGER_CHARGING_POWER_KEY,
CHARGER_CHARGING_SPEED_KEY,
CHARGER_CURRENCY_KEY,
CHARGER_CURRENT_VERSION_KEY,
CHARGER_DATA_KEY,
CHARGER_DATA_POST_L1_KEY,
CHARGER_DATA_POST_L2_KEY,
CHARGER_ECO_SMART_KEY,
CHARGER_ECO_SMART_MODE_KEY,
CHARGER_ECO_SMART_STATUS_KEY,
CHARGER_ENERGY_PRICE_KEY,
CHARGER_FEATURES_KEY,
CHARGER_LOCKED_UNLOCKED_KEY,
CHARGER_MAX_AVAILABLE_POWER_KEY,
CHARGER_MAX_CHARGING_CURRENT_KEY,
CHARGER_MAX_CHARGING_CURRENT_POST_KEY,
CHARGER_MAX_ICP_CURRENT_KEY,
CHARGER_NAME_KEY,
CHARGER_PART_NUMBER_KEY,
CHARGER_PLAN_KEY,
CHARGER_POWER_BOOST_KEY,
CHARGER_SERIAL_NUMBER_KEY,
CHARGER_SOFTWARE_KEY,
CHARGER_STATUS_ID_KEY,
CONF_STATION,
DOMAIN,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .const import ERROR, REFRESH_TOKEN_TTL, STATUS, TTL, USER_ID
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
test_response = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: False,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_bidir = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: False,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_eco_mode = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: True,
CHARGER_ECO_SMART_MODE_KEY: 0,
},
},
}
test_response_full_solar = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]},
CHARGER_ECO_SMART_KEY: {
CHARGER_ECO_SMART_STATUS_KEY: True,
CHARGER_ECO_SMART_MODE_KEY: 1,
},
},
}
test_response_no_power_boost = {
CHARGER_CHARGING_POWER_KEY: 0,
CHARGER_STATUS_ID_KEY: 193,
CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0,
CHARGER_CHARGING_SPEED_KEY: 0,
CHARGER_ADDED_RANGE_KEY: 150,
CHARGER_ADDED_ENERGY_KEY: 44.697,
CHARGER_NAME_KEY: "WallboxName",
CHARGER_DATA_KEY: {
CHARGER_MAX_CHARGING_CURRENT_KEY: 24,
CHARGER_ENERGY_PRICE_KEY: 0.4,
CHARGER_LOCKED_UNLOCKED_KEY: False,
CHARGER_SERIAL_NUMBER_KEY: "20000",
CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E",
CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"},
CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"},
CHARGER_MAX_ICP_CURRENT_KEY: 20,
CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []},
},
}
http_403_error = requests.exceptions.HTTPError()
http_403_error.response = requests.Response()
http_403_error.response.status_code = HTTPStatus.FORBIDDEN
http_404_error = requests.exceptions.HTTPError()
http_404_error.response = requests.Response()
http_404_error.response.status_code = HTTPStatus.NOT_FOUND
http_429_error = requests.exceptions.HTTPError()
http_429_error.response = requests.Response()
http_429_error.response.status_code = HTTPStatus.TOO_MANY_REQUESTS
authorisation_response = {
"data": {
"attributes": {
"token": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
USER_ID: 12345,
TTL: 145656758,
REFRESH_TOKEN_TTL: 145756758,
ERROR: "false",
STATUS: 200,
}
}
}
authorisation_response_unauthorised = {
"data": {
"attributes": {
"token": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
USER_ID: 12345,
TTL: 145656758,
REFRESH_TOKEN_TTL: 145756758,
ERROR: "false",
STATUS: 404,
}
}
}
invalid_reauth_response = {
"jwt": "fakekeyhere",
"refresh_token": "refresh_fakekeyhere",
"user_id": 12345,
"ttl": 145656758,
"refresh_token_ttl": 145756758,
"error": False,
"status": 200,
}
@pytest.fixture @pytest.fixture
def entry(hass: HomeAssistant) -> MockConfigEntry: def entry(hass: HomeAssistant) -> MockConfigEntry:
@ -23,3 +230,46 @@ def entry(hass: HomeAssistant) -> MockConfigEntry:
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)
return entry return entry
@pytest.fixture
def mock_wallbox():
"""Patch Wallbox class for tests."""
with patch("homeassistant.components.wallbox.Wallbox") as mock:
wallbox = MagicMock()
wallbox.authenticate = Mock(return_value=authorisation_response)
wallbox.lockCharger = Mock(
return_value={
CHARGER_DATA_POST_L1_KEY: {
CHARGER_DATA_POST_L2_KEY: {CHARGER_LOCKED_UNLOCKED_KEY: True}
}
}
)
wallbox.unlockCharger = Mock(
return_value={
CHARGER_DATA_POST_L1_KEY: {
CHARGER_DATA_POST_L2_KEY: {CHARGER_LOCKED_UNLOCKED_KEY: True}
}
}
)
wallbox.setEnergyCost = Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 0.25})
wallbox.setMaxChargingCurrent = Mock(
return_value={
CHARGER_DATA_POST_L1_KEY: {
CHARGER_DATA_POST_L2_KEY: {
CHARGER_MAX_CHARGING_CURRENT_POST_KEY: True
}
}
}
)
wallbox.setIcpMaxCurrent = Mock(return_value={CHARGER_MAX_ICP_CURRENT_KEY: 25})
wallbox.getChargerStatus = Mock(return_value=test_response)
mock.return_value = wallbox
yield wallbox
async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Test wallbox sensor class setup."""
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

View File

@ -18,7 +18,7 @@ from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from . import ( from .conftest import (
authorisation_response, authorisation_response,
authorisation_response_unauthorised, authorisation_response_unauthorised,
http_403_error, http_403_error,
@ -38,7 +38,7 @@ test_response = {
} }
async def test_show_set_form(hass: HomeAssistant) -> None: async def test_show_set_form(hass: HomeAssistant, mock_wallbox) -> None:
"""Test that the setup form is served.""" """Test that the setup form is served."""
flow = config_flow.WallboxConfigFlow() flow = config_flow.WallboxConfigFlow()
flow.hass = hass flow.hass = hass
@ -53,7 +53,6 @@ async def test_form_cannot_authenticate(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with ( with (
patch( patch(
"homeassistant.components.wallbox.Wallbox.authenticate", "homeassistant.components.wallbox.Wallbox.authenticate",
@ -82,7 +81,6 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with ( with (
patch( patch(
"homeassistant.components.wallbox.Wallbox.authenticate", "homeassistant.components.wallbox.Wallbox.authenticate",
@ -111,15 +109,14 @@ async def test_form_validate_input(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
) )
with ( with (
patch( patch(
"homeassistant.components.wallbox.Wallbox.authenticate", "homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response), return_value=authorisation_response,
), ),
patch( patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus", "homeassistant.components.wallbox.Wallbox.pauseChargingSession",
new=Mock(return_value=test_response), return_value=test_response,
), ),
): ):
result2 = await hass.config_entries.flow.async_configure( result2 = await hass.config_entries.flow.async_configure(
@ -135,20 +132,20 @@ async def test_form_validate_input(hass: HomeAssistant) -> None:
assert result2["data"]["station"] == "12345" assert result2["data"]["station"] == "12345"
async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None: async def test_form_reauth(
hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None:
"""Test we handle reauth flow.""" """Test we handle reauth flow."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with (
patch( patch.object(
"homeassistant.components.wallbox.Wallbox.authenticate", mock_wallbox,
new=Mock(return_value=authorisation_response_unauthorised), "authenticate",
), return_value=authorisation_response_unauthorised,
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(return_value=test_response),
), ),
patch.object(mock_wallbox, "getChargerStatus", return_value=test_response),
): ):
result = await entry.start_reauth_flow(hass) result = await entry.start_reauth_flow(hass)
@ -168,20 +165,20 @@ async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None:
await hass.config_entries.async_unload(entry.entry_id) await hass.config_entries.async_unload(entry.entry_id)
async def test_form_reauth_invalid(hass: HomeAssistant, entry: MockConfigEntry) -> None: async def test_form_reauth_invalid(
hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None:
"""Test we handle reauth invalid flow.""" """Test we handle reauth invalid flow."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with (
patch( patch.object(
"homeassistant.components.wallbox.Wallbox.authenticate", mock_wallbox,
new=Mock(return_value=authorisation_response_unauthorised), "authenticate",
), return_value=authorisation_response_unauthorised,
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(return_value=test_response),
), ),
patch.object(mock_wallbox, "getChargerStatus", return_value=test_response),
): ):
result = await entry.start_reauth_flow(hass) result = await entry.start_reauth_flow(hass)

View File

@ -1,27 +1,23 @@
"""Test Wallbox Init Component.""" """Test Wallbox Init Component."""
from unittest.mock import Mock, patch from unittest.mock import patch
from homeassistant.components.wallbox.const import DOMAIN from homeassistant.components.wallbox.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import ( from .conftest import (
authorisation_response,
http_403_error, http_403_error,
http_429_error, http_429_error,
setup_integration, setup_integration,
setup_integration_connection_error, test_response_no_power_boost,
setup_integration_no_eco_mode,
setup_integration_read_only,
test_response,
) )
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_wallbox_setup_unload_entry( async def test_wallbox_setup_unload_entry(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox Unload.""" """Test Wallbox Unload."""
@ -33,11 +29,11 @@ async def test_wallbox_setup_unload_entry(
async def test_wallbox_unload_entry_connection_error( async def test_wallbox_unload_entry_connection_error(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox Unload Connection Error.""" """Test Wallbox Unload Connection Error."""
with patch.object(mock_wallbox, "authenticate", side_effect=http_403_error):
await setup_integration_connection_error(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.SETUP_ERROR assert entry.state is ConfigEntryState.SETUP_ERROR
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)
@ -45,25 +41,15 @@ async def test_wallbox_unload_entry_connection_error(
async def test_wallbox_refresh_failed_connection_error_auth( async def test_wallbox_refresh_failed_connection_error_auth(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox setup with connection error.""" """Test Wallbox setup with connection error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with patch.object(mock_wallbox, "authenticate", side_effect=http_429_error):
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(side_effect=http_429_error),
),
patch(
"homeassistant.components.wallbox.Wallbox.pauseChargingSession",
new=Mock(return_value=test_response),
),
):
wallbox = hass.data[DOMAIN][entry.entry_id] wallbox = hass.data[DOMAIN][entry.entry_id]
await wallbox.async_refresh() await wallbox.async_refresh()
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)
@ -71,7 +57,7 @@ async def test_wallbox_refresh_failed_connection_error_auth(
async def test_wallbox_refresh_failed_invalid_auth( async def test_wallbox_refresh_failed_invalid_auth(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox setup with authentication error.""" """Test Wallbox setup with authentication error."""
@ -79,14 +65,8 @@ async def test_wallbox_refresh_failed_invalid_auth(
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with (
patch( patch.object(mock_wallbox, "authenticate", side_effect=http_403_error),
"homeassistant.components.wallbox.Wallbox.authenticate", patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error),
new=Mock(side_effect=http_403_error),
),
patch(
"homeassistant.components.wallbox.Wallbox.pauseChargingSession",
new=Mock(side_effect=http_403_error),
),
): ):
wallbox = hass.data[DOMAIN][entry.entry_id] wallbox = hass.data[DOMAIN][entry.entry_id]
@ -97,23 +77,14 @@ async def test_wallbox_refresh_failed_invalid_auth(
async def test_wallbox_refresh_failed_http_error( async def test_wallbox_refresh_failed_http_error(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox setup with authentication error.""" """Test Wallbox setup with authentication error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_403_error):
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(side_effect=http_403_error),
),
):
wallbox = hass.data[DOMAIN][entry.entry_id] wallbox = hass.data[DOMAIN][entry.entry_id]
await wallbox.async_refresh() await wallbox.async_refresh()
@ -123,23 +94,14 @@ async def test_wallbox_refresh_failed_http_error(
async def test_wallbox_refresh_failed_too_many_requests( async def test_wallbox_refresh_failed_too_many_requests(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox setup with authentication error.""" """Test Wallbox setup with authentication error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_429_error):
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(side_effect=http_429_error),
),
):
wallbox = hass.data[DOMAIN][entry.entry_id] wallbox = hass.data[DOMAIN][entry.entry_id]
await wallbox.async_refresh() await wallbox.async_refresh()
@ -149,23 +111,14 @@ async def test_wallbox_refresh_failed_too_many_requests(
async def test_wallbox_refresh_failed_connection_error( async def test_wallbox_refresh_failed_connection_error(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox setup with connection error.""" """Test Wallbox setup with connection error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
with ( with patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error):
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.pauseChargingSession",
new=Mock(side_effect=http_403_error),
),
):
wallbox = hass.data[DOMAIN][entry.entry_id] wallbox = hass.data[DOMAIN][entry.entry_id]
await wallbox.async_refresh() await wallbox.async_refresh()
@ -174,24 +127,14 @@ async def test_wallbox_refresh_failed_connection_error(
assert entry.state is ConfigEntryState.NOT_LOADED assert entry.state is ConfigEntryState.NOT_LOADED
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, entry)
assert entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id)
assert entry.state is ConfigEntryState.NOT_LOADED
async def test_wallbox_setup_load_entry_no_eco_mode( async def test_wallbox_setup_load_entry_no_eco_mode(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test Wallbox Unload.""" """Test Wallbox Unload."""
with patch.object(
await setup_integration_no_eco_mode(hass, entry) mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost
):
await setup_integration(hass, entry)
assert entry.state is ConfigEntryState.LOADED assert entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(entry.entry_id) assert await hass.config_entries.async_unload(entry.entry_id)

View File

@ -1,28 +1,24 @@
"""Test Wallbox Lock component.""" """Test Wallbox Lock component."""
from unittest.mock import Mock, patch from unittest.mock import patch
import pytest import pytest
from homeassistant.components.lock import SERVICE_LOCK, SERVICE_UNLOCK from homeassistant.components.lock import SERVICE_LOCK, SERVICE_UNLOCK
from homeassistant.components.wallbox.const import CHARGER_LOCKED_UNLOCKED_KEY from homeassistant.components.wallbox.coordinator import InvalidAuth
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from . import ( from .conftest import http_403_error, http_404_error, http_429_error, setup_integration
authorisation_response,
http_403_error,
http_404_error,
http_429_error,
setup_integration,
)
from .const import MOCK_LOCK_ENTITY_ID from .const import MOCK_LOCK_ENTITY_ID
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -> None: async def test_wallbox_lock_class(
hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None:
"""Test wallbox lock class.""" """Test wallbox lock class."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
@ -31,24 +27,6 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -
assert state assert state
assert state.state == "unlocked" assert state.state == "unlocked"
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.lockCharger",
new=Mock(
return_value={"data": {"chargerData": {CHARGER_LOCKED_UNLOCKED_KEY: 1}}}
),
),
patch(
"homeassistant.components.wallbox.Wallbox.unlockCharger",
new=Mock(
return_value={"data": {"chargerData": {CHARGER_LOCKED_UNLOCKED_KEY: 0}}}
),
),
):
await hass.services.async_call( await hass.services.async_call(
"lock", "lock",
SERVICE_LOCK, SERVICE_LOCK,
@ -68,23 +46,16 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -
) )
async def test_wallbox_lock_class_connection_error( async def test_wallbox_lock_class_error_handling(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox lock class connection error.""" """Test wallbox lock class connection error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error),
"homeassistant.components.wallbox.Wallbox.authenticate", pytest.raises(HomeAssistantError),
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.lockCharger",
new=Mock(side_effect=ConnectionError),
),
pytest.raises(ConnectionError),
): ):
await hass.services.async_call( await hass.services.async_call(
"lock", "lock",
@ -96,42 +67,8 @@ async def test_wallbox_lock_class_connection_error(
) )
with ( with (
patch( patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error),
"homeassistant.components.wallbox.Wallbox.authenticate", patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error),
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.lockCharger",
new=Mock(side_effect=ConnectionError),
),
patch(
"homeassistant.components.wallbox.Wallbox.unlockCharger",
new=Mock(side_effect=ConnectionError),
),
pytest.raises(ConnectionError),
):
await hass.services.async_call(
"lock",
SERVICE_UNLOCK,
{
ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
},
blocking=True,
)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.lockCharger",
new=Mock(side_effect=http_429_error),
),
patch(
"homeassistant.components.wallbox.Wallbox.unlockCharger",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -143,18 +80,8 @@ async def test_wallbox_lock_class_connection_error(
blocking=True, blocking=True,
) )
with ( with (
patch( patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error),
"homeassistant.components.wallbox.Wallbox.authenticate", patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error),
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.lockCharger",
new=Mock(side_effect=http_403_error),
),
patch(
"homeassistant.components.wallbox.Wallbox.unlockCharger",
new=Mock(side_effect=http_403_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -165,19 +92,24 @@ async def test_wallbox_lock_class_connection_error(
}, },
blocking=True, blocking=True,
) )
with ( with (
patch( patch.object(mock_wallbox, "lockCharger", side_effect=http_403_error),
"homeassistant.components.wallbox.Wallbox.authenticate", patch.object(mock_wallbox, "unlockCharger", side_effect=http_403_error),
new=Mock(return_value=authorisation_response), pytest.raises(InvalidAuth),
), ):
patch( await hass.services.async_call(
"homeassistant.components.wallbox.Wallbox.lockCharger", "lock",
new=Mock(side_effect=http_404_error), SERVICE_UNLOCK,
), {
patch( ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID,
"homeassistant.components.wallbox.Wallbox.unlockCharger", },
new=Mock(side_effect=http_404_error), blocking=True,
), )
with (
patch.object(mock_wallbox, "lockCharger", side_effect=http_429_error),
patch.object(mock_wallbox, "unlockCharger", side_effect=http_429_error),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(

View File

@ -1,28 +1,22 @@
"""Test Wallbox Switch component.""" """Test Wallbox Switch component."""
from unittest.mock import Mock, patch from unittest.mock import patch
import pytest import pytest
from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
from homeassistant.components.wallbox.const import (
CHARGER_ENERGY_PRICE_KEY,
CHARGER_MAX_CHARGING_CURRENT_KEY,
CHARGER_MAX_ICP_CURRENT_KEY,
)
from homeassistant.components.wallbox.coordinator import InvalidAuth from homeassistant.components.wallbox.coordinator import InvalidAuth
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from . import ( from .conftest import (
authorisation_response,
http_403_error, http_403_error,
http_404_error, http_404_error,
http_429_error, http_429_error,
setup_integration, setup_integration,
setup_integration_bidir, test_response_bidir,
) )
from .const import ( from .const import (
MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
@ -32,34 +26,13 @@ from .const import (
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
mock_wallbox = Mock()
mock_wallbox.authenticate = Mock(return_value=authorisation_response)
mock_wallbox.setEnergyCost = Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 1.1})
mock_wallbox.setMaxChargingCurrent = Mock(
return_value={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}
)
mock_wallbox.setIcpMaxCurrent = Mock(return_value={CHARGER_MAX_ICP_CURRENT_KEY: 10})
async def test_wallbox_number_power_class(
async def test_wallbox_number_class( hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
hass: HomeAssistant, entry: MockConfigEntry
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent",
new=Mock(
return_value={"data": {"chargerData": {"maxChargingCurrent": 20}}}
),
),
):
state = hass.states.get(MOCK_NUMBER_ENTITY_ID) state = hass.states.get(MOCK_NUMBER_ENTITY_ID)
assert state.attributes["min"] == 6 assert state.attributes["min"] == 6
assert state.attributes["max"] == 25 assert state.attributes["max"] == 25
@ -75,12 +48,14 @@ async def test_wallbox_number_class(
) )
async def test_wallbox_number_class_bidir( async def test_wallbox_number_power_class_bidir(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""
with patch.object(
await setup_integration_bidir(hass, entry) mock_wallbox, "getChargerStatus", return_value=test_response_bidir
):
await setup_integration(hass, entry)
state = hass.states.get(MOCK_NUMBER_ENTITY_ID) state = hass.states.get(MOCK_NUMBER_ENTITY_ID)
assert state.attributes["min"] == -25 assert state.attributes["min"] == -25
@ -88,22 +63,12 @@ async def test_wallbox_number_class_bidir(
async def test_wallbox_number_energy_class( async def test_wallbox_number_energy_class(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setEnergyCost",
new=Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 1.1}),
),
):
await hass.services.async_call( await hass.services.async_call(
"number", "number",
SERVICE_SET_VALUE, SERVICE_SET_VALUE,
@ -115,22 +80,33 @@ async def test_wallbox_number_energy_class(
) )
async def test_wallbox_number_class_connection_error( async def test_wallbox_number_icp_power_class(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID,
ATTR_VALUE: 10,
},
blocking=True,
)
async def test_wallbox_number_power_class_error_handling(
hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_404_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent",
new=Mock(side_effect=http_404_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -143,23 +119,8 @@ async def test_wallbox_number_class_connection_error(
blocking=True, blocking=True,
) )
async def test_wallbox_number_class_too_many_requests(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_429_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -172,167 +133,8 @@ async def test_wallbox_number_class_too_many_requests(
blocking=True, blocking=True,
) )
async def test_wallbox_number_class_energy_price_update_failed(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_403_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setEnergyCost",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
async def test_wallbox_number_class_energy_price_update_connection_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setEnergyCost",
new=Mock(side_effect=http_404_error),
),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
async def test_wallbox_number_class_energy_price_auth_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setEnergyCost",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
async def test_wallbox_number_class_icp_energy(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent",
new=Mock(return_value={"icp_max_current": 20}),
),
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID,
ATTR_VALUE: 10,
},
blocking=True,
)
async def test_wallbox_number_class_icp_energy_auth_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent",
new=Mock(side_effect=http_403_error),
),
pytest.raises(InvalidAuth),
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID,
ATTR_VALUE: 10,
},
blocking=True,
)
async def test_wallbox_number_class_energy_auth_error(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent",
new=Mock(side_effect=http_403_error),
),
pytest.raises(InvalidAuth), pytest.raises(InvalidAuth),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -346,22 +148,79 @@ async def test_wallbox_number_class_energy_auth_error(
) )
async def test_wallbox_number_class_icp_energy_connection_error( async def test_wallbox_number_energy_class_error_handling(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "setEnergyCost", side_effect=http_429_error),
"homeassistant.components.wallbox.Wallbox.authenticate", pytest.raises(HomeAssistantError),
new=Mock(return_value=authorisation_response), ):
), await hass.services.async_call(
patch( "number",
"homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", SERVICE_SET_VALUE,
new=Mock(side_effect=http_404_error), {
), ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
with (
patch.object(mock_wallbox, "setEnergyCost", side_effect=http_404_error),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
with (
patch.object(mock_wallbox, "setEnergyCost", side_effect=http_429_error),
pytest.raises(HomeAssistantError),
):
await hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID,
ATTR_VALUE: 1.1,
},
blocking=True,
)
async def test_wallbox_number_icp_power_class_error_handling(
hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with (
patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_403_error),
pytest.raises(InvalidAuth),
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID,
ATTR_VALUE: 10,
},
blocking=True,
)
with (
patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_404_error),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -374,23 +233,8 @@ async def test_wallbox_number_class_icp_energy_connection_error(
blocking=True, blocking=True,
) )
async def test_wallbox_number_class_icp_energy_too_many_request(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox sensor class."""
await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_429_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(

View File

@ -1,6 +1,6 @@
"""Test Wallbox Select component.""" """Test Wallbox Select component."""
from unittest.mock import Mock, patch from unittest.mock import patch
import pytest import pytest
@ -9,15 +9,14 @@ from homeassistant.components.select import (
DOMAIN as SELECT_DOMAIN, DOMAIN as SELECT_DOMAIN,
SERVICE_SELECT_OPTION, SERVICE_SELECT_OPTION,
) )
from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY, EcoSmartMode from homeassistant.components.wallbox.const import EcoSmartMode
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant, HomeAssistantError from homeassistant.core import HomeAssistant, HomeAssistantError
from . import ( from .conftest import (
authorisation_response,
http_404_error, http_404_error,
http_429_error, http_429_error,
setup_integration_select, setup_integration,
test_response, test_response,
test_response_eco_mode, test_response_eco_mode,
test_response_full_solar, test_response_full_solar,
@ -34,44 +33,13 @@ TEST_OPTIONS = [
] ]
@pytest.fixture
def mock_authenticate():
"""Fixture to patch Wallbox methods."""
with patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
):
yield
@pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) @pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS)
async def test_wallbox_select_solar_charging_class( async def test_wallbox_select_solar_charging_class(
hass: HomeAssistant, entry: MockConfigEntry, mode, response, mock_authenticate hass: HomeAssistant, entry: MockConfigEntry, mode, response, mock_wallbox
) -> None: ) -> None:
"""Test wallbox select class.""" """Test wallbox select class."""
with patch.object(mock_wallbox, "getChargerStatus", return_value=response):
if mode == EcoSmartMode.OFF: await setup_integration(hass, entry)
response = test_response
elif mode == EcoSmartMode.ECO_MODE:
response = test_response_eco_mode
elif mode == EcoSmartMode.FULL_SOLAR:
response = test_response_full_solar
with (
patch(
"homeassistant.components.wallbox.Wallbox.enableEcoSmart",
new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}),
),
patch(
"homeassistant.components.wallbox.Wallbox.disableEcoSmart",
new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}),
),
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(return_value=response),
),
):
await setup_integration_select(hass, entry, response)
await hass.services.async_call( await hass.services.async_call(
SELECT_DOMAIN, SELECT_DOMAIN,
@ -88,43 +56,35 @@ async def test_wallbox_select_solar_charging_class(
async def test_wallbox_select_no_power_boost_class( async def test_wallbox_select_no_power_boost_class(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox select class.""" """Test wallbox select class."""
await setup_integration_select(hass, entry, test_response_no_power_boost) with patch.object(
mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost
):
await setup_integration(hass, entry)
state = hass.states.get(MOCK_SELECT_ENTITY_ID) state = hass.states.get(MOCK_SELECT_ENTITY_ID)
assert state is None assert state is None
@pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) @pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS)
@pytest.mark.parametrize("error", [http_404_error, ConnectionError])
async def test_wallbox_select_class_error( async def test_wallbox_select_class_error(
hass: HomeAssistant, hass: HomeAssistant,
entry: MockConfigEntry, entry: MockConfigEntry,
mode, mode,
response, response,
error, mock_wallbox,
mock_authenticate,
) -> None: ) -> None:
"""Test wallbox select class connection error.""" """Test wallbox select class connection error."""
await setup_integration_select(hass, entry, response) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "getChargerStatus", return_value=response),
"homeassistant.components.wallbox.Wallbox.disableEcoSmart", patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_404_error),
new=Mock(side_effect=error), patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_404_error),
),
patch(
"homeassistant.components.wallbox.Wallbox.enableEcoSmart",
new=Mock(side_effect=error),
),
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(return_value=test_response_eco_mode),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(
@ -144,25 +104,45 @@ async def test_wallbox_select_too_many_requests_error(
entry: MockConfigEntry, entry: MockConfigEntry,
mode, mode,
response, response,
mock_authenticate, mock_wallbox,
) -> None: ) -> None:
"""Test wallbox select class connection error.""" """Test wallbox select class connection error."""
await setup_integration_select(hass, entry, response) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "getChargerStatus", return_value=response),
"homeassistant.components.wallbox.Wallbox.disableEcoSmart", patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_429_error),
new=Mock(side_effect=http_429_error), patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_429_error),
), pytest.raises(HomeAssistantError),
patch( ):
"homeassistant.components.wallbox.Wallbox.enableEcoSmart", await hass.services.async_call(
new=Mock(side_effect=http_429_error), SELECT_DOMAIN,
), SERVICE_SELECT_OPTION,
patch( {
"homeassistant.components.wallbox.Wallbox.getChargerStatus", ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID,
new=Mock(return_value=test_response_eco_mode), ATTR_OPTION: mode,
), },
blocking=True,
)
@pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS)
async def test_wallbox_select_connection_error(
hass: HomeAssistant,
entry: MockConfigEntry,
mode,
response,
mock_wallbox,
) -> None:
"""Test wallbox select class connection error."""
await setup_integration(hass, entry)
with (
patch.object(mock_wallbox, "getChargerStatus", return_value=response),
patch.object(mock_wallbox, "disableEcoSmart", side_effect=ConnectionError),
patch.object(mock_wallbox, "enableEcoSmart", side_effect=ConnectionError),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
await hass.services.async_call( await hass.services.async_call(

View File

@ -3,7 +3,7 @@
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, UnitOfPower from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, UnitOfPower
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import setup_integration from .conftest import setup_integration
from .const import ( from .const import (
MOCK_SENSOR_CHARGING_POWER_ID, MOCK_SENSOR_CHARGING_POWER_ID,
MOCK_SENSOR_CHARGING_SPEED_ID, MOCK_SENSOR_CHARGING_SPEED_ID,
@ -14,7 +14,7 @@ from tests.common import MockConfigEntry
async def test_wallbox_sensor_class( async def test_wallbox_sensor_class(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox sensor class.""" """Test wallbox sensor class."""

View File

@ -1,29 +1,22 @@
"""Test Wallbox Lock component.""" """Test Wallbox Lock component."""
from unittest.mock import Mock, patch from unittest.mock import patch
import pytest import pytest
from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from . import ( from .conftest import http_404_error, http_429_error, setup_integration
authorisation_response,
http_404_error,
http_429_error,
setup_integration,
test_response,
)
from .const import MOCK_SWITCH_ENTITY_ID from .const import MOCK_SWITCH_ENTITY_ID
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_wallbox_switch_class( async def test_wallbox_switch_class(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox switch class.""" """Test wallbox switch class."""
@ -33,24 +26,6 @@ async def test_wallbox_switch_class(
assert state assert state
assert state.state == "on" assert state.state == "on"
with (
patch(
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.pauseChargingSession",
new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}),
),
patch(
"homeassistant.components.wallbox.Wallbox.resumeChargingSession",
new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}),
),
patch(
"homeassistant.components.wallbox.Wallbox.getChargerStatus",
new=Mock(return_value=test_response),
),
):
await hass.services.async_call( await hass.services.async_call(
"switch", "switch",
SERVICE_TURN_ON, SERVICE_TURN_ON,
@ -70,22 +45,15 @@ async def test_wallbox_switch_class(
) )
async def test_wallbox_switch_class_connection_error( async def test_wallbox_switch_class_error_handling(
hass: HomeAssistant, entry: MockConfigEntry hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox
) -> None: ) -> None:
"""Test wallbox switch class connection error.""" """Test wallbox switch class connection error."""
await setup_integration(hass, entry) await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_404_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.resumeChargingSession",
new=Mock(side_effect=http_404_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
# Test behavior when a connection error occurs # Test behavior when a connection error occurs
@ -98,23 +66,8 @@ async def test_wallbox_switch_class_connection_error(
blocking=True, blocking=True,
) )
async def test_wallbox_switch_class_too_many_requests(
hass: HomeAssistant, entry: MockConfigEntry
) -> None:
"""Test wallbox switch class connection error."""
await setup_integration(hass, entry)
with ( with (
patch( patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_429_error),
"homeassistant.components.wallbox.Wallbox.authenticate",
new=Mock(return_value=authorisation_response),
),
patch(
"homeassistant.components.wallbox.Wallbox.resumeChargingSession",
new=Mock(side_effect=http_429_error),
),
pytest.raises(HomeAssistantError), pytest.raises(HomeAssistantError),
): ):
# Test behavior when a connection error occurs # Test behavior when a connection error occurs