mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Limit waze_travel_time to 0.5call/s over all entries (#101514)
This commit is contained in:
parent
d14934861e
commit
7f6506cfcf
@ -1,13 +1,19 @@
|
||||
"""The waze_travel_time component."""
|
||||
import asyncio
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN, SEMAPHORE
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
"""Load the saved entities."""
|
||||
if SEMAPHORE not in hass.data.setdefault(DOMAIN, {}):
|
||||
hass.data.setdefault(DOMAIN, {})[SEMAPHORE] = asyncio.Semaphore(1)
|
||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
DOMAIN = "waze_travel_time"
|
||||
SEMAPHORE = "semaphore"
|
||||
|
||||
CONF_DESTINATION = "destination"
|
||||
CONF_ORIGIN = "origin"
|
||||
|
@ -43,6 +43,7 @@ from .const import (
|
||||
DEFAULT_NAME,
|
||||
DOMAIN,
|
||||
IMPERIAL_UNITS,
|
||||
SEMAPHORE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -51,7 +52,7 @@ SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
PARALLEL_UPDATES = 1
|
||||
|
||||
MS_BETWEEN_API_CALLS = 0.5
|
||||
SECONDS_BETWEEN_API_CALLS = 0.5
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
@ -148,8 +149,12 @@ class WazeTravelTime(SensorEntity):
|
||||
_LOGGER.debug("Fetching Route for %s", self._attr_name)
|
||||
self._waze_data.origin = find_coordinates(self.hass, self._origin)
|
||||
self._waze_data.destination = find_coordinates(self.hass, self._destination)
|
||||
await self._waze_data.async_update()
|
||||
await asyncio.sleep(MS_BETWEEN_API_CALLS)
|
||||
await self.hass.data[DOMAIN][SEMAPHORE].acquire()
|
||||
try:
|
||||
await self._waze_data.async_update()
|
||||
await asyncio.sleep(SECONDS_BETWEEN_API_CALLS)
|
||||
finally:
|
||||
self.hass.data[DOMAIN][SEMAPHORE].release()
|
||||
|
||||
|
||||
class WazeTravelTimeData:
|
||||
|
Loading…
x
Reference in New Issue
Block a user