Skip to content

Commit

Permalink
Merge pull request #6 from antoinebou12/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Sep 14, 2023
2 parents 2b51801 + 6390414 commit 3ad29f1
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 88 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ This custom component allows you to integrate Renpho's weight scale data into Ho

![Sensors](docs/images/renpho_google.png)

![Complete View](docs/images/image.png)

## Table of Contents

- [Prerequisites](#prerequisites)
Expand Down
6 changes: 4 additions & 2 deletions custom_components/renpho/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging

from const import (
from .const import (
CONF_EMAIL,
CONF_PASSWORD,
CONF_PUBLIC_KEY,
Expand All @@ -10,7 +10,7 @@
DOMAIN,
EVENT_HOMEASSISTANT_STOP,
)
from api_renpho import RenphoWeight
from .api_renpho import RenphoWeight


# Initialize logger
Expand Down Expand Up @@ -58,12 +58,14 @@ async def setup_renpho(hass, conf):
user_id = conf.get(CONF_USER_ID, None)
refresh = conf.get(CONF_REFRESH, 600)
renpho = RenphoWeight(CONF_PUBLIC_KEY, email, password, user_id, refresh)
await renpho.get_info()
hass.data[DOMAIN] = renpho


async def async_prepare(hass, renpho, refresh):
"""Prepare and start polling."""
await renpho.start_polling(refresh)
await renpho.get_info()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_cleanup(renpho))


Expand Down
38 changes: 24 additions & 14 deletions custom_components/renpho/api_renpho.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA

from const import METRIC_TYPE_GIRTH, METRIC_TYPE_GIRTH_GOAL, METRIC_TYPE_GROWTH_RECORD, METRIC_TYPE_WEIGHT
from .const import METRIC_TYPE_GIRTH, METRIC_TYPE_GIRTH_GOAL, METRIC_TYPE_GROWTH_RECORD, METRIC_TYPE_WEIGHT

# Initialize logging
_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -293,12 +293,13 @@ async def get_specific_metric(
if self.weight is not None:
return last_measurement[1].get(metric, None)
try:
func_name, last_measurement_key = METRIC_TYPE_FUNCTIONS.get(
metric_type, ("weight", None))
if metric_type == METRIC_TYPE_GIRTH_GOAL:
return await self.get_specific_girth_goal_metric(metric, user_id)

func_name, last_measurement_key = METRIC_TYPE_FUNCTIONS.get(metric_type, ("weight", None))

if func_name is None:
_LOGGER.error(
f"Invalid metric_type: {metric_type}. Must be one of {list(METRIC_TYPE_FUNCTIONS.keys())}.")
_LOGGER.error(f"Invalid metric_type: {metric_type}. Must be one of {list(METRIC_TYPE_FUNCTIONS.keys())}.")
await self.close()
return None

Expand All @@ -312,8 +313,7 @@ async def get_specific_metric(
last_measurement = metric_info[last_measurement_key][0] if metric_info[last_measurement_key] else None

if last_measurement is None:
_LOGGER.warning(
f"Invalid response or '{last_measurement_key}' not in the response.")
_LOGGER.warning(f"Invalid response or '{last_measurement_key}' not in the response.")
await self.close()
return None

Expand Down Expand Up @@ -466,7 +466,7 @@ async def get_specific_girth_goal_metric(
Fetch a specific girth goal metric for a particular user ID from the most recent girth goal information.
Parameters:
metric (str): The specific metric to fetch .
metric (str): The specific metric to fetch.
user_id (str, optional): The user ID for whom to fetch the metric. Defaults to None.
Returns:
Expand All @@ -475,13 +475,23 @@ async def get_specific_girth_goal_metric(
try:
if user_id:
self.set_user_id(user_id)

girth_goal_info = await self.list_girth_goal()
last_goal = (
girth_goal_info.get("girth_goals", [])[0]
if girth_goal_info.get("girth_goals")
else None

if not girth_goal_info or 'girth_goals' not in girth_goal_info:
return None

# Filter to find the specific metric
last_goal = next(
(goal for goal in girth_goal_info['girth_goals'] if goal['girth_type'] == metric),
None
)
return last_goal.get(metric, None) if last_goal else None

if last_goal:
return last_goal.get('goal_value', None)

return None

except Exception as e:
await self.close()
print(f"An error occurred: {e}")
Expand Down Expand Up @@ -600,4 +610,4 @@ class APIError(Exception):


class ClientSSLError(Exception):
pass
pass
15 changes: 3 additions & 12 deletions custom_components/renpho/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from homeassistant import config_entries, exceptions
from homeassistant.core import HomeAssistant

from const import CONF_EMAIL, CONF_PASSWORD, CONF_PUBLIC_KEY, CONF_REFRESH, CONF_USER_ID, DOMAIN
from api_renpho import RenphoWeight
from .const import CONF_EMAIL, CONF_PASSWORD, CONF_PUBLIC_KEY, CONF_REFRESH, CONF_USER_ID, DOMAIN
from .api_renpho import RenphoWeight

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -92,15 +92,6 @@ async def async_step_user(self, user_input=None):
description_placeholders=placeholders,
)

async def async_step_advanced_options(self, user_input=None):
# Implement advanced options step here
pass

async def async_step_select_device(self, user_input=None):
# Implement device selection step here
pass


class CannotConnect(exceptions.HomeAssistantError):
def __init__(self, reason: str = "", details: dict = None):
super().__init__(self)
Expand All @@ -111,4 +102,4 @@ def __str__(self):
return f"CannotConnect: {self.reason}"

def get_details(self):
return self.details
return self.details
2 changes: 1 addition & 1 deletion custom_components/renpho/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
METRIC_TYPE_WEIGHT: Final = "weight"
METRIC_TYPE_GROWTH_RECORD: Final = "growth_record"
METRIC_TYPE_GIRTH: Final = "girth"
METRIC_TYPE_GIRTH_GOAL: Final = "girth_goal"
METRIC_TYPE_GIRTH_GOAL: Final = "girth_goals"

METRIC_TYPE = [
METRIC_TYPE_WEIGHT,
Expand Down
80 changes: 51 additions & 29 deletions custom_components/renpho/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify

from const import CM_TO_INCH, DOMAIN, KG_TO_LBS, METRIC_TYPE, METRIC_TYPE_WEIGHT, METRIC_TYPE_GIRTH, METRIC_TYPE_GIRTH_GOAL
from api_renpho import _LOGGER, RenphoWeight
from .const import CM_TO_INCH, DOMAIN, KG_TO_LBS, METRIC_TYPE, METRIC_TYPE_WEIGHT, METRIC_TYPE_GIRTH, METRIC_TYPE_GIRTH_GOAL
from .api_renpho import _LOGGER, RenphoWeight


async def sensors_list(
Expand Down Expand Up @@ -465,8 +465,16 @@ async def sensors_list(
"metric": METRIC_TYPE_GIRTH
},
{
"id": "arm_value",
"name": "Arm Value",
"id": "left_arm_value",
"name": "Left Arm Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
"metric": METRIC_TYPE_GIRTH
},
{
"id": "right_arm_value",
"name": "Right Arm Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
Expand Down Expand Up @@ -497,16 +505,32 @@ async def sensors_list(
"metric": METRIC_TYPE_GIRTH
},
{
"id": "thigh_value",
"name": "Thigh Value",
"id": "left_thigh_value",
"name": "Left Thigh Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
"metric": METRIC_TYPE_GIRTH
},
{
"id": "right_thigh_value",
"name": "Right Thigh Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
"metric": METRIC_TYPE_GIRTH
},
{
"id": "calf_value",
"name": "Calf Value",
"id": "left_calf_value",
"name": "Left Calf Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
"metric": METRIC_TYPE_GIRTH
},
{
"id": "right_calf_value",
"name": "Right Calf Value",
"unit": "cm",
"category": "Measurements",
"label": "Girth Measurements",
Expand Down Expand Up @@ -578,127 +602,127 @@ async def sensors_list(
},
# Girth Goals
{
"id": "neck_goal_value",
"id": "neck",
"name": "Neck Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "shoulder_goal_value",
"id": "shoulder",
"name": "Shoulder Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "arm_goal_value",
"id": "arm",
"name": "Arm Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "chest_goal_value",
"id": "chest",
"name": "Chest Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "waist_goal_value",
"id": "waist",
"name": "Waist Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "hip_goal_value",
"id": "hip",
"name": "Hip Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "thigh_goal_value",
"id": "thigh",
"name": "Thigh Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "calf_goal_value",
"id": "calf",
"name": "Calf Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "left_arm_goal_value",
"id": "left_arm",
"name": "Left Arm Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "left_thigh_goal_value",
"id": "left_thigh",
"name": "Left Thigh Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "left_calf_goal_value",
"id": "left_calf",
"name": "Left Calf Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "right_arm_goal_value",
"id": "right_arm",
"name": "Right Arm Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "right_thigh_goal_value",
"id": "right_thigh",
"name": "Right Thigh Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "right_calf_goal_value",
"id": "right_calf",
"name": "Right Calf Goal Value",
"unit": "cm",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "whr_goal_value",
"id": "whr",
"name": "WHR Goal Value",
"unit": "ratio",
"category": "Goals",
"label": "Girth Goals",
"metric": METRIC_TYPE_GIRTH_GOAL
},
{
"id": "abdomen_goal_value",
"id": "abdomen",
"name": "Abdomen Goal Value",
"unit": "cm",
"category": "Goals",
Expand Down Expand Up @@ -834,7 +858,8 @@ async def async_update(self) -> None:
self._state = metric_value

# Convert the unit if necessary
self._state = self.convert_unit(self._state, self._unit)
# if self._unit is not None or self._unit != "":
# self._state = self.convert_unit(self._state, self._unit)

# Update the timestamp
self._timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Expand All @@ -845,7 +870,4 @@ async def async_update(self) -> None:
_LOGGER.error(f"{type(e).__name__} occurred while updating {self._name} for metric type {self._metric}: {e}")

except Exception as e:
_LOGGER.critical(f"An unexpected error occurred while updating {self._name} for metric type {self._metric}: {e}")



_LOGGER.critical(f"An unexpected error occurred while updating {self._name} for metric type {self._metric}: {e}")
Loading

0 comments on commit 3ad29f1

Please sign in to comment.