Skip to content

Commit

Permalink
feat: Add Shift State Sensor (#569)
Browse files Browse the repository at this point in the history
closes #476
  • Loading branch information
Megabytemb authored Apr 21, 2023
1 parent 437212b commit 72ac435
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
entities.append(TeslaCarChargerEnergy(hass, car, coordinator))
entities.append(TeslaCarChargerPower(hass, car, coordinator))
entities.append(TeslaCarOdometer(hass, car, coordinator))
entities.append(TeslaCarShiftState(hass, car, coordinator))
entities.append(TeslaCarRange(hass, car, coordinator))
entities.append(TeslaCarTemp(hass, car, coordinator))
entities.append(TeslaCarTemp(hass, car, coordinator, inside=True))
Expand Down Expand Up @@ -293,6 +294,48 @@ def native_value(self) -> float:
return round(odometer_value, 2)


class TeslaCarShiftState(TeslaCarEntity, SensorEntity):
"""Representation of the Tesla car Shift State sensor."""

def __init__(
self,
hass: HomeAssistant,
car: TeslaCar,
coordinator: TeslaDataUpdateCoordinator,
) -> None:
"""Initialize odometer entity."""
super().__init__(hass, car, coordinator)
self.type = "shift state"
self._attr_device_class = SensorDeviceClass.ENUM
self._attr_icon = "mdi:car-shift-pattern"

@property
def native_value(self) -> float:
"""Return the shift state."""
value = self._car.shift_state

# When car is parked and off, Tesla API reports shift_state None
if value is None or value == "":
return "P"

return value

@property
def options(self) -> float:
"""Return the values for the ENUM."""
values = ["P", "D", "R", "N"]

return values

@property
def extra_state_attributes(self):
"""Return device state attributes."""

return {
"raw_state": self._car.shift_state,
}


class TeslaCarRange(TeslaCarEntity, SensorEntity):
"""Representation of the Tesla car range sensor."""

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Tesla",
"hacs": "1.6.0",
"homeassistant": "2022.11.0",
"homeassistant": "2023.1.0",
"zip_release": true,
"filename": "tesla_custom.zip"
}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ teslajsonpy = "^3.8.0"


[tool.poetry.group.dev.dependencies]
homeassistant = ">=2022.11.0"
homeassistant = ">=2023.1.0"
pytest-homeassistant-custom-component = ">=0.13.1"
bandit = ">=1.7.0"
black = {version = ">=21.12b0", allow-prereleases = true}
Expand Down

0 comments on commit 72ac435

Please sign in to comment.