Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set sensor device class #292

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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