mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add static_duration to google_travel_time action responses (#188308)
This commit is contained in:
@@ -195,7 +195,9 @@ async def async_compute_routes(
|
||||
transit_routing_preference: str | None = None,
|
||||
departure_time: str | None = None,
|
||||
arrival_time: str | None = None,
|
||||
field_mask: str = ("routes.duration,routes.distanceMeters,routes.localizedValues"),
|
||||
field_mask: str = (
|
||||
"routes.duration,routes.staticDuration,routes.distanceMeters,routes.localizedValues"
|
||||
),
|
||||
) -> ComputeRoutesResponse | None:
|
||||
"""Compute routes using Google Routes API."""
|
||||
origin_waypoint = convert_to_waypoint(hass, origin)
|
||||
|
||||
@@ -62,10 +62,15 @@ def _build_routes_response(
|
||||
return []
|
||||
return [
|
||||
{
|
||||
"duration": _seconds(route.duration),
|
||||
"duration": (duration := _seconds(route.duration)),
|
||||
"duration_text": get_localized_text(
|
||||
route.localized_values.duration if route.localized_values else None
|
||||
),
|
||||
"static_duration": (
|
||||
_seconds(route.static_duration)
|
||||
if route.static_duration is not None
|
||||
else duration
|
||||
),
|
||||
"static_duration_text": get_localized_text(
|
||||
route.localized_values.static_duration
|
||||
if route.localized_values
|
||||
|
||||
@@ -54,6 +54,7 @@ def routes_mock() -> Generator[AsyncMock]:
|
||||
{
|
||||
"distanceMeters": 21300,
|
||||
"duration": "1620s",
|
||||
"staticDuration": "1560s",
|
||||
"localizedValues": {
|
||||
"distance": {"text": "21.3 km"},
|
||||
"duration": {"text": "27 mins"},
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from google_maps_routing_api import (
|
||||
ComputeRoutesResponse,
|
||||
GoogleMapsRoutingApiAuthError,
|
||||
GoogleMapsRoutingApiConnectionTimeoutError,
|
||||
GoogleMapsRoutingApiError,
|
||||
@@ -50,6 +51,7 @@ async def test_service_get_travel_times(
|
||||
{
|
||||
"duration": 1620,
|
||||
"duration_text": "27 mins",
|
||||
"static_duration": 1560,
|
||||
"static_duration_text": "26 mins",
|
||||
"distance_meters": 21300,
|
||||
"distance_text": "21.3 km",
|
||||
@@ -204,6 +206,7 @@ async def test_service_get_transit_times(
|
||||
{
|
||||
"duration": 1620,
|
||||
"duration_text": "27 mins",
|
||||
"static_duration": 1560,
|
||||
"static_duration_text": "26 mins",
|
||||
"distance_meters": 21300,
|
||||
"distance_text": "21.3 km",
|
||||
@@ -302,3 +305,34 @@ async def test_service_get_transit_times_errors(
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("data", "options"),
|
||||
[(MOCK_CONFIG, DEFAULT_OPTIONS)],
|
||||
)
|
||||
async def test_service_static_duration_falls_back_to_duration(
|
||||
hass: HomeAssistant,
|
||||
routes_mock: AsyncMock,
|
||||
mock_config: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test static_duration is the duration when the API doesn't return it."""
|
||||
routes_mock.async_compute_routes.return_value = ComputeRoutesResponse.from_dict(
|
||||
{"routes": [{"distanceMeters": 21300, "duration": "1620s"}]}
|
||||
)
|
||||
|
||||
response_data = await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"get_travel_times",
|
||||
{
|
||||
"config_entry_id": mock_config.entry_id,
|
||||
"origin": "location1",
|
||||
"destination": "location2",
|
||||
},
|
||||
blocking=True,
|
||||
return_response=True,
|
||||
)
|
||||
|
||||
route = response_data["routes"][0]
|
||||
assert route["duration"] == 1620
|
||||
assert route["static_duration"] == 1620
|
||||
|
||||
Reference in New Issue
Block a user