Limit waze_travel_time to 0.5call/s over all entries (#101514)

This commit is contained in:
Kevin Stillhammer 2023-10-06 13:23:32 +02:00 committed by GitHub
parent f7aad4a9e6
commit 79eaaec1a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View file

@ -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

View file

@ -2,6 +2,7 @@
from __future__ import annotations
DOMAIN = "waze_travel_time"
SEMAPHORE = "semaphore"
CONF_DESTINATION = "destination"
CONF_ORIGIN = "origin"

View file

@ -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: