Skip to content

Commit

Permalink
2022.12.5
Browse files Browse the repository at this point in the history
- Add Biggest Ride Disatance Sensor
- Add Biggest Elevation Climb Sensor
  • Loading branch information
craibo committed Dec 26, 2022
1 parent 3b7b6a6 commit 5bd80e0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
8 changes: 8 additions & 0 deletions custom_components/ha_strava/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
CONF_IMG_UPDATE_EVENT,
CONF_SENSOR_ACTIVITY_COUNT,
CONF_SENSOR_ACTIVITY_TYPE,
CONF_SENSOR_BIGGEST_ELEVATION_GAIN,
CONF_SENSOR_BIGGEST_RIDE_DISTANCE,
CONF_SENSOR_CALORIES,
CONF_SENSOR_CITY,
CONF_SENSOR_DATE,
Expand Down Expand Up @@ -332,6 +334,9 @@ def _sensor_summary_stats(self, summary_stats: dict) -> dict:
"moving_time", 0
)
),
CONF_SENSOR_BIGGEST_RIDE_DISTANCE: float(
summary_stats.get(CONF_SENSOR_BIGGEST_RIDE_DISTANCE, 0)
),
},
},
CONF_ACTIVITY_TYPE_RUN: {
Expand Down Expand Up @@ -385,6 +390,9 @@ def _sensor_summary_stats(self, summary_stats: dict) -> dict:
"moving_time", 0
)
),
CONF_SENSOR_BIGGEST_ELEVATION_GAIN: float(
summary_stats.get(CONF_SENSOR_BIGGEST_ELEVATION_GAIN, 0)
),
},
},
CONF_ACTIVITY_TYPE_SWIM: {
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ha_strava/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
CONF_SENSOR_ACTIVITY_TYPE = "activity_type"
CONF_SENSOR_HEART_RATE_AVG = "average_heartrate"
CONF_SENSOR_HEART_RATE_MAX = "max_heartrate"
CONF_SENSOR_BIGGEST_RIDE_DISTANCE = "biggest_ride_distance"
CONF_SENSOR_BIGGEST_ELEVATION_GAIN = "biggest_climb_elevation_gain"

CONF_ACTIVITY_TYPE_RUN = "run"
CONF_ACTIVITY_TYPE_RIDE = "ride"
Expand Down
68 changes: 64 additions & 4 deletions custom_components/ha_strava/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
CONF_DISTANCE_UNIT_OVERRIDE_DEFAULT,
CONF_DISTANCE_UNIT_OVERRIDE_METRIC,
CONF_SENSOR_ACTIVITY_COUNT,
CONF_SENSOR_BIGGEST_ELEVATION_GAIN,
CONF_SENSOR_BIGGEST_RIDE_DISTANCE,
CONF_SENSOR_CALORIES,
CONF_SENSOR_CITY,
CONF_SENSOR_DATE,
Expand Down Expand Up @@ -111,6 +113,23 @@ async def async_setup_entry(
summary_type=summary_type,
)
)
if metric == CONF_SENSOR_BIGGEST_ELEVATION_GAIN:
entries.append(
StravaSummaryStatsSensor(
activity_type=activity_type,
metric=metric,
summary_type=CONF_SUMMARY_ALL,
)
)

if metric == CONF_SENSOR_BIGGEST_RIDE_DISTANCE:
entries.append(
StravaSummaryStatsSensor(
activity_type=activity_type,
metric=metric,
summary_type=CONF_SUMMARY_ALL,
)
)

async_add_entities(entries)

Expand Down Expand Up @@ -162,7 +181,7 @@ def icon(self):
return CONF_SENSORS[self._metric]["icon"]

@property
def native_value(self):
def native_value(self): # pylint: disable=too-many-return-statements
if self._metric == CONF_SENSOR_MOVING_TIME:
return self._data[CONF_SENSOR_MOVING_TIME]

Expand All @@ -179,22 +198,53 @@ def native_value(self):
2,
)

if self._metric == CONF_SENSOR_BIGGEST_RIDE_DISTANCE:
self.set_distance_units()
distance = self._data[CONF_SENSOR_BIGGEST_RIDE_DISTANCE] / 1000
if self._is_unit_metric_default or self._is_unit_metric:
return round(distance, 2)

return round(
DistanceConverter.convert(
distance, UnitOfLength.KILOMETERS, UnitOfLength.MILES
),
2,
)

if self._metric == CONF_SENSOR_BIGGEST_ELEVATION_GAIN:
self.set_distance_units()
distance = self._data[CONF_SENSOR_BIGGEST_ELEVATION_GAIN]
if self._is_unit_metric_default or self._is_unit_metric:
return round(distance, 2)

return round(
DistanceConverter.convert(
distance, UnitOfLength.METERS, UnitOfLength.FEET
),
2,
)

return int(self._data[CONF_SENSOR_ACTIVITY_COUNT])

@property
def native_unit_of_measurement(self):
def native_unit_of_measurement(self): # pylint: disable=too-many-return-statements
if self._metric not in [CONF_SENSOR_MOVING_TIME, CONF_SENSOR_DISTANCE]:
return None

if self._metric == CONF_SENSOR_MOVING_TIME:
return TIME_SECONDS

self.set_distance_units()
if self._metric == CONF_SENSOR_DISTANCE:
if self._metric in [CONF_SENSOR_DISTANCE, CONF_SENSOR_BIGGEST_RIDE_DISTANCE]:
if self._is_unit_metric_default or self._is_unit_metric:
return UnitOfLength.KILOMETERS
return UnitOfLength.MILES

if self._metric == CONF_SENSOR_BIGGEST_ELEVATION_GAIN:
if self._is_unit_metric_default or self._is_unit_metric:
return UnitOfLength.METERS
return UnitOfLength.FEET

return None

@property
Expand All @@ -209,6 +259,12 @@ def suggested_unit_of_measurement(self):

@property
def name(self):
if self._metric == CONF_SENSOR_BIGGEST_ELEVATION_GAIN:
return "ALL Run Biggest Elevation Gain"

if self._metric == CONF_SENSOR_BIGGEST_ELEVATION_GAIN:
return "ALL Ride Biggest Distance"

ret = ""
if self._summary_type == CONF_SUMMARY_YTD:
ret += "YTD "
Expand Down Expand Up @@ -239,7 +295,11 @@ def capability_attributes(self): # pylint: disable=too-many-return-statements
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DURATION
return attr

if self._metric == CONF_SENSOR_DISTANCE:
if self._metric in [
CONF_SENSOR_DISTANCE,
CONF_SENSOR_BIGGEST_ELEVATION_GAIN,
CONF_SENSOR_BIGGEST_RIDE_DISTANCE,
]:
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DISTANCE
return attr

Expand Down

0 comments on commit 5bd80e0

Please sign in to comment.