Skip to content

Commit

Permalink
2022.12.4-beta.3 Fixes
Browse files Browse the repository at this point in the history
- Modify the title sensor to use the date as the value and set the state class
- Add the location and title to the entity as attributes
  • Loading branch information
craibo committed Dec 12, 2022
1 parent 3826e5b commit f1e4a4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
7 changes: 5 additions & 2 deletions custom_components/ha_strava/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,14 @@
CONF_SENSOR_12: CONF_SENSOR_KUDOS,
}

DEVICE_CLASS_DURATION = "duration"
DEVICE_CLASS_DISTANCE = "distance"

CONF_ATTR_START_LATLONG = "start_latlng"
CONF_ATTR_END_LATLONG = "end_latlng"
CONF_ATTR_SPORT_TYPE = "sport_type"
CONF_ATTR_DURATION = "duration"
CONF_ATTR_DISTANCE = "distance"
CONF_ATTR_LOCATION = "location"
CONF_ATTR_TITLE = "title"

UNIT_BEATS_PER_MINUTE = "bpm"
UNIT_PACE_MINUTES_PER_KILOMETER = "min/km"
Expand Down
41 changes: 26 additions & 15 deletions custom_components/ha_strava/sensor.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
"""Sensor platform for HA Strava"""
import logging

# generic imports
from datetime import datetime as dt

# HASS imports
from homeassistant.components.sensor import SensorEntity, SensorStateClass
from homeassistant.components.sensor.const import CONF_STATE_CLASS
from homeassistant.const import (
CONF_DEVICE_CLASS,
CONF_LATITUDE,
CONF_LONGITUDE,
DEVICE_CLASS_DATE,
DEVICE_CLASS_POWER,
LENGTH_FEET,
LENGTH_KILOMETERS,
Expand Down Expand Up @@ -38,10 +36,10 @@
CONF_ACTIVITY_TYPE_SWIM,
CONF_ACTIVITY_TYPE_WALK,
CONF_ACTIVITY_TYPE_WORKOUT,
CONF_ATTR_DISTANCE,
CONF_ATTR_DURATION,
CONF_ATTR_LOCATION,
CONF_ATTR_SPORT_TYPE,
CONF_ATTR_START_LATLONG,
CONF_ATTR_TITLE,
CONF_DISTANCE_UNIT_OVERRIDE,
CONF_DISTANCE_UNIT_OVERRIDE_DEFAULT,
CONF_DISTANCE_UNIT_OVERRIDE_METRIC,
Expand All @@ -66,6 +64,8 @@
CONF_SUMMARY_RECENT,
CONF_SUMMARY_YTD,
DEFAULT_NB_ACTIVITIES,
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_DURATION,
DOMAIN,
EVENT_ACTIVITIES_UPDATE,
EVENT_SUMMARY_STATS_UPDATE,
Expand Down Expand Up @@ -238,11 +238,11 @@ def capability_attributes(self): # pylint: disable=too-many-return-statements
return attr

if self._metric == CONF_SENSOR_MOVING_TIME:
attr[CONF_DEVICE_CLASS] = CONF_ATTR_DURATION
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DURATION
return attr

if self._metric == CONF_SENSOR_DISTANCE:
attr[CONF_DEVICE_CLASS] = CONF_ATTR_DISTANCE
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DISTANCE
return attr

return attr
Expand Down Expand Up @@ -334,7 +334,11 @@ def native_value(self):
# pylint: disable=too-many-return-statements,too-many-branches

if self._sensor_index == 0:
return f"{self._data[CONF_SENSOR_TITLE]} | {self._data[CONF_SENSOR_CITY]}"
return (
self._data.get(CONF_SENSOR_DATE)
if self._data.get(CONF_SENSOR_DATE)
else None
)

metric = self.get_metric()

Expand Down Expand Up @@ -493,10 +497,14 @@ def suggested_unit_of_measurement(
@property
def name(self): # pylint: disable=too-many-return-statements
if self._sensor_index == 0:
if self._data.get(CONF_SENSOR_CITY):
return (
f"{self._data[CONF_SENSOR_TITLE]} | {self._data[CONF_SENSOR_CITY]}"
)
return (
"Title & Date"
if not self._data
else f"{dt.strftime(self._data[CONF_SENSOR_DATE], '%d.%m.%y - %H:%M')}"
f"{self._data[CONF_SENSOR_TITLE]}"
if self._data.get(CONF_SENSOR_TITLE)
else "Title & Date"
)

metric = self.get_metric()
Expand Down Expand Up @@ -530,8 +538,11 @@ def capability_attributes(self): # pylint: disable=too-many-return-statements
return attr

if self._sensor_index == 0:
attr[CONF_STATE_CLASS] = None
attr.pop(CONF_STATE_CLASS, None)
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DATE
attr[CONF_ATTR_SPORT_TYPE] = self._data[CONF_ATTR_SPORT_TYPE]
attr[CONF_ATTR_LOCATION] = self._data[CONF_SENSOR_CITY]
attr[CONF_ATTR_TITLE] = self._data[CONF_SENSOR_TITLE]
if self._data[CONF_ATTR_START_LATLONG]:
attr[CONF_LATITUDE] = float(
self._data[CONF_ATTR_START_LATLONG][0]
Expand All @@ -543,19 +554,19 @@ def capability_attributes(self): # pylint: disable=too-many-return-statements

metric = self.get_metric()
if metric == CONF_SENSOR_MOVING_TIME:
attr[CONF_DEVICE_CLASS] = CONF_ATTR_DURATION
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DURATION
return attr

if metric == CONF_SENSOR_ELAPSED_TIME:
attr[CONF_DEVICE_CLASS] = CONF_ATTR_DURATION
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DURATION
return attr

if metric == CONF_SENSOR_POWER:
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_POWER
return attr

if metric == CONF_SENSOR_DISTANCE:
attr[CONF_DEVICE_CLASS] = CONF_ATTR_DISTANCE
attr[CONF_DEVICE_CLASS] = DEVICE_CLASS_DISTANCE
return attr

if metric == CONF_SENSOR_SPEED:
Expand Down

0 comments on commit f1e4a4e

Please sign in to comment.