Skip to content

Commit

Permalink
Merge pull request #17 from helto4real:fix-unit
Browse files Browse the repository at this point in the history
Now use Home Assistant unit for weight
  • Loading branch information
helto4real authored Feb 20, 2022
2 parents b0b2f27 + 6623b9c commit d3b3065
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/vscode/devcontainers/python:0-3.8
FROM mcr.microsoft.com/vscode/devcontainers/python:0-3.9

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
git \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

Expand Down
33 changes: 17 additions & 16 deletions custom_components/my_fitnesspal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,18 @@ def __init__(

self.client = client
self.display_name = display_name
self.unit_of_weight = "kg" if hass.config.units.is_metric else "lb"

if len(self.display_name) == 0:
self.display_name = self._username

self.platforms = []

super().__init__(
hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL,
hass,
_LOGGER,
name=DOMAIN,
update_interval=SCAN_INTERVAL,
)

async def _async_update_data(self):
Expand All @@ -133,7 +137,10 @@ def update_data_sync(self) -> Dict[str, str]:

weights = self.client.get_measurements("Weight")
latest_record = weights.popitem(last=False)
latest_weight = latest_record[1]
if len(latest_record) > 1:
latest_weight = latest_record[1]
else:
latest_weight = 0

goal_calories = info.goals.get("calories", 0)
goal_carbohydrates = info.goals.get("carbohydrates", 0)
Expand All @@ -149,7 +156,7 @@ def update_data_sync(self) -> Dict[str, str]:
total_sugar = info.totals.get("sugar", 0)
total_protein = info.totals.get("protein", 0)
water = info.water
_, weight = list(weights.items())[0] if len(weights) > 0 else 0,0
_, weight = list(weights.items())[0] if len(weights) > 0 else 0, 0

cardio_calories_burned = 0
for exercise in info.exercises[0]:
Expand All @@ -163,7 +170,7 @@ def update_data_sync(self) -> Dict[str, str]:
result["goal_sodium"] = goal_sodium
result["goal_sugar"] = goal_sugar
result["goal_protein"] = goal_protein

result["total_calories"] = total_calories
result["total_carbohydrates"] = total_carbohydrates
result["total_fat"] = total_fat
Expand All @@ -175,20 +182,14 @@ def update_data_sync(self) -> Dict[str, str]:
result["water"] = water
result["weight"] = latest_weight
result["cal_remaining"] = goal_calories - total_calories
result["cal_remaining_ex_workout"] = goal_calories - total_calories - cardio_calories_burned
result["cal_remaining_ex_workout"] = (
goal_calories - total_calories - cardio_calories_burned
)
result["cal_goal"] = goal_calories - cardio_calories_burned
result["goal_pct"] = round(
(
total_calories
/ (
goal_calories
+ cardio_calories_burned
)
)
* 100,
0,
)

(total_calories / (goal_calories + cardio_calories_burned)) * 100,
0,
)

result[ATTR_ATTRIBUTION] = ATTRIBUTION

Expand Down
2 changes: 1 addition & 1 deletion custom_components/my_fitnesspal/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ def icon(self):
@property
def unit_of_measurement(self):
"""Return the units of measurement."""
return "lb"
return self.coordinator.unit_of_weight

0 comments on commit d3b3065

Please sign in to comment.