Skip to content

Commit

Permalink
Decrease fitbit logging verbosity on connection error (home-assistant…
Browse files Browse the repository at this point in the history
…#108228)

* Add test for connection error

* Decrease fitbit connection error log verbosity
  • Loading branch information
MartinHjelmare authored Jan 17, 2024
1 parent c47fb5d commit 9d5f714
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions homeassistant/components/fitbit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tests/components/fitbit/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -599,25 +600,26 @@ 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(
hass: HomeAssistant,
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()
Expand Down

0 comments on commit 9d5f714

Please sign in to comment.