Skip to content

Commit

Permalink
Merge pull request #8 from RobertD502/rate_limit_fix
Browse files Browse the repository at this point in the history
Rate limit fix
  • Loading branch information
RobertD502 authored Jan 8, 2024
2 parents b7a2e39 + e09d8d8 commit 65af7dc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Purrsong is currently rate limiting connections to their API. As a result, you may see 500 status code messages in your log. I am actively working on/testing a workaround fix. This message will be removed when a fix has been implemented.

# LavvieBot S [![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg)](https://github.com/custom-components/hacs) ![GitHub manifest version (path)](https://img.shields.io/github/manifest-json/v/RobertD502/home-assistant-lavviebot?filename=custom_components%2Fpurrsong%2Fmanifest.json)
<a href="https://www.buymeacoffee.com/RobertD502" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="100" width="424"></a>
<a href="https://liberapay.com/RobertD502/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg" height="100" width="300"></a>
Expand Down
20 changes: 14 additions & 6 deletions custom_components/purrsong/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

from datetime import timedelta

from aiohttp import ClientSession
from lavviebot import LavviebotClient
from lavviebot.exceptions import LavviebotAuthError, LavviebotError
from lavviebot.exceptions import LavviebotAuthError, LavviebotError, LavviebotRateLimit
from lavviebot.model import LavviebotData


from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, LOGGER, TIMEOUT
Expand All @@ -28,7 +28,7 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
self.client = LavviebotClient(
entry.data[CONF_EMAIL],
entry.data[CONF_PASSWORD],
session=async_get_clientsession(hass),
session=ClientSession(),
timeout=TIMEOUT,
)
super().__init__(
Expand All @@ -47,6 +47,14 @@ async def _async_update_data(self) -> LavviebotData:
raise ConfigEntryAuthFailed(error) from error
except LavviebotError as error:
raise UpdateFailed(error) from error
if not data.litterboxes:
raise UpdateFailed("No Litter Boxes found")
return data
except LavviebotRateLimit:
LOGGER.debug("Purrsong API has rate limited current session. Starting new ClientSession.")
await self.client._session.close()
self.client._session = ClientSession()
self.client.token = None
return await self._async_update_data()
else:
if not data.litterboxes:
raise UpdateFailed("No Litter Boxes found")
else:
return data
10 changes: 7 additions & 3 deletions custom_components/purrsong/manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"domain": "purrsong",
"name": "LavvieBot S",
"codeowners": ["@RobertD502"],
"codeowners": [
"@RobertD502"
],
"config_flow": true,
"dependencies": [],
"documentation": "https://github.com/RobertD502/home-assistant-lavviebot/blob/main/README.md",
"integration_type": "hub",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/RobertD502/home-assistant-lavviebot/issues",
"requirements": ["lavviebotaio==0.2.0"],
"version": "0.1.12"
"requirements": [
"lavviebotaio==0.2.2.1"
],
"version": "0.2.0"
}

0 comments on commit 65af7dc

Please sign in to comment.