Skip to content

Commit

Permalink
2022.12.4-beta.4 Bug Fixes
Browse files Browse the repository at this point in the history
- Fix comparrison for power and calories
- Update deprecated constants to use UnitOfPower, UnitOfLength, UnitOfDistance
  • Loading branch information
craibo committed Dec 12, 2022
1 parent 0bd3d27 commit 62e7842
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions custom_components/ha_strava/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
CONF_LONGITUDE,
DEVICE_CLASS_DATE,
DEVICE_CLASS_POWER,
LENGTH_FEET,
LENGTH_KILOMETERS,
LENGTH_METERS,
LENGTH_MILES,
POWER_WATT,
SPEED,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
TIME_SECONDS,
UnitOfLength,
UnitOfPower,
UnitOfSpeed,
)
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM

Expand Down Expand Up @@ -182,7 +178,7 @@ def native_unit_of_measurement(self):
return TIME_SECONDS

if self._metric == CONF_SENSOR_DISTANCE:
return LENGTH_KILOMETERS
return UnitOfLength.KILOMETERS

return None

Expand All @@ -205,7 +201,7 @@ def suggested_unit_of_measurement(self):
)

if self._metric == CONF_SENSOR_DISTANCE:
return LENGTH_KILOMETERS if is_metric else LENGTH_MILES
return UnitOfLength.KILOMETERS if is_metric else UnitOfLength.MILES

return super().suggested_unit_of_measurement

Expand Down Expand Up @@ -388,14 +384,14 @@ def native_value(self):
if metric == CONF_SENSOR_POWER:
return (
f"{int(round(self._data[CONF_SENSOR_POWER],1))}"
if (self._data.get(CONF_SENSOR_POWER) != "-1")
if (self._data.get(CONF_SENSOR_POWER) != -1)
else None
)

if metric == CONF_SENSOR_CALORIES:
return (
str(self._data.get(metric))
if (self._data.get(CONF_SENSOR_CALORIES) == "-1")
if (self._data.get(CONF_SENSOR_CALORIES) != -1)
else None
)

Expand All @@ -421,16 +417,16 @@ def native_unit_of_measurement(self): # pylint: disable=too-many-return-stateme
return TIME_SECONDS

if metric == CONF_SENSOR_POWER:
return POWER_WATT
return UnitOfPower.WATT

if metric == CONF_SENSOR_ELEVATION:
return LENGTH_METERS
return UnitOfLength.METERS

if metric == CONF_SENSOR_SPEED:
return SPEED_KILOMETERS_PER_HOUR
return UnitOfSpeed.KILOMETERS_PER_HOUR

if metric == CONF_SENSOR_DISTANCE:
return LENGTH_KILOMETERS
return UnitOfLength.KILOMETERS

if metric in [CONF_SENSOR_HEART_RATE_MAX, CONF_SENSOR_HEART_RATE_AVG]:
return UNIT_BEATS_PER_MINUTE
Expand Down Expand Up @@ -477,13 +473,17 @@ def suggested_unit_of_measurement(
is_metric = conf_distance_unit_override == CONF_DISTANCE_UNIT_OVERRIDE_METRIC

if metric == CONF_SENSOR_DISTANCE:
return LENGTH_KILOMETERS if is_metric else LENGTH_MILES
return UnitOfLength.KILOMETERS if is_metric else UnitOfLength.MILES

if metric == CONF_SENSOR_SPEED:
return SPEED_KILOMETERS_PER_HOUR if is_metric else SPEED_MILES_PER_HOUR
return (
UnitOfSpeed.KILOMETERS_PER_HOUR
if is_metric
else UnitOfSpeed.MILES_PER_HOUR
)

if metric == CONF_SENSOR_ELEVATION:
return LENGTH_METERS if is_metric else LENGTH_FEET
return UnitOfLength.METERS if is_metric else UnitOfLength.FEET

if metric == CONF_SENSOR_PACE:
return (
Expand Down

0 comments on commit 62e7842

Please sign in to comment.