zacs / ha-gtfs-rt

Real-time transit information for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Protobuf parsing error

rafaelmagu opened this issue · comments

I'm struggling to track down an error with the integration using Auckland Transport's GTFS-RT feed. This API is provided by free of charge (account registration required) by Auckland Transport, who recently revamped their API portal and started offering GTFS V3. However, I noticed I need to provide V2 feeds to this integration, which AT also provides under the legacy/ path.

I've been trying to set up a sensor for a ferry route using the following config:

- platform: gtfs_rt
  trip_update_url: 'https://api.at.govt.nz/realtime/legacy/tripupdates?tripid=514-96011-39600-2-91e07bcd,514-96012-54300-2-f085c4f3'
  headers:
    Cache-Control: none
    Ocp-Apim-Subscription-Key: !secret at_api_key
  departures:
    - name: Hobsonville to Downtown
      route: "HOBS-209"
      stopid: "21779-d98fae38"

But it appears to be failing at parsing the protobuf response:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 573, in _async_add_entity
await entity.async_device_update(warning=False)
File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 938, in async_device_update
await hass.async_add_executor_job(self.update)
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/gtfs_rt/sensor.py", line 141, in update
self.data.update()
File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 190, in wrapper
result = method(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/gtfs_rt/sensor.py", line 157, in update
self._update_route_statuses(positions, vehicles_trips, occupancy)
File "/config/custom_components/gtfs_rt/sensor.py", line 173, in _update_route_statuses
feed.ParseFromString(response.content)
google.protobuf.message.DecodeError: Error parsing message

I can get the results manually through Insomnia:

{
	"status": "OK",
	"response": {
		"header": {
			"timestamp": 1696387755.058,
			"gtfs_realtime_version": "1.0",
			"incrementality": 0
		},
		"entity": [
			{
				"id": "7012303965-20230928120550_v106.29",
				"trip_update": {
					"trip": {
						"trip_id": "514-96012-54300-2-f085c4f3",
						"start_time": "15:05:00",
						"start_date": "20231004",
						"schedule_relationship": 3,
						"route_id": "HOBS-209",
						"direction_id": 1
					},
					"timestamp": 1696384892,
					"delay": 0
				},
				"is_deleted": false
			}
		]
	},
	"error": null
}

Logging is already set to debug for this component. What other info can I gather to help me diagnose this?

Turns out I had to manually include the Accept: application/x-protobuf header under headers. This particular API by default responds with JSON unless the Accept header is set.