Skip to content

Commit

Permalink
fix specific data
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Mar 5, 2024
1 parent 9114057 commit a86c2e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 26 deletions.
21 changes: 6 additions & 15 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ async def get_measurements(self):
if measurements := parsed["last_ary"]:
self.weight_history = [MeasurementDetail(**measurement) for measurement in measurements]
self.weight_info = self.weight_history[0] if self.weight_history else None
self.weight = self.weight_info.weight if self.weight_info else None
self.weight = self.weight_history[0].weight if self.weight_info else None
self.time_stamp = self.weight_info.time_stamp if self.weight_info else None
self._last_updated_weight = time.time()
return self.weight_info
Expand Down Expand Up @@ -652,9 +652,8 @@ async def get_device_info(self):

# Check for successful response code
if parsed.get("status_code") == "20000" and "device_binds_ary" in parsed:
device_info = [DeviceBind(**device) for device in parsed["device_binds_ary"]]
self.device_info = device_info
return device_info
self.device_info = parsed["device_binds_ary"]
return self.device_info
else:
# Handling different error scenarios
if "status_code" not in parsed:
Expand Down Expand Up @@ -834,34 +833,26 @@ async def get_specific_metric(self, metric_type: str, metric: str, user_id: Opti

try:
if metric_type == METRIC_TYPE_WEIGHT:
if self.weight_info:
return self.weight_info.get(metric, None)
if self._last_updated_weight is None or time.time() - self._last_updated_weight > self.refresh:
last_measurement = await self.get_weight()
if last_measurement and self.weight is not None:
return last_measurement[1].get(metric, None) if last_measurement[1] else None
elif metric_type == METRIC_TYPE_GIRTH:
if not self.girth_info:
return await self.list_girth()
if self._last_updated_girth is None or time.time() - self._last_updated_girth > self.refresh:
last_measurement = (
self.girth_info.get("girths", [])[0]
if self.girth_info.get("girths")
self.girth_info[0]
if self.girth_info
else None
)
return last_measurement.get(metric, None) if last_measurement else None
elif metric_type == METRIC_TYPE_GIRTH_GOAL:
if not self.girth_goal:
return await self.get_specific_girth_goal_metric(metric)
last_goal = next(
(goal for goal in self.girth_goal['girth_goals'] if goal['girth_type'] == metric),
(goal for goal in self.girth_goal if goal['girth_type'] == metric),
None
)
if self._last_updated_girth_goal is None or time.time() - self._last_updated_girth_goal > self.refresh:
return last_goal.get('goal_value', None)
elif metric_type == METRIC_TYPE_GROWTH_RECORD:
if not self.growth_record:
return await self.list_growth_record()
if self._last_updated_growth_record is None or time.time() - self._last_updated_growth_record > self.refresh:
last_measurement = (
self.growth_record.get("growths", [])[0]
Expand Down
14 changes: 3 additions & 11 deletions custom_components/renpho/api_renpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,34 +530,26 @@ async def get_specific_metric(self, metric_type: str, metric: str, user_id: Opti

try:
if metric_type == METRIC_TYPE_WEIGHT:
if self.weight_info:
return self.weight_info.get(metric, None)
if self._last_updated_weight is None or time.time() - self._last_updated_weight > self.refresh:
last_measurement = await self.get_weight()
if last_measurement and self.weight is not None:
return last_measurement[1].get(metric, None) if last_measurement[1] else None
elif metric_type == METRIC_TYPE_GIRTH:
if not self.girth_info:
return await self.list_girth()
if self._last_updated_girth is None or time.time() - self._last_updated_girth > self.refresh:
last_measurement = (
self.girth_info.get("girths", [])[0]
if self.girth_info.get("girths")
self.girth_info[0]
if self.girth_info
else None
)
return last_measurement.get(metric, None) if last_measurement else None
elif metric_type == METRIC_TYPE_GIRTH_GOAL:
if not self.girth_goal:
return await self.get_specific_girth_goal_metric(metric)
last_goal = next(
(goal for goal in self.girth_goal['girth_goals'] if goal['girth_type'] == metric),
(goal for goal in self.girth_goal if goal['girth_type'] == metric),
None
)
if self._last_updated_girth_goal is None or time.time() - self._last_updated_girth_goal > self.refresh:
return last_goal.get('goal_value', None)
elif metric_type == METRIC_TYPE_GROWTH_RECORD:
if not self.growth_record:
return await self.list_growth_record()
if self._last_updated_growth_record is None or time.time() - self._last_updated_growth_record > self.refresh:
last_measurement = (
self.growth_record.get("growths", [])[0]
Expand Down

0 comments on commit a86c2e9

Please sign in to comment.