Skip to content

Commit c7f0745

Browse files
authored
Catch googlemaps exceptions in google_travel_time (#130903)
Catch googlemaps exceptions
1 parent 999f3e0 commit c7f0745

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

homeassistant/components/google_travel_time/sensor.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from googlemaps import Client
99
from googlemaps.distance_matrix import distance_matrix
10+
from googlemaps.exceptions import ApiError, Timeout, TransportError
1011

1112
from homeassistant.components.sensor import (
1213
SensorDeviceClass,
@@ -172,9 +173,13 @@ def update(self) -> None:
172173
self._resolved_destination,
173174
)
174175
if self._resolved_destination is not None and self._resolved_origin is not None:
175-
self._matrix = distance_matrix(
176-
self._client,
177-
self._resolved_origin,
178-
self._resolved_destination,
179-
**options_copy,
180-
)
176+
try:
177+
self._matrix = distance_matrix(
178+
self._client,
179+
self._resolved_origin,
180+
self._resolved_destination,
181+
**options_copy,
182+
)
183+
except (ApiError, TransportError, Timeout) as ex:
184+
_LOGGER.error("Error getting travel time: %s", ex)
185+
self._matrix = None

tests/components/google_travel_time/test_sensor.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import Generator
44
from unittest.mock import MagicMock, patch
55

6+
from googlemaps.exceptions import ApiError, Timeout, TransportError
67
import pytest
78

89
from homeassistant.components.google_travel_time.config_flow import default_options
@@ -13,7 +14,9 @@
1314
UNITS_IMPERIAL,
1415
UNITS_METRIC,
1516
)
17+
from homeassistant.components.google_travel_time.sensor import SCAN_INTERVAL
1618
from homeassistant.core import HomeAssistant
19+
from homeassistant.util import dt as dt_util
1720
from homeassistant.util.unit_system import (
1821
METRIC_SYSTEM,
1922
US_CUSTOMARY_SYSTEM,
@@ -22,7 +25,7 @@
2225

2326
from .const import MOCK_CONFIG
2427

25-
from tests.common import MockConfigEntry
28+
from tests.common import MockConfigEntry, async_fire_time_changed
2629

2730

2831
@pytest.fixture(name="mock_update")
@@ -240,3 +243,25 @@ async def test_sensor_unit_system(
240243

241244
distance_matrix_mock.assert_called_once()
242245
assert distance_matrix_mock.call_args.kwargs["units"] == expected_unit_option
246+
247+
248+
@pytest.mark.parametrize(
249+
("exception"),
250+
[(ApiError), (TransportError), (Timeout)],
251+
)
252+
@pytest.mark.parametrize(
253+
("data", "options"),
254+
[(MOCK_CONFIG, {})],
255+
)
256+
async def test_sensor_exception(
257+
hass: HomeAssistant,
258+
caplog: pytest.LogCaptureFixture,
259+
mock_update: MagicMock,
260+
mock_config: MagicMock,
261+
exception: Exception,
262+
) -> None:
263+
"""Test that exception gets caught."""
264+
mock_update.side_effect = exception("Errormessage")
265+
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
266+
await hass.async_block_till_done()
267+
assert "Error getting travel time" in caplog.text

0 commit comments

Comments
 (0)