Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions custom_components/petlibro/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# https://api.us.petlibro.com/device/feedingPlan/list
# https://api.us.petlibro.com/device/wetFeedingPlan/wetListV3

import asyncio
from logging import getLogger
from hashlib import md5
import sys
Expand Down Expand Up @@ -92,6 +93,22 @@ async def request(self, method: str, url: str, **kwargs: Any) -> JSON:
# Send the request
async with self.websession.request(method, joined_url, **kwargs) as resp:
_LOGGER.debug(f"Received response status: {resp.status}")

# Retry on 5xx server errors before attempting to parse JSON
max_retries = 2
retry_delay = 2
if resp.status >= 500:
for attempt in range(max_retries):
_LOGGER.warning(
"Retrying %s %s (attempt %d/%d) after status %d",
method, joined_url, attempt + 1, max_retries, resp.status,
)
await asyncio.sleep(retry_delay)
async with self.websession.request(method, joined_url, **kwargs) as retry_resp:
resp = retry_resp
if resp.status < 500:
break

try:
data = await resp.json()
except Exception as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,51 @@ class DockstreamSmartFountain(Device):
async def refresh(self):
"""Refresh the device data from the API."""
try:
await super().refresh() # Call the refresh method from the parent class (Device)

# Fetch real info from the API
real_info = await self.api.device_real_info(self.serial)
data_real_info = await self.api.device_data_real_info(self.serial)
attribute_settings = await self.api.device_attribute_settings(self.serial)
get_upgrade = await self.api.get_device_upgrade(self.serial)
get_work_record = await self.api.get_device_work_record(self.serial)
get_feeding_plan_today = await self.api.device_feeding_plan_today_new(self.serial)
get_drink_water = await self.api.get_device_drink_water(self.serial)

# Update internal data with fetched API data
await super().refresh()

real_info = None
data_real_info = None
attribute_settings = None
get_upgrade = None
get_work_record = None
get_feeding_plan_today = None
get_drink_water = None

try:
real_info = await self.api.device_real_info(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching realInfo for DockstreamSmartFountain: {err}")

try:
data_real_info = await self.api.device_data_real_info(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching dataRealInfo for DockstreamSmartFountain: {err}")

try:
attribute_settings = await self.api.device_attribute_settings(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching attributeSettings for DockstreamSmartFountain: {err}")

try:
get_upgrade = await self.api.get_device_upgrade(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching getUpgrade for DockstreamSmartFountain: {err}")

try:
get_work_record = await self.api.get_device_work_record(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching workRecord for DockstreamSmartFountain: {err}")

try:
get_feeding_plan_today = await self.api.device_feeding_plan_today_new(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching feedingPlanToday for DockstreamSmartFountain: {err}")

try:
get_drink_water = await self.api.get_device_drink_water(self.serial)
except PetLibroAPIError as err:
_LOGGER.warning(f"Error fetching drinkWater for DockstreamSmartFountain: {err}")

self.update_data({
"realInfo": real_info or {},
"dataRealInfo": data_real_info or {},
Expand Down