From e354bd0865d4e116abd3212da776d1e5fbe17e08 Mon Sep 17 00:00:00 2001 From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:20:24 -0500 Subject: [PATCH 1/3] start new ClientSession if rate limited --- custom_components/purrsong/coordinator.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/custom_components/purrsong/coordinator.py b/custom_components/purrsong/coordinator.py index 5d1b2ee..bf1d5d2 100644 --- a/custom_components/purrsong/coordinator.py +++ b/custom_components/purrsong/coordinator.py @@ -3,8 +3,9 @@ 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 @@ -12,7 +13,6 @@ 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 @@ -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__( @@ -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 From 96d625c7d59a3ad1860bff7e5028b9de42c698ba Mon Sep 17 00:00:00 2001 From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:21:47 -0500 Subject: [PATCH 2/3] bump integration to 0.2.0, bump lavviebotaio to 0.2.2.1 --- custom_components/purrsong/manifest.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/purrsong/manifest.json b/custom_components/purrsong/manifest.json index a8f1fca..6d85a5e 100644 --- a/custom_components/purrsong/manifest.json +++ b/custom_components/purrsong/manifest.json @@ -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" } From e09d8d8b78bff596358faf387d2ce1e2db31a455 Mon Sep 17 00:00:00 2001 From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com> Date: Mon, 8 Jan 2024 15:22:16 -0500 Subject: [PATCH 3/3] remove rate limit message --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 876a08e..c029c16 100644 --- a/README.md +++ b/README.md @@ -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) Buy Me A Coffee Donate using Liberapay