From bfccee17efffc29163f4e0b200915fd99728aa1f Mon Sep 17 00:00:00 2001 From: Hessel Date: Fri, 4 Jul 2025 21:56:44 +0200 Subject: [PATCH] Wallbox, Improve test setup (#148036) --- tests/components/wallbox/__init__.py | 386 ----------------- tests/components/wallbox/conftest.py | 254 ++++++++++- tests/components/wallbox/test_config_flow.py | 57 ++- tests/components/wallbox/test_init.py | 115 ++--- tests/components/wallbox/test_lock.py | 162 ++----- tests/components/wallbox/test_number.py | 424 ++++++------------- tests/components/wallbox/test_select.py | 126 +++--- tests/components/wallbox/test_sensor.py | 4 +- tests/components/wallbox/test_switch.py | 93 +--- 9 files changed, 567 insertions(+), 1054 deletions(-) diff --git a/tests/components/wallbox/__init__.py b/tests/components/wallbox/__init__.py index 37e7d5059f0..35bf3cee242 100644 --- a/tests/components/wallbox/__init__.py +++ b/tests/components/wallbox/__init__.py @@ -1,387 +1 @@ """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() diff --git a/tests/components/wallbox/conftest.py b/tests/components/wallbox/conftest.py index 72d493ceb69..ab1032b3816 100644 --- a/tests/components/wallbox/conftest.py +++ b/tests/components/wallbox/conftest.py @@ -1,13 +1,220 @@ """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.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_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 def entry(hass: HomeAssistant) -> MockConfigEntry: @@ -23,3 +230,46 @@ def entry(hass: HomeAssistant) -> MockConfigEntry: ) entry.add_to_hass(hass) 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() diff --git a/tests/components/wallbox/test_config_flow.py b/tests/components/wallbox/test_config_flow.py index bdfb4cad18d..d0c34ae0bce 100644 --- a/tests/components/wallbox/test_config_flow.py +++ b/tests/components/wallbox/test_config_flow.py @@ -18,7 +18,7 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import ( +from .conftest import ( authorisation_response, authorisation_response_unauthorised, 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.""" flow = config_flow.WallboxConfigFlow() flow.hass = hass @@ -53,7 +53,6 @@ async def test_form_cannot_authenticate(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", @@ -73,8 +72,8 @@ async def test_form_cannot_authenticate(hass: HomeAssistant) -> None: }, ) - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "invalid_auth"} + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "invalid_auth"} async def test_form_cannot_connect(hass: HomeAssistant) -> None: @@ -82,7 +81,6 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", @@ -102,8 +100,8 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: }, ) - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "cannot_connect"} async def test_form_validate_input(hass: HomeAssistant) -> None: @@ -111,15 +109,14 @@ async def test_form_validate_input(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), + return_value=authorisation_response, ), patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + "homeassistant.components.wallbox.Wallbox.pauseChargingSession", + return_value=test_response, ), ): 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" -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.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response_unauthorised), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + patch.object( + mock_wallbox, + "authenticate", + return_value=authorisation_response_unauthorised, ), + patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): result = await entry.start_reauth_flow(hass) @@ -161,27 +158,27 @@ async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None: }, ) - assert result2["type"] is FlowResultType.ABORT - assert result2["reason"] == "reauth_successful" + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reauth_successful" await hass.async_block_till_done() 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.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response_unauthorised), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + patch.object( + mock_wallbox, + "authenticate", + return_value=authorisation_response_unauthorised, ), + patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): result = await entry.start_reauth_flow(hass) diff --git a/tests/components/wallbox/test_init.py b/tests/components/wallbox/test_init.py index 5048385aaf6..ef73decea8f 100644 --- a/tests/components/wallbox/test_init.py +++ b/tests/components/wallbox/test_init.py @@ -1,27 +1,23 @@ """Test Wallbox Init Component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch from homeassistant.components.wallbox.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant -from . import ( - authorisation_response, +from .conftest import ( http_403_error, http_429_error, setup_integration, - setup_integration_connection_error, - setup_integration_no_eco_mode, - setup_integration_read_only, - test_response, + test_response_no_power_boost, ) from tests.common import MockConfigEntry async def test_wallbox_setup_unload_entry( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload.""" @@ -33,37 +29,27 @@ async def test_wallbox_setup_unload_entry( async def test_wallbox_unload_entry_connection_error( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload Connection Error.""" + with patch.object(mock_wallbox, "authenticate", side_effect=http_403_error): + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.SETUP_ERROR - await setup_integration_connection_error(hass, entry) - assert entry.state is ConfigEntryState.SETUP_ERROR - - assert await hass.config_entries.async_unload(entry.entry_id) - assert entry.state is ConfigEntryState.NOT_LOADED + assert await hass.config_entries.async_unload(entry.entry_id) + assert entry.state is ConfigEntryState.NOT_LOADED async def test_wallbox_refresh_failed_connection_error_auth( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with connection error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - 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), - ), - ): + with patch.object(mock_wallbox, "authenticate", side_effect=http_429_error): wallbox = hass.data[DOMAIN][entry.entry_id] - await wallbox.async_refresh() 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( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" @@ -79,14 +65,8 @@ async def test_wallbox_refresh_failed_invalid_auth( assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(side_effect=http_403_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.pauseChargingSession", - new=Mock(side_effect=http_403_error), - ), + patch.object(mock_wallbox, "authenticate", side_effect=http_403_error), + patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error), ): 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( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - 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), - ), - ): + with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_403_error): wallbox = hass.data[DOMAIN][entry.entry_id] 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( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - 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), - ), - ): + with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_429_error): wallbox = hass.data[DOMAIN][entry.entry_id] 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( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with connection error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - 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), - ), - ): + with patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error): wallbox = hass.data[DOMAIN][entry.entry_id] await wallbox.async_refresh() @@ -174,25 +127,15 @@ async def test_wallbox_refresh_failed_connection_error( 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( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload.""" + with patch.object( + mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost + ): + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.LOADED - await setup_integration_no_eco_mode(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 + assert await hass.config_entries.async_unload(entry.entry_id) + assert entry.state is ConfigEntryState.NOT_LOADED diff --git a/tests/components/wallbox/test_lock.py b/tests/components/wallbox/test_lock.py index 7d95aed7a5d..e3c6048e928 100644 --- a/tests/components/wallbox/test_lock.py +++ b/tests/components/wallbox/test_lock.py @@ -1,28 +1,24 @@ """Test Wallbox Lock component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest 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.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, - http_403_error, - http_404_error, - http_429_error, - setup_integration, -) +from .conftest import http_403_error, http_404_error, http_429_error, setup_integration from .const import MOCK_LOCK_ENTITY_ID 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.""" await setup_integration(hass, entry) @@ -31,60 +27,35 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) - assert state 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( - "lock", - SERVICE_LOCK, - { - ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "lock", + SERVICE_LOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + blocking=True, + ) - await hass.services.async_call( - "lock", - SERVICE_UNLOCK, - { - ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "lock", + SERVICE_UNLOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + blocking=True, + ) -async def test_wallbox_lock_class_connection_error( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_lock_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox lock class connection error.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=ConnectionError), - ), - pytest.raises(ConnectionError), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + pytest.raises(HomeAssistantError), ): await hass.services.async_call( "lock", @@ -96,42 +67,8 @@ async def test_wallbox_lock_class_connection_error( ) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - 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), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -143,18 +80,8 @@ async def test_wallbox_lock_class_connection_error( 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_403_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=http_403_error), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -165,19 +92,24 @@ async def test_wallbox_lock_class_connection_error( }, 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_404_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_403_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_403_error), + pytest.raises(InvalidAuth), + ): + await hass.services.async_call( + "lock", + SERVICE_UNLOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + 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), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_number.py b/tests/components/wallbox/test_number.py index 8067917977d..3aba0792baa 100644 --- a/tests/components/wallbox/test_number.py +++ b/tests/components/wallbox/test_number.py @@ -1,28 +1,22 @@ """Test Wallbox Switch component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE 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.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, +from .conftest import ( http_403_error, http_404_error, http_429_error, setup_integration, - setup_integration_bidir, + test_response_bidir, ) from .const import ( MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, @@ -32,105 +26,87 @@ from .const import ( 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_class( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_power_class( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> 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( - return_value={"data": {"chargerData": {"maxChargingCurrent": 20}}} - ), - ), - ): - state = hass.states.get(MOCK_NUMBER_ENTITY_ID) - assert state.attributes["min"] == 6 - assert state.attributes["max"] == 25 + state = hass.states.get(MOCK_NUMBER_ENTITY_ID) + assert state.attributes["min"] == 6 + assert state.attributes["max"] == 25 - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ID, - ATTR_VALUE: 20, - }, - blocking=True, - ) + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ID, + ATTR_VALUE: 20, + }, + blocking=True, + ) -async def test_wallbox_number_class_bidir( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_power_class_bidir( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" + with patch.object( + mock_wallbox, "getChargerStatus", return_value=test_response_bidir + ): + await setup_integration(hass, entry) - await setup_integration_bidir(hass, entry) - - state = hass.states.get(MOCK_NUMBER_ENTITY_ID) - assert state.attributes["min"] == -25 - assert state.attributes["max"] == 25 + state = hass.states.get(MOCK_NUMBER_ENTITY_ID) + assert state.attributes["min"] == -25 + assert state.attributes["max"] == 25 async def test_wallbox_number_energy_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", + 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( + 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: """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(return_value={CHARGER_ENERGY_PRICE_KEY: 1.1}), - ), - ): - 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_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.setMaxChargingCurrent", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -143,23 +119,8 @@ async def test_wallbox_number_class_connection_error( 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 ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -172,167 +133,8 @@ async def test_wallbox_number_class_too_many_requests( 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 ( - 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_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), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_403_error), pytest.raises(InvalidAuth), ): 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( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_energy_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> 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_404_error), - ), + 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, + ) + + 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), ): await hass.services.async_call( @@ -374,23 +233,8 @@ async def test_wallbox_number_class_icp_energy_connection_error( 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 ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_select.py b/tests/components/wallbox/test_select.py index e46347bfa5a..f194566dbae 100644 --- a/tests/components/wallbox/test_select.py +++ b/tests/components/wallbox/test_select.py @@ -1,6 +1,6 @@ """Test Wallbox Select component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest @@ -9,15 +9,14 @@ from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, 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.core import HomeAssistant, HomeAssistantError -from . import ( - authorisation_response, +from .conftest import ( http_404_error, http_429_error, - setup_integration_select, + setup_integration, test_response, test_response_eco_mode, 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) 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: """Test wallbox select class.""" - - if mode == EcoSmartMode.OFF: - 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) + with patch.object(mock_wallbox, "getChargerStatus", return_value=response): + await setup_integration(hass, entry) await hass.services.async_call( SELECT_DOMAIN, @@ -88,43 +56,35 @@ async def test_wallbox_select_solar_charging_class( async def test_wallbox_select_no_power_boost_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """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) - assert state is None + state = hass.states.get(MOCK_SELECT_ENTITY_ID) + assert state is None @pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) -@pytest.mark.parametrize("error", [http_404_error, ConnectionError]) async def test_wallbox_select_class_error( hass: HomeAssistant, entry: MockConfigEntry, mode, response, - error, - mock_authenticate, + mock_wallbox, ) -> None: """Test wallbox select class connection error.""" - await setup_integration_select(hass, entry, response) + await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.disableEcoSmart", - new=Mock(side_effect=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), - ), + patch.object(mock_wallbox, "getChargerStatus", return_value=response), + patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_404_error), + patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -144,25 +104,45 @@ async def test_wallbox_select_too_many_requests_error( entry: MockConfigEntry, mode, response, - mock_authenticate, + mock_wallbox, ) -> None: """Test wallbox select class connection error.""" - await setup_integration_select(hass, entry, response) + await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.disableEcoSmart", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.enableEcoSmart", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response_eco_mode), - ), + patch.object(mock_wallbox, "getChargerStatus", return_value=response), + patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_429_error), + patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_429_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, + 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), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_sensor.py b/tests/components/wallbox/test_sensor.py index 69d0cc57340..7373b5e70bb 100644 --- a/tests/components/wallbox/test_sensor.py +++ b/tests/components/wallbox/test_sensor.py @@ -3,7 +3,7 @@ from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, UnitOfPower from homeassistant.core import HomeAssistant -from . import setup_integration +from .conftest import setup_integration from .const import ( MOCK_SENSOR_CHARGING_POWER_ID, MOCK_SENSOR_CHARGING_SPEED_ID, @@ -14,7 +14,7 @@ from tests.common import MockConfigEntry async def test_wallbox_sensor_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" diff --git a/tests/components/wallbox/test_switch.py b/tests/components/wallbox/test_switch.py index 98b87828f74..189ce59f55c 100644 --- a/tests/components/wallbox/test_switch.py +++ b/tests/components/wallbox/test_switch.py @@ -1,29 +1,22 @@ """Test Wallbox Lock component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest 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.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, - http_404_error, - http_429_error, - setup_integration, - test_response, -) +from .conftest import http_404_error, http_429_error, setup_integration from .const import MOCK_SWITCH_ENTITY_ID from tests.common import MockConfigEntry async def test_wallbox_switch_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox switch class.""" @@ -33,59 +26,34 @@ async def test_wallbox_switch_class( assert state 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( - "switch", - SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "switch", + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, + }, + blocking=True, + ) - await hass.services.async_call( - "switch", - SERVICE_TURN_OFF, - { - ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "switch", + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, + }, + blocking=True, + ) -async def test_wallbox_switch_class_connection_error( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_switch_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox switch class connection error.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.resumeChargingSession", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): # Test behavior when a connection error occurs @@ -98,23 +66,8 @@ async def test_wallbox_switch_class_connection_error( 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 ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.resumeChargingSession", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): # Test behavior when a connection error occurs