Skip to content

Commit

Permalink
feat: add estimated battery range attributes (#443)
Browse files Browse the repository at this point in the history
closes #412
  • Loading branch information
InTheDaylight14 committed Dec 20, 2022
1 parent f555131 commit 7584fdc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
15 changes: 15 additions & 0 deletions custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,21 @@ def native_value(self) -> float:

return round(range_value, 2)

@property
def extra_state_attributes(self):
"""Return device state attributes."""
est_battery_range = self._car._vehicle_data.get("charge_state", {}).get(
"est_battery_range"
)
est_battery_range_km = DistanceConverter.convert(
est_battery_range, LENGTH_MILES, LENGTH_KILOMETERS
)

return {
"est_battery_range_miles": est_battery_range,
"est_battery_range_km": est_battery_range_km,
}


class TeslaCarTemp(TeslaCarEntity, SensorEntity):
"""Representation of a Tesla car temp sensor."""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ async def test_range_value(hass: HomeAssistant) -> None:
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DISTANCE
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT

assert (
state.attributes.get("est_battery_range_miles")
== car_mock_data.VEHICLE_DATA["charge_state"]["est_battery_range"]
)
est_range_km = DistanceConverter.convert(
car_mock_data.VEHICLE_DATA["charge_state"]["est_battery_range"],
LENGTH_MILES,
LENGTH_KILOMETERS,
)

assert state.attributes.get("est_battery_range_km") == est_range_km


async def test_solar_power_value(hass: HomeAssistant) -> None:
"""Tests solar_power is getting the correct value."""
Expand Down

0 comments on commit 7584fdc

Please sign in to comment.