mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +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."""
|
"""The waze_travel_time component."""
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DOMAIN, SEMAPHORE
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
"""Load the saved entities."""
|
"""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)
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
DOMAIN = "waze_travel_time"
|
DOMAIN = "waze_travel_time"
|
||||||
|
SEMAPHORE = "semaphore"
|
||||||
|
|
||||||
CONF_DESTINATION = "destination"
|
CONF_DESTINATION = "destination"
|
||||||
CONF_ORIGIN = "origin"
|
CONF_ORIGIN = "origin"
|
||||||
|
@ -43,6 +43,7 @@ from .const import (
|
|||||||
DEFAULT_NAME,
|
DEFAULT_NAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
IMPERIAL_UNITS,
|
IMPERIAL_UNITS,
|
||||||
|
SEMAPHORE,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -51,7 +52,7 @@ SCAN_INTERVAL = timedelta(minutes=5)
|
|||||||
|
|
||||||
PARALLEL_UPDATES = 1
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
MS_BETWEEN_API_CALLS = 0.5
|
SECONDS_BETWEEN_API_CALLS = 0.5
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -148,8 +149,12 @@ class WazeTravelTime(SensorEntity):
|
|||||||
_LOGGER.debug("Fetching Route for %s", self._attr_name)
|
_LOGGER.debug("Fetching Route for %s", self._attr_name)
|
||||||
self._waze_data.origin = find_coordinates(self.hass, self._origin)
|
self._waze_data.origin = find_coordinates(self.hass, self._origin)
|
||||||
self._waze_data.destination = find_coordinates(self.hass, self._destination)
|
self._waze_data.destination = find_coordinates(self.hass, self._destination)
|
||||||
await self._waze_data.async_update()
|
await self.hass.data[DOMAIN][SEMAPHORE].acquire()
|
||||||
await asyncio.sleep(MS_BETWEEN_API_CALLS)
|
try:
|
||||||
|
await self._waze_data.async_update()
|
||||||
|
await asyncio.sleep(SECONDS_BETWEEN_API_CALLS)
|
||||||
|
finally:
|
||||||
|
self.hass.data[DOMAIN][SEMAPHORE].release()
|
||||||
|
|
||||||
|
|
||||||
class WazeTravelTimeData:
|
class WazeTravelTimeData:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user