diff --git a/homeassistant/components/google_travel_time/helpers.py b/homeassistant/components/google_travel_time/helpers.py index 5d30fa610a65..919e30b92436 100644 --- a/homeassistant/components/google_travel_time/helpers.py +++ b/homeassistant/components/google_travel_time/helpers.py @@ -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) diff --git a/homeassistant/components/google_travel_time/services.py b/homeassistant/components/google_travel_time/services.py index 98349697a87a..7b78491a8075 100644 --- a/homeassistant/components/google_travel_time/services.py +++ b/homeassistant/components/google_travel_time/services.py @@ -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 diff --git a/tests/components/google_travel_time/conftest.py b/tests/components/google_travel_time/conftest.py index 435b8f327916..5368fa291a65 100644 --- a/tests/components/google_travel_time/conftest.py +++ b/tests/components/google_travel_time/conftest.py @@ -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"}, diff --git a/tests/components/google_travel_time/test_services.py b/tests/components/google_travel_time/test_services.py index 496810055ee1..2328a0ae005e 100644 --- a/tests/components/google_travel_time/test_services.py +++ b/tests/components/google_travel_time/test_services.py @@ -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