From a534c136a1f7dec97fbe27b67e5c40ec5596df88 Mon Sep 17 00:00:00 2001 From: hesselonline Date: Fri, 14 Oct 2022 13:09:00 +0200 Subject: [PATCH] Fix wallbox jwt issue (#79948) * Bump Wallbox package * remove debug message * Force update of auth token by emptying it first * Force token refresh by emptying token Improve exception handling * include tests * Update __init__.py * Removed the clearing ot jwt token, issue is fixed by upstream fix in wallbox package. * Catch connectionerror * Update homeassistant/components/wallbox/__init__.py Co-authored-by: Martin Hjelmare * Run black Co-authored-by: Martin Hjelmare --- homeassistant/components/wallbox/__init__.py | 11 +++++--- homeassistant/components/wallbox/lock.py | 3 +++ .../components/wallbox/manifest.json | 2 +- homeassistant/components/wallbox/number.py | 3 +++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/wallbox/__init__.py | 26 +++++++++++++++++++ tests/components/wallbox/test_lock.py | 13 ++++++++++ tests/components/wallbox/test_number.py | 19 +++++++++++++- 9 files changed, 73 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/wallbox/__init__.py b/homeassistant/components/wallbox/__init__.py index 9175f42827d..6382cf05940 100644 --- a/homeassistant/components/wallbox/__init__.py +++ b/homeassistant/components/wallbox/__init__.py @@ -17,6 +17,7 @@ from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, + UpdateFailed, ) from .const import ( @@ -93,6 +94,7 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Authenticate using Wallbox API.""" try: self._wallbox.authenticate() + except requests.exceptions.HTTPError as wallbox_connection_error: if wallbox_connection_error.response.status_code == HTTPStatus.FORBIDDEN: raise ConfigEntryAuthFailed from wallbox_connection_error @@ -125,11 +127,12 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): data[CHARGER_STATUS_DESCRIPTION_KEY] = CHARGER_STATUS.get( data[CHARGER_STATUS_ID_KEY], ChargerStatus.UNKNOWN ) - return data - - except requests.exceptions.HTTPError as wallbox_connection_error: - raise ConnectionError from wallbox_connection_error + except ( + ConnectionError, + requests.exceptions.HTTPError, + ) as wallbox_connection_error: + raise UpdateFailed from wallbox_connection_error async def _async_update_data(self) -> dict[str, Any]: """Get new sensor data for Wallbox component.""" diff --git a/homeassistant/components/wallbox/lock.py b/homeassistant/components/wallbox/lock.py index d1b3c787735..7b5dca58010 100644 --- a/homeassistant/components/wallbox/lock.py +++ b/homeassistant/components/wallbox/lock.py @@ -6,6 +6,7 @@ from typing import Any from homeassistant.components.lock import LockEntity, LockEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import InvalidAuth, WallboxCoordinator, WallboxEntity @@ -36,6 +37,8 @@ async def async_setup_entry( ) except InvalidAuth: return + except ConnectionError as exc: + raise PlatformNotReady from exc async_add_entities( [ diff --git a/homeassistant/components/wallbox/manifest.json b/homeassistant/components/wallbox/manifest.json index 5c195b8bfce..433a759bea5 100644 --- a/homeassistant/components/wallbox/manifest.json +++ b/homeassistant/components/wallbox/manifest.json @@ -3,7 +3,7 @@ "name": "Wallbox", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/wallbox", - "requirements": ["wallbox==0.4.9"], + "requirements": ["wallbox==0.4.10"], "codeowners": ["@hesselonline"], "iot_class": "cloud_polling", "loggers": ["wallbox"] diff --git a/homeassistant/components/wallbox/number.py b/homeassistant/components/wallbox/number.py index 5470ec11532..04db0ad9f7c 100644 --- a/homeassistant/components/wallbox/number.py +++ b/homeassistant/components/wallbox/number.py @@ -7,6 +7,7 @@ from typing import Optional, cast from homeassistant.components.number import NumberEntity, NumberEntityDescription from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import InvalidAuth, WallboxCoordinator, WallboxEntity @@ -46,6 +47,8 @@ async def async_setup_entry( ) except InvalidAuth: return + except ConnectionError as exc: + raise PlatformNotReady from exc async_add_entities( [ diff --git a/requirements_all.txt b/requirements_all.txt index c8e5427e2c7..b6bc47c6141 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2512,7 +2512,7 @@ vultr==0.1.2 wakeonlan==2.1.0 # homeassistant.components.wallbox -wallbox==0.4.9 +wallbox==0.4.10 # homeassistant.components.waqi waqiasync==1.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 884c57df5dc..588a41eaa62 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1740,7 +1740,7 @@ vultr==0.1.2 wakeonlan==2.1.0 # homeassistant.components.wallbox -wallbox==0.4.9 +wallbox==0.4.10 # homeassistant.components.folder_watcher watchdog==2.1.9 diff --git a/tests/components/wallbox/__init__.py b/tests/components/wallbox/__init__.py index 8c979c42ebe..53af3b6383d 100644 --- a/tests/components/wallbox/__init__.py +++ b/tests/components/wallbox/__init__.py @@ -173,3 +173,29 @@ async def setup_integration_read_only(hass: HomeAssistant) -> None: await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + + +async def setup_integration_platform_not_ready(hass: HomeAssistant) -> None: + """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, + ) + + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/wallbox/test_lock.py b/tests/components/wallbox/test_lock.py index 567d92757cd..bf0daa5c828 100644 --- a/tests/components/wallbox/test_lock.py +++ b/tests/components/wallbox/test_lock.py @@ -13,6 +13,7 @@ from . import ( authorisation_response, entry, setup_integration, + setup_integration_platform_not_ready, setup_integration_read_only, ) from .const import MOCK_LOCK_ENTITY_ID @@ -109,3 +110,15 @@ async def test_wallbox_lock_class_authentication_error(hass: HomeAssistant) -> N assert state is None await hass.config_entries.async_unload(entry.entry_id) + + +async def test_wallbox_lock_class_platform_not_ready(hass: HomeAssistant) -> None: + """Test wallbox lock not loaded on authentication error.""" + + await setup_integration_platform_not_ready(hass) + + state = hass.states.get(MOCK_LOCK_ENTITY_ID) + + assert state is None + + await hass.config_entries.async_unload(entry.entry_id) diff --git a/tests/components/wallbox/test_number.py b/tests/components/wallbox/test_number.py index 58e3450e6aa..c16c85d3696 100644 --- a/tests/components/wallbox/test_number.py +++ b/tests/components/wallbox/test_number.py @@ -9,7 +9,12 @@ from homeassistant.components.wallbox import CHARGER_MAX_CHARGING_CURRENT_KEY from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant -from . import authorisation_response, entry, setup_integration +from . import ( + authorisation_response, + entry, + setup_integration, + setup_integration_platform_not_ready, +) from .const import MOCK_NUMBER_ENTITY_ID @@ -71,3 +76,15 @@ async def test_wallbox_number_class_connection_error(hass: HomeAssistant) -> Non blocking=True, ) await hass.config_entries.async_unload(entry.entry_id) + + +async def test_wallbox_number_class_platform_not_ready(hass: HomeAssistant) -> None: + """Test wallbox lock not loaded on authentication error.""" + + await setup_integration_platform_not_ready(hass) + + state = hass.states.get(MOCK_NUMBER_ENTITY_ID) + + assert state is None + + await hass.config_entries.async_unload(entry.entry_id)