Skip to content

Commit

Permalink
Use the user scan interval value in bixi coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
benVigie committed Aug 7, 2024
1 parent 198be65 commit f68e244
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion custom_components/bixi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class BixiData:
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up this integration using UI."""
# Setup coordinator
coordinator = BixiCoordinator(hass, entry.data["stations"])
coordinator = BixiCoordinator(
hass, entry.data["stations"], entry.data["scan_interval"]
)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.title] = coordinator

Expand Down
16 changes: 11 additions & 5 deletions custom_components/bixi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from custom_components.bixi.model import BixiStationList

from .bixi_helper import fetch_stations_data
from .const import DOMAIN
from .const import BIXI_FETCH_TIMEOUT, DOMAIN

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,24 +26,30 @@ class BixiCoordinator(DataUpdateCoordinator[Any]):

config_entry: ConfigEntry

def __init__(self, hass: HomeAssistant, stations: list[str]) -> None:
def __init__(
self, hass: HomeAssistant, stations: list[str], polling_interval: int
) -> None:
"""Initialize coordinator."""
super().__init__(
hass, _LOGGER, name=DOMAIN, update_interval=timedelta(seconds=10)
hass,
_LOGGER,
name=DOMAIN,
update_interval=timedelta(minutes=polling_interval),
)
self._stations = stations
self._polling_interval = polling_interval

async def _async_update_data(self) -> Any:
"""Fetch data from API endpoint."""
try:
async with async_timeout.timeout(10):
async with async_timeout.timeout(BIXI_FETCH_TIMEOUT):
return await self.hass.async_add_executor_job(self.fetch_data)
except Exception as err:
msg = f"Error communicating with API: {err}"
raise UpdateFailed(msg) from err

def fetch_data(self) -> BixiStationList:
"""Fetch the bixi updates."""
"""Fetch the bixi stations updates."""
try:
return fetch_stations_data(self._stations)
except requests.HTTPError as err:
Expand Down

0 comments on commit f68e244

Please sign in to comment.