Skip to content

Commit

Permalink
improve the girths and start renpho cloud api
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Mar 11, 2024
1 parent 126af0d commit fc799b5
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions custom_components/renpho/api_renpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ async def check_proxy(self):
try:
async with aiohttp.ClientSession(connector=ProxyConnector.from_url(self.proxy)) as session:
async with session.get(test_url) as response:
_LOGGER.error("Proxy connection successful.")
return True
except Exception as e:
_LOGGER.error(f"Proxy connection failed: {e}")
Expand Down Expand Up @@ -161,7 +160,7 @@ async def _request(self, method: str, url: str, retries: int = 3, skip_auth=Fals
async with session.request(method, url, **kwargs) as response:
response.raise_for_status()
parsed_response = await response.json()

if parsed_response.get("status_code") == "40302":
skip_auth = False
auth_success = await self.auth()
Expand Down Expand Up @@ -569,16 +568,22 @@ async def get_specific_metric(self, metric_type: str, metric: str, user_id: Opti
if self._last_updated_girth is None or self.girth_info is None:
await self.list_girth()
if self.girth_info:
for girth_entry in self.girth_info:
if hasattr(girth_entry, f"{metric}_value"):
return getattr(girth_entry, f"{metric}_value", None)
valid_girths = sorted([g for g in self.girth_info if getattr(g, f"{metric}_value", 0) not in (None, 0.0)], key=lambda x: x.time_stamp, reverse=True)
for girth in valid_girths:
value = getattr(girth, f"{metric}_value", None)
if value not in (None, 0.0):
return value
return None
elif metric_type == METRIC_TYPE_GIRTH_GOAL:
if self._last_updated_girth_goal is None or self.girth_goal is None:
await self.list_girth_goal()
if self.girth_goal:
for goal in self.girth_goal:
if goal.girth_type == metric:
valid_goals = sorted([g for g in self.girth_goal if g.girth_type == metric and g.goal_value not in (None, 0.0)], key=lambda x: x.setup_goal_at, reverse=True)
# Iterate to find the first valid goal
for goal in valid_goals:
if goal.goal_value not in (None, 0.0):
return goal.goal_value
return None
else:
_LOGGER.error(f"Invalid metric type: {metric_type}")
return None
Expand Down

0 comments on commit fc799b5

Please sign in to comment.