Skip to content

Commit

Permalink
Add sunroof
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 4, 2024
1 parent 50e67c3 commit 9de49f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
48 changes: 45 additions & 3 deletions custom_components/teslemetry/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ async def async_setup_entry(
TeslemetryChargePortEntity,
TeslemetryFrontTrunkEntity,
TeslemetryRearTrunkEntity,
TeslemetrySunroofEntity,
)
for vehicle in entry.runtime_data.vehicles
)


class TeslemetryWindowEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for current charge."""
"""Cover entity for windows."""

_attr_device_class = CoverDeviceClass.WINDOW
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
Expand Down Expand Up @@ -131,7 +132,7 @@ async def async_close_cover(self, **kwargs: Any) -> None:


class TeslemetryFrontTrunkEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the charge port."""
"""Cover entity for the front trunk."""

_attr_device_class = CoverDeviceClass.DOOR
_attr_supported_features = CoverEntityFeature.OPEN
Expand Down Expand Up @@ -160,7 +161,7 @@ async def async_open_cover(self, **kwargs: Any) -> None:


class TeslemetryRearTrunkEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the charge port."""
"""Cover entity for the rear trunk."""

_attr_device_class = CoverDeviceClass.DOOR
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
Expand Down Expand Up @@ -200,3 +201,44 @@ async def async_close_cover(self, **kwargs: Any) -> None:
await self.handle_command(self.api.actuate_trunk(Trunk.REAR))
self._attr_is_closed = True
self.async_write_ha_state()

class TeslemetrySunroofEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the sunroof."""

_attr_device_class = CoverDeviceClass.WINDOW
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
_attr_entity_registry_enabled_default = False

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_sun_roof_state")

self.scoped = Scope.VEHICLE_CMDS in scopes
if not self.scoped:
self._attr_supported_features = CoverEntityFeature(0)

def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
value = self._value
if value == None or value == "unknown":
self._attr_is_closed = None
else:
self._attr_is_closed = value == "closed"

self._attr_current_cover_position = self.get("vehicle_state_sun_roof_percent_open")

async def async_open_cover(self, **kwargs: Any) -> None:
"""Open sunroof."""
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.handle_command(self.api.sun_roof_control("vent"))
self._attr_is_closed = False
self.async_write_ha_state()

async def async_close_cover(self, **kwargs: Any) -> None:
"""Close sunroof."""
self.raise_for_scope()
await self.wake_up_if_asleep()
await self.handle_command(self.api.sun_roof_control("close"))
self._attr_is_closed = True
self.async_write_ha_state()
3 changes: 3 additions & 0 deletions custom_components/teslemetry/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@
},
"windows": {
"name": "Vent windows"
},
"vehicle_state_sun_roof_state": {
"name": "Sun roof"
}
},
"device_tracker": {
Expand Down

0 comments on commit 9de49f2

Please sign in to comment.