mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Catch googlemaps exceptions in google_travel_time (#130903)
Catch googlemaps exceptions
This commit is contained in:
parent
999f3e0d77
commit
c7f0745f48
@ -7,6 +7,7 @@ import logging
|
||||
|
||||
from googlemaps import Client
|
||||
from googlemaps.distance_matrix import distance_matrix
|
||||
from googlemaps.exceptions import ApiError, Timeout, TransportError
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
@ -172,9 +173,13 @@ class GoogleTravelTimeSensor(SensorEntity):
|
||||
self._resolved_destination,
|
||||
)
|
||||
if self._resolved_destination is not None and self._resolved_origin is not None:
|
||||
self._matrix = distance_matrix(
|
||||
self._client,
|
||||
self._resolved_origin,
|
||||
self._resolved_destination,
|
||||
**options_copy,
|
||||
)
|
||||
try:
|
||||
self._matrix = distance_matrix(
|
||||
self._client,
|
||||
self._resolved_origin,
|
||||
self._resolved_destination,
|
||||
**options_copy,
|
||||
)
|
||||
except (ApiError, TransportError, Timeout) as ex:
|
||||
_LOGGER.error("Error getting travel time: %s", ex)
|
||||
self._matrix = None
|
||||
|
@ -3,6 +3,7 @@
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from googlemaps.exceptions import ApiError, Timeout, TransportError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.google_travel_time.config_flow import default_options
|
||||
@ -13,7 +14,9 @@ from homeassistant.components.google_travel_time.const import (
|
||||
UNITS_IMPERIAL,
|
||||
UNITS_METRIC,
|
||||
)
|
||||
from homeassistant.components.google_travel_time.sensor import SCAN_INTERVAL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.unit_system import (
|
||||
METRIC_SYSTEM,
|
||||
US_CUSTOMARY_SYSTEM,
|
||||
@ -22,7 +25,7 @@ from homeassistant.util.unit_system import (
|
||||
|
||||
from .const import MOCK_CONFIG
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_update")
|
||||
@ -240,3 +243,25 @@ async def test_sensor_unit_system(
|
||||
|
||||
distance_matrix_mock.assert_called_once()
|
||||
assert distance_matrix_mock.call_args.kwargs["units"] == expected_unit_option
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception"),
|
||||
[(ApiError), (TransportError), (Timeout)],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
("data", "options"),
|
||||
[(MOCK_CONFIG, {})],
|
||||
)
|
||||
async def test_sensor_exception(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
mock_update: MagicMock,
|
||||
mock_config: MagicMock,
|
||||
exception: Exception,
|
||||
) -> None:
|
||||
"""Test that exception gets caught."""
|
||||
mock_update.side_effect = exception("Errormessage")
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
assert "Error getting travel time" in caplog.text
|
||||
|
Loading…
x
Reference in New Issue
Block a user