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

feat: Add Shift State Sensor #569

Merged
merged 4 commits into from
Apr 21, 2023
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
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