Skip to content

Commit

Permalink
Use local copy of _route_stops to avoid NextBus race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
drozycki committed Aug 16, 2024
1 parent 8c6796e commit a854587
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 1 addition & 7 deletions homeassistant/components/nextbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_STOP, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.start import async_at_started

from .const import CONF_AGENCY, CONF_ROUTE, DOMAIN
from .coordinator import NextBusDataUpdateCoordinator
Expand All @@ -24,12 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

coordinator.add_stop_route(entry.data[CONF_STOP], entry.data[CONF_ROUTE])

async def _async_finish_startup(hass: HomeAssistant) -> None:
"""Run this only when HA has finished its startup."""
await coordinator.async_config_entry_first_refresh()

# Don't call the NextBus API during startup
async_at_started(hass, _async_finish_startup)
await coordinator.async_config_entry_first_refresh()

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
8 changes: 5 additions & 3 deletions homeassistant/components/nextbus/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ def has_routes(self) -> bool:
"""Check if this coordinator is tracking any routes."""
return len(self._route_stops) > 0

async def _async_update_data(self) -> dict[str, Any]:
async def _async_update_data(self) -> dict:
"""Fetch data from NextBus."""
self.logger.debug("Updating data from API. Routes: %s", str(self._route_stops))

_route_stops = set(self._route_stops)
self.logger.debug("Updating data from API. Routes: %s", str(_route_stops))

def _update_data() -> dict:
"""Fetch data from NextBus."""
self.logger.debug("Updating data from API (executor)")
predictions: dict[RouteStop, dict[str, Any]] = {}
for route_stop in self._route_stops:
for route_stop in _route_stops:
prediction_results: list[dict[str, Any]] = []
try:
prediction_results = self.client.predictions_for_stop(
Expand Down

0 comments on commit a854587

Please sign in to comment.