Skip to content

Commit

Permalink
Adjust polling rate of Rituals Perfume Genie (#127544)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck committed Oct 4, 2024
1 parent 1b0f731 commit ea8aa6b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
9 changes: 7 additions & 2 deletions homeassistant/components/rituals_perfume_genie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import ACCOUNT_HASH, DOMAIN
from .const import ACCOUNT_HASH, DOMAIN, UPDATE_INTERVAL
from .coordinator import RitualsDataUpdateCoordinator

PLATFORMS = [
Expand All @@ -37,9 +37,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Migrate old unique_ids to the new format
async_migrate_entities_unique_ids(hass, entry, account_devices)

# The API provided by Rituals is currently rate limited to 30 requests
# per hour per IP address. To avoid hitting this limit, we will adjust
# the polling interval based on the number of diffusers one has.
update_interval = UPDATE_INTERVAL * len(account_devices)

# Create a coordinator for each diffuser
coordinators = {
diffuser.hublot: RitualsDataUpdateCoordinator(hass, diffuser)
diffuser.hublot: RitualsDataUpdateCoordinator(hass, diffuser, update_interval)
for diffuser in account_devices
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async def async_step_user(
try:
await account.authenticate()
except ClientResponseError:
_LOGGER.exception("Unexpected response")
errors["base"] = "cannot_connect"
except AuthenticationException:
errors["base"] = "invalid_auth"
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/rituals_perfume_genie/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

ACCOUNT_HASH = "account_hash"

UPDATE_INTERVAL = timedelta(minutes=2)
# The API provided by Rituals is currently rate limited to 30 requests
# per hour per IP address. To avoid hitting this limit, the polling
# interval is set to 3 minutes. This also gives a little room for
# Home Assistant restarts.
UPDATE_INTERVAL = timedelta(minutes=3)
12 changes: 9 additions & 3 deletions homeassistant/components/rituals_perfume_genie/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
"""The Rituals Perfume Genie data update coordinator."""

from datetime import timedelta
import logging

from pyrituals import Diffuser

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import DOMAIN, UPDATE_INTERVAL
from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)


class RitualsDataUpdateCoordinator(DataUpdateCoordinator[None]):
"""Class to manage fetching Rituals Perfume Genie device data from single endpoint."""

def __init__(self, hass: HomeAssistant, diffuser: Diffuser) -> None:
def __init__(
self,
hass: HomeAssistant,
diffuser: Diffuser,
update_interval: timedelta,
) -> None:
"""Initialize global Rituals Perfume Genie data updater."""
self.diffuser = diffuser
super().__init__(
hass,
_LOGGER,
name=f"{DOMAIN}-{diffuser.hublot}",
update_interval=UPDATE_INTERVAL,
update_interval=update_interval,
)

async def _async_update_data(self) -> None:
Expand Down

0 comments on commit ea8aa6b

Please sign in to comment.