Handle special transit errors for here_travel_time (#83649)

Handle special transit errors
This commit is contained in:
Kevin Stillhammer 2022-12-10 03:09:55 +01:00 committed by GitHub
parent 2a6162a34d
commit ab3f0fc63b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 22 deletions

View file

@ -1,8 +1,6 @@
"""The HERE Travel Time integration."""
from __future__ import annotations
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_MODE, Platform
from homeassistant.core import HomeAssistant
@ -28,7 +26,6 @@ from .coordinator import (
from .model import HERETravelTimeConfig
PLATFORMS = [Platform.SENSOR]
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:

View file

@ -7,7 +7,12 @@ import logging
import here_routing
from here_routing import HERERoutingApi, Return, RoutingMode, Spans, TransportMode
import here_transit
from here_transit import HERETransitApi
from here_transit import (
HERETransitApi,
HERETransitConnectionError,
HERETransitDepartureArrivalTooCloseError,
HERETransitNoRouteFoundError,
)
import voluptuous as vol
from homeassistant.const import ATTR_ATTRIBUTION, UnitOfLength
@ -154,23 +159,28 @@ class HERETransitDataUpdateCoordinator(DataUpdateCoordinator):
arrival,
departure,
)
try:
response = await self._api.route(
origin=here_transit.Place(latitude=origin[0], longitude=origin[1]),
destination=here_transit.Place(
latitude=destination[0], longitude=destination[1]
),
arrival_time=arrival,
departure_time=departure,
return_values=[
here_transit.Return.POLYLINE,
here_transit.Return.TRAVEL_SUMMARY,
],
)
response = await self._api.route(
origin=here_transit.Place(latitude=origin[0], longitude=origin[1]),
destination=here_transit.Place(
latitude=destination[0], longitude=destination[1]
),
arrival_time=arrival,
departure_time=departure,
return_values=[
here_transit.Return.POLYLINE,
here_transit.Return.TRAVEL_SUMMARY,
],
)
_LOGGER.debug("Raw response is: %s", response)
_LOGGER.debug("Raw response is: %s", response)
return self._parse_transit_response(response)
return self._parse_transit_response(response)
except HERETransitDepartureArrivalTooCloseError:
_LOGGER.debug("Ignoring HERETransitDepartureArrivalTooCloseError")
return None
except (HERETransitConnectionError, HERETransitNoRouteFoundError) as error:
raise UpdateFailed from error
def _parse_transit_response(self, response) -> HERETravelTimeData:
"""Parse the transit response dict to a HERETravelTimeData."""

View file

@ -3,7 +3,7 @@
"name": "HERE Travel Time",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/here_travel_time",
"requirements": ["here_routing==0.1.1", "here_transit==1.0.0"],
"requirements": ["here_routing==0.1.1", "here_transit==1.1.1"],
"codeowners": ["@eifinger"],
"iot_class": "cloud_polling",
"loggers": ["here_routing", "here_transit"]

View file

@ -866,7 +866,7 @@ heatmiserV3==1.1.18
here_routing==0.1.1
# homeassistant.components.here_travel_time
here_transit==1.0.0
here_transit==1.1.1
# homeassistant.components.hikvisioncam
hikvision==0.4

View file

@ -652,7 +652,7 @@ hdate==0.10.4
here_routing==0.1.1
# homeassistant.components.here_travel_time
here_transit==1.0.0
here_transit==1.1.1
# homeassistant.components.hlk_sw16
hlk-sw16==0.0.9

View file

@ -9,6 +9,11 @@ from here_routing import (
Spans,
TransportMode,
)
from here_transit import (
HERETransitDepartureArrivalTooCloseError,
HERETransitNoRouteFoundError,
HERETransitNoTransitRouteFoundError,
)
import pytest
from homeassistant.components.here_travel_time.config_flow import DEFAULT_OPTIONS
@ -583,3 +588,49 @@ async def test_restore_state(hass):
state = hass.states.get("sensor.test_destination")
assert state.state == "Destination Address 1"
@pytest.mark.parametrize(
"exception,expected_message",
[
(
HERETransitNoRouteFoundError,
"Error fetching here_travel_time data",
),
(
HERETransitNoTransitRouteFoundError,
"Error fetching here_travel_time data",
),
(
HERETransitDepartureArrivalTooCloseError,
"Ignoring HERETransitDepartureArrivalTooCloseError",
),
],
)
async def test_transit_errors(hass: HomeAssistant, caplog, exception, expected_message):
"""Test that transit errors are correctly handled."""
with patch(
"here_transit.HERETransitApi.route",
side_effect=exception(),
):
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="0123456789",
data={
CONF_ORIGIN_LATITUDE: float(ORIGIN_LATITUDE),
CONF_ORIGIN_LONGITUDE: float(ORIGIN_LONGITUDE),
CONF_DESTINATION_LATITUDE: float(DESTINATION_LATITUDE),
CONF_DESTINATION_LONGITUDE: float(DESTINATION_LONGITUDE),
CONF_API_KEY: API_KEY,
CONF_MODE: TRAVEL_MODE_PUBLIC,
CONF_NAME: "test",
},
options=DEFAULT_OPTIONS,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
assert expected_message in caplog.text