Skip to content

Commit

Permalink
2022.12.4-beta.9 Bug fixes
Browse files Browse the repository at this point in the history
- Resolve issue with rounding
  • Loading branch information
craibo committed Dec 16, 2022
1 parent de90f26 commit 2f379c3
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions custom_components/ha_strava/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ def native_value(self):

if self._metric == CONF_SENSOR_DISTANCE:
self.set_distance_units()
distance = self._data[CONF_SENSOR_DISTANCE] / 1000
if self._is_unit_metric_default or self._is_unit_metric:
return f"{round(self._data[CONF_SENSOR_DISTANCE]/1000,2)}"
return round(distance, 2)

return DistanceConverter.convert(
(self._data[CONF_SENSOR_DISTANCE] / 1000),
UnitOfLength.KILOMETERS,
UnitOfLength.MILES,
return round(
DistanceConverter.convert(
distance, UnitOfLength.KILOMETERS, UnitOfLength.MILES
),
2,
)

return int(self._data[CONF_SENSOR_ACTIVITY_COUNT])
Expand Down Expand Up @@ -361,11 +363,14 @@ def native_value(self):
return self._data[CONF_SENSOR_ELAPSED_TIME]

if metric == CONF_SENSOR_DISTANCE:
distance = f"{round(self._data[CONF_SENSOR_DISTANCE]/1000,2)}"
distance = self._data[CONF_SENSOR_DISTANCE] / 1000
if self._is_unit_metric_default or self._is_unit_metric:
return distance
return DistanceConverter.convert(
distance, UnitOfLength.KILOMETERS, UnitOfLength.MILES
return round(distance, 2)
return round(
DistanceConverter.convert(
distance, UnitOfLength.KILOMETERS, UnitOfLength.MILES
),
2,
)

if metric == CONF_SENSOR_PACE:
Expand Down Expand Up @@ -395,11 +400,16 @@ def native_value(self):
return "".join(["" if minutes == 0 else f"{minutes:02}:", f"{seconds:02}"])

if metric == CONF_SENSOR_SPEED:
speed = f"{round((self._data[CONF_SENSOR_DISTANCE]/1000) / (self._data[CONF_SENSOR_MOVING_TIME]/3600),2)}"
speed = (self._data[CONF_SENSOR_DISTANCE] / 1000) / (
self._data[CONF_SENSOR_MOVING_TIME] / 3600
)
if self._is_unit_metric_default or self._is_unit_metric:
return speed
return SpeedConverter.convert(
speed, UnitOfSpeed.KILOMETERS_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR
return round(speed, 2)
return round(
SpeedConverter.convert(
speed, UnitOfSpeed.KILOMETERS_PER_HOUR, UnitOfSpeed.MILES_PER_HOUR
),
2,
)

if metric == CONF_SENSOR_POWER:
Expand All @@ -417,11 +427,14 @@ def native_value(self):
)

if metric == CONF_SENSOR_ELEVATION:
elevation_gain = f"{round(self._data[CONF_SENSOR_ELEVATION],0)}"
elevation_gain = self._data[CONF_SENSOR_ELEVATION]
if self._is_unit_metric_default or self._is_unit_metric:
return elevation_gain
return DistanceConverter.convert(
elevation_gain, UnitOfLength.METERS, UnitOfLength.FEET
return round(elevation_gain, 2)
return round(
DistanceConverter.convert(
elevation_gain, UnitOfLength.METERS, UnitOfLength.FEET
),
2,
)

if metric == CONF_SENSOR_HEART_RATE_AVG:
Expand Down

0 comments on commit 2f379c3

Please sign in to comment.