Skip to content

Commit

Permalink
fix: set sensor device class (#292)
Browse files Browse the repository at this point in the history
closes #284
  • Loading branch information
shred86 committed Oct 22, 2022
1 parent 1bc6d7f commit 658295f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 42 deletions.
48 changes: 6 additions & 42 deletions custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
PERCENTAGE,
POWER_WATT,
POWER_KILO_WATT,
SPEED_KILOMETERS_PER_HOUR,
SPEED_MILES_PER_HOUR,
TEMP_CELSIUS,
)
Expand Down Expand Up @@ -203,7 +202,9 @@ def __init__(
"""Initialize charging rate entity."""
super().__init__(hass, car, coordinator)
self.type = "charging rate"
self._attr_device_class = SensorDeviceClass.SPEED
self._attr_state_class = SensorStateClass.MEASUREMENT
self._attr_native_unit_of_measurement = SPEED_MILES_PER_HOUR
self._attr_icon = "mdi:speedometer"

@property
Expand All @@ -214,21 +215,8 @@ def native_value(self) -> float:
if charge_rate is None:
return charge_rate

if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
charge_rate = DistanceConverter.convert(
charge_rate, LENGTH_MILES, LENGTH_KILOMETERS
)

return round(charge_rate, 2)

@property
def native_unit_of_measurement(self) -> str:
"""Return distance units."""
if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
return SPEED_KILOMETERS_PER_HOUR

return SPEED_MILES_PER_HOUR

@property
def extra_state_attributes(self):
"""Return device state attributes."""
Expand All @@ -249,8 +237,9 @@ def __init__(
"""Initialize odometer entity."""
super().__init__(hass, car, coordinator)
self.type = "odometer"
self._attr_device_class = None
self._attr_device_class = SensorDeviceClass.DISTANCE
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
self._attr_native_unit_of_measurement = LENGTH_MILES
self._attr_icon = "mdi:counter"

@property
Expand All @@ -261,21 +250,8 @@ def native_value(self) -> float:
if odometer_value is None:
return None

if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
odometer_value = DistanceConverter.convert(
odometer_value, LENGTH_MILES, LENGTH_KILOMETERS
)

return round(odometer_value, 2)

@property
def native_unit_of_measurement(self) -> str:
"""Return distance units."""
if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
return LENGTH_KILOMETERS

return LENGTH_MILES


class TeslaCarRange(TeslaCarEntity, SensorEntity):
"""Representation of the Tesla car range sensor."""
Expand All @@ -289,8 +265,9 @@ def __init__(
"""Initialize range entity."""
super().__init__(hass, car, coordinator)
self.type = "range"
self._attr_device_class = None
self._attr_device_class = SensorDeviceClass.DISTANCE
self._attr_state_class = SensorStateClass.MEASUREMENT
self._attr_native_unit_of_measurement = LENGTH_MILES
self._attr_icon = "mdi:gauge"

@property
Expand All @@ -304,21 +281,8 @@ def native_value(self) -> float:
if range_value is None:
return None

if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
range_value = DistanceConverter.convert(
range_value, LENGTH_MILES, LENGTH_KILOMETERS
)

return round(range_value, 2)

@property
def native_unit_of_measurement(self) -> str:
"""Return distance units."""
if self._car.gui_distance_units == DISTANCE_UNITS_KM_HR:
return LENGTH_KILOMETERS

return LENGTH_MILES


class TeslaCarTemp(TeslaCarEntity, SensorEntity):
"""Representation of a Tesla car temp sensor."""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ async def test_charger_rate_value(hass: HomeAssistant) -> None:
state = hass.states.get("sensor.my_model_s_charging_rate")
assert state.state == str(car_mock_data.VEHICLE_DATA["charge_state"]["charge_rate"])

assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.SPEED
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT

assert (
Expand Down Expand Up @@ -245,6 +246,7 @@ async def test_odometer_value(hass: HomeAssistant) -> None:
round(car_mock_data.VEHICLE_DATA["vehicle_state"]["odometer"], 1)
)

assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DISTANCE
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_MILES

Expand Down Expand Up @@ -272,6 +274,7 @@ async def test_range_value(hass: HomeAssistant) -> None:
car_mock_data.VEHICLE_DATA["charge_state"]["battery_range"]
)

assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DISTANCE
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == LENGTH_MILES

Expand Down

0 comments on commit 658295f

Please sign in to comment.