Skip to content

Commit

Permalink
fix: fix usable_battery_level to match app (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
carleeno authored Nov 20, 2022
1 parent 30f319c commit c9e4abb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"documentation": "https://github.com/alandtse/tesla/wiki",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"requirements": [
"teslajsonpy==3.1.0"
"teslajsonpy==3.2.0"
],
"codeowners": [
"@alandtse"
Expand Down
12 changes: 10 additions & 2 deletions custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,25 @@ def has_battery() -> bool:
@property
def native_value(self) -> int:
"""Return battery level."""
return self._car.battery_level
# usable_battery_level matches the Tesla app and car display
return self._car.usable_battery_level

@property
def icon(self):
"""Return icon for the battery."""
charging = self._car.battery_level == "Charging"
charging = self._car.charging_state == "Charging"

return icon_for_battery_level(
battery_level=self.native_value, charging=charging
)

@property
def extra_state_attributes(self):
"""Return device state attributes."""
return {
"raw_soc": self._car.battery_level,
}


class TeslaCarChargerEnergy(TeslaCarEntity, SensorEntity):
"""Representation of a Tesla car energy added sensor."""
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "Apache-2.0"

[tool.poetry.dependencies]
python = "^3.10"
teslajsonpy = "^3.1.0"
teslajsonpy = "^3.2.0"

[tool.poetry.dev-dependencies]
homeassistant = ">=2021.10.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/mock_data/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"time_to_full_charge": 0.25,
"timestamp": 1661641175268,
"trip_charging": False,
"usable_battery_level": 78,
"usable_battery_level": 77,
"user_charge_enable_request": None,
},
"climate_state": {
Expand Down
6 changes: 5 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,16 @@ async def test_battery_value(hass: HomeAssistant) -> None:

state = hass.states.get("sensor.my_model_s_battery")
assert state.state == str(
car_mock_data.VEHICLE_DATA["charge_state"]["battery_level"]
car_mock_data.VEHICLE_DATA["charge_state"]["usable_battery_level"]
)

assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.BATTERY
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert (
state.attributes.get("raw_soc")
== car_mock_data.VEHICLE_DATA["charge_state"]["battery_level"]
)


async def test_charger_energy_value(hass: HomeAssistant) -> None:
Expand Down

0 comments on commit c9e4abb

Please sign in to comment.