Skip to content

Commit

Permalink
Merge pull request #21 from actuallydamo/main
Browse files Browse the repository at this point in the history
Add support for KJ and replace deprecated function
  • Loading branch information
helto4real authored Apr 3, 2022
2 parents 1027d41 + 6e09fd6 commit fc64743
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Myfitnesspal custom component
This custom component gets fitnessdata from your account. Please advice that the component uses underlying scraping API:s that can change at any time and used at your own risk. **The author takes no liability of usage**.
This custom component gets fitness data from your account. Please note that the component uses an underlying scraping API that can change at any time and use at your own risk. **The author takes no liability of usage**.

# Usage

## Copy and paste
*If you use HACS, skip this step!*
The component can be used by copy everything under `custom_component` folder to your `custom_component`, i.e. the `my_fitnesspal` folder.
The component can be used by copying everything under the `custom_component` folder to your `custom_component`, i.e. the `my_fitnesspal` folder.

### Configure trough integrations (prefered way)
### Configure through integrations (prefered way)
Check under configuration/integrations. Add the `Myfitnesspal` integration.

### Configure with old school yaml
Expand All @@ -25,4 +25,4 @@ Not supported
## HACS component
You can use HACS [Se this link for more information](https://github.com/custom-components/hacs). Add `https://github.com/helto4real/custom_component_myfitnesspal` to custom repository under `SETTINGS`. Select integration as type.

Configure through integrationspage or old school yaml as described above.
Configure through the integrations page or old school yaml as described above.
32 changes: 28 additions & 4 deletions custom_components/my_fitnesspal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,24 @@ def update_data_sync(self) -> Dict[str, str]:
if len(latest_record) > 1:
latest_weight = latest_record[1]

goal_calories = info.goals.get("calories", 0)
goal_calories = info.goals.get("calories",
round(info.goals.get("kilojoules", 0) / 4.184)
)
goal_kilojoules = info.goals.get("kilojoules",
round(info.goals.get("calories", 0) * 4.184)
)
goal_carbohydrates = info.goals.get("carbohydrates", 0)
goal_fat = info.goals.get("fat", 0)
goal_sodium = info.goals.get("sodium", 0)
goal_sugar = info.goals.get("sugar", 0)
goal_protein = info.goals.get("protein", 0)

total_calories = info.totals.get("calories", 0)
total_calories = info.totals.get("calories",
round(info.totals.get("kilojoules", 0) / 4.184)
)
total_kilojoules = info.totals.get("kilojoules",
round(info.totals.get("calories", 0) * 4.184)
)
total_carbohydrates = info.totals.get("carbohydrates", 0)
total_fat = info.totals.get("fat", 0)
total_sodium = info.totals.get("sodium", 0)
Expand All @@ -158,35 +168,49 @@ def update_data_sync(self) -> Dict[str, str]:
_, weight = list(weights.items())[0] if len(weights) > 0 else 0, 0

cardio_calories_burned = 0
cardio_kilojoules_burned = 0
for exercise in info.exercises[0]:
cardio_calories_burned += exercise["calories burned"]
cardio_calories_burned += exercise.totals.get("calories burned",
round(exercise.totals.get("kilojoules burned", 0) / 4.184)
)
cardio_kilojoules_burned += exercise.totals.get("kilojoules burned",
round(exercise.totals.get("calories burned", 0) * 4.184)
)

result = {}

result["goal_calories"] = goal_calories
result["goal_kilojoules"] = goal_kilojoules
result["goal_carbohydrates"] = goal_carbohydrates
result["goal_fat"] = goal_fat
result["goal_sodium"] = goal_sodium
result["goal_sugar"] = goal_sugar
result["goal_protein"] = goal_protein

result["total_calories"] = total_calories
result["total_kilojoules"] = total_kilojoules
result["total_carbohydrates"] = total_carbohydrates
result["total_fat"] = total_fat
result["total_sodium"] = total_sodium
result["total_sugar"] = total_sugar
result["total_protein"] = total_protein

result["cardio_kilojoules_burned"] = cardio_kilojoules_burned
result["cardio_calories_burned"] = cardio_calories_burned
result["water"] = water
result["weight"] = latest_weight
result["cal_remaining"] = goal_calories - total_calories
result["kj_remaining"] = goal_kilojoules - total_kilojoules
result["cal_remaining_ex_workout"] = (
goal_calories - total_calories - cardio_calories_burned
)
result["kj_remaining_ex_workout"] = (
goal_kilojoules - total_kilojoules - cardio_kilojoules_burned
)
result["cal_goal"] = goal_calories - cardio_calories_burned
result["kj_goal"] = goal_kilojoules - cardio_kilojoules_burned
result["goal_pct"] = round(
(total_calories / (goal_calories + cardio_calories_burned)) * 100,
(total_kilojoules / (goal_kilojoules + cardio_kilojoules_burned)) * 100,
0,
)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/my_fitnesspal/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def device_info(self):
}

@property
def device_state_attributes(self):
def extra_state_attributes(self):
"""Return the state attributes."""
return self.coordinator.data

Expand Down

0 comments on commit fc64743

Please sign in to comment.