diff --git a/homeassistant/components/fitbit/api.py b/homeassistant/components/fitbit/api.py index 49e51a0fd9863..0f49c0858f593 100644 --- a/homeassistant/components/fitbit/api.py +++ b/homeassistant/components/fitbit/api.py @@ -7,6 +7,7 @@ from fitbit import Fitbit from fitbit.exceptions import HTTPException, HTTPUnauthorized +from requests.exceptions import ConnectionError as RequestsConnectionError from homeassistant.const import CONF_ACCESS_TOKEN from homeassistant.core import HomeAssistant @@ -132,6 +133,9 @@ async def _run(self, func: Callable[[], _T]) -> _T: """Run client command.""" try: return await self._hass.async_add_executor_job(func) + except RequestsConnectionError as err: + _LOGGER.debug("Connection error to fitbit API: %s", err) + raise FitbitApiException("Connection error to fitbit API") from err except HTTPUnauthorized as err: _LOGGER.debug("Unauthorized error from fitbit API: %s", err) raise FitbitAuthException("Authentication error from fitbit API") from err diff --git a/tests/components/fitbit/test_sensor.py b/tests/components/fitbit/test_sensor.py index 91aafd944b085..59405e3ea9136 100644 --- a/tests/components/fitbit/test_sensor.py +++ b/tests/components/fitbit/test_sensor.py @@ -6,6 +6,7 @@ from typing import Any import pytest +from requests.exceptions import ConnectionError as RequestsConnectionError from requests_mock.mocker import Mocker from syrupy.assertion import SnapshotAssertion @@ -599,10 +600,11 @@ async def test_settings_scope_config_entry( @pytest.mark.parametrize( - ("scopes", "server_status"), + ("scopes", "request_condition"), [ - (["heartrate"], HTTPStatus.INTERNAL_SERVER_ERROR), - (["heartrate"], HTTPStatus.BAD_REQUEST), + (["heartrate"], {"status_code": HTTPStatus.INTERNAL_SERVER_ERROR}), + (["heartrate"], {"status_code": HTTPStatus.BAD_REQUEST}), + (["heartrate"], {"exc": RequestsConnectionError}), ], ) async def test_sensor_update_failed( @@ -610,14 +612,14 @@ async def test_sensor_update_failed( setup_credentials: None, integration_setup: Callable[[], Awaitable[bool]], requests_mock: Mocker, - server_status: HTTPStatus, + request_condition: dict[str, Any], ) -> None: """Test a failed sensor update when talking to the API.""" requests_mock.register_uri( "GET", TIMESERIES_API_URL_FORMAT.format(resource="activities/heart"), - status_code=server_status, + **request_condition, ) assert await integration_setup()