2024-05-24 10:09:23 +00:00
|
|
|
"""Cover platform for Teslemetry integration."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
from itertools import chain
|
2024-05-24 10:09:23 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2024-07-09 06:36:34 +00:00
|
|
|
from tesla_fleet_api.const import Scope, SunRoofCommand, Trunk, WindowCommand
|
2025-01-17 16:38:03 +00:00
|
|
|
from teslemetry_stream import Signal
|
|
|
|
from teslemetry_stream.const import WindowState
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
from homeassistant.components.cover import (
|
|
|
|
CoverDeviceClass,
|
|
|
|
CoverEntity,
|
|
|
|
CoverEntityFeature,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2025-01-17 16:38:03 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2024-05-24 10:09:23 +00:00
|
|
|
|
2024-05-27 17:11:55 +00:00
|
|
|
from . import TeslemetryConfigEntry
|
2025-01-17 16:38:03 +00:00
|
|
|
from .entity import (
|
|
|
|
TeslemetryRootEntity,
|
|
|
|
TeslemetryVehicleEntity,
|
|
|
|
TeslemetryVehicleStreamEntity,
|
|
|
|
)
|
2024-06-10 19:12:09 +00:00
|
|
|
from .helpers import handle_vehicle_command
|
2024-05-24 10:09:23 +00:00
|
|
|
from .models import TeslemetryVehicleData
|
|
|
|
|
|
|
|
OPEN = 1
|
|
|
|
CLOSED = 0
|
|
|
|
|
2024-06-22 06:56:50 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
2024-05-27 17:11:55 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: TeslemetryConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2024-05-24 10:09:23 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Teslemetry cover platform from a config entry."""
|
|
|
|
|
|
|
|
async_add_entities(
|
2025-01-17 16:38:03 +00:00
|
|
|
chain(
|
|
|
|
(
|
|
|
|
TeslemetryPollingWindowEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
if vehicle.api.pre2021 or vehicle.firmware < "2024.26"
|
|
|
|
else TeslemetryStreamingWindowEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TeslemetryPollingChargePortEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
if vehicle.api.pre2021 or vehicle.firmware < "2024.44.25"
|
|
|
|
else TeslemetryStreamingChargePortEntity(
|
|
|
|
vehicle, entry.runtime_data.scopes
|
|
|
|
)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TeslemetryPollingFrontTrunkEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
if vehicle.api.pre2021 or vehicle.firmware < "2024.26"
|
|
|
|
else TeslemetryStreamingFrontTrunkEntity(
|
|
|
|
vehicle, entry.runtime_data.scopes
|
|
|
|
)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TeslemetryPollingRearTrunkEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
if vehicle.api.pre2021 or vehicle.firmware < "2024.26"
|
|
|
|
else TeslemetryStreamingRearTrunkEntity(
|
|
|
|
vehicle, entry.runtime_data.scopes
|
|
|
|
)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TeslemetrySunroofEntity(vehicle, entry.runtime_data.scopes)
|
|
|
|
for vehicle in entry.runtime_data.vehicles
|
|
|
|
if vehicle.coordinator.data.get("vehicle_config_sun_roof_installed")
|
|
|
|
),
|
|
|
|
)
|
2024-05-24 10:09:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class CoverRestoreEntity(RestoreEntity, CoverEntity):
|
|
|
|
"""Restore class for cover entities."""
|
2024-05-24 10:09:23 +00:00
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
if (state := await self.async_get_last_state()) is not None:
|
|
|
|
if state.state == "open":
|
|
|
|
self._attr_is_closed = False
|
|
|
|
elif state.state == "closed":
|
|
|
|
self._attr_is_closed = True
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class TeslemetryWindowEntity(TeslemetryRootEntity, CoverEntity):
|
|
|
|
"""Base class for window cover entities."""
|
2024-05-24 10:09:23 +00:00
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
_attr_device_class = CoverDeviceClass.WINDOW
|
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Vent windows."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(
|
|
|
|
self.api.window_control(command=WindowCommand.VENT)
|
|
|
|
)
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close windows."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(
|
|
|
|
self.api.window_control(command=WindowCommand.CLOSE)
|
|
|
|
)
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class TeslemetryPollingWindowEntity(
|
|
|
|
TeslemetryVehicleEntity, TeslemetryWindowEntity, CoverEntity
|
|
|
|
):
|
|
|
|
"""Polling cover entity for windows."""
|
2024-05-24 10:09:23 +00:00
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
def __init__(self, data: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
2024-05-24 10:09:23 +00:00
|
|
|
"""Initialize the cover."""
|
2025-01-17 16:38:03 +00:00
|
|
|
super().__init__(data, "windows")
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
2024-05-24 10:09:23 +00:00
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update the entity attributes."""
|
2025-01-17 16:38:03 +00:00
|
|
|
fd = self.get("vehicle_state_fd_window")
|
|
|
|
fp = self.get("vehicle_state_fp_window")
|
|
|
|
rd = self.get("vehicle_state_rd_window")
|
|
|
|
rp = self.get("vehicle_state_rp_window")
|
|
|
|
|
|
|
|
if OPEN in (fd, fp, rd, rp):
|
|
|
|
self._attr_is_closed = False
|
|
|
|
elif None in (fd, fp, rd, rp):
|
|
|
|
self._attr_is_closed = None
|
|
|
|
else:
|
|
|
|
self._attr_is_closed = True
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryStreamingWindowEntity(
|
|
|
|
TeslemetryVehicleStreamEntity, TeslemetryWindowEntity, CoverRestoreEntity
|
|
|
|
):
|
|
|
|
"""Streaming cover entity for windows."""
|
|
|
|
|
|
|
|
fd: bool | None = None
|
|
|
|
fp: bool | None = None
|
|
|
|
rd: bool | None = None
|
|
|
|
rp: bool | None = None
|
|
|
|
|
|
|
|
def __init__(self, data: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the cover."""
|
|
|
|
super().__init__(
|
|
|
|
data,
|
|
|
|
"windows",
|
|
|
|
)
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
self._attr_is_closed = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
|
|
self.stream.async_add_listener(
|
|
|
|
self._handle_stream_update,
|
|
|
|
{"vin": self.vin, "data": {self.streaming_key: None}},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
for signal in (
|
|
|
|
Signal.FD_WINDOW,
|
|
|
|
Signal.FP_WINDOW,
|
|
|
|
Signal.RD_WINDOW,
|
|
|
|
Signal.RP_WINDOW,
|
|
|
|
):
|
|
|
|
self.vehicle.config_entry.async_create_background_task(
|
|
|
|
self.hass,
|
|
|
|
self.add_field(signal),
|
|
|
|
f"Adding field {signal} to {self.vehicle.vin}",
|
|
|
|
)
|
|
|
|
|
|
|
|
def _handle_stream_update(self, data) -> None:
|
|
|
|
"""Update the entity attributes."""
|
|
|
|
|
|
|
|
if value := data.get(Signal.FD_WINDOW):
|
|
|
|
self.fd = WindowState.get(value) == "closed"
|
|
|
|
if value := data.get(Signal.FP_WINDOW):
|
|
|
|
self.fp = WindowState.get(value) == "closed"
|
|
|
|
if value := data.get(Signal.RD_WINDOW):
|
|
|
|
self.rd = WindowState.get(value) == "closed"
|
|
|
|
if value := data.get(Signal.RP_WINDOW):
|
|
|
|
self.rp = WindowState.get(value) == "closed"
|
|
|
|
|
|
|
|
if False in (self.fd, self.fp, self.rd, self.rp):
|
|
|
|
self._attr_is_closed = False
|
|
|
|
elif None in (self.fd, self.fp, self.rd, self.rp):
|
|
|
|
self._attr_is_closed = None
|
|
|
|
else:
|
|
|
|
self._attr_is_closed = True
|
|
|
|
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryChargePortEntity(
|
|
|
|
TeslemetryRootEntity,
|
|
|
|
CoverEntity,
|
|
|
|
):
|
|
|
|
"""Base class for for charge port cover entities."""
|
|
|
|
|
|
|
|
_attr_device_class = CoverDeviceClass.DOOR
|
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Open charge port."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CHARGING_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(self.api.charge_port_door_open())
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close charge port."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CHARGING_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(self.api.charge_port_door_close())
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class TeslemetryPollingChargePortEntity(
|
|
|
|
TeslemetryVehicleEntity, TeslemetryChargePortEntity
|
|
|
|
):
|
|
|
|
"""Polling cover entity for the charge port."""
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the cover."""
|
2025-01-17 16:38:03 +00:00
|
|
|
super().__init__(vehicle, "charge_state_charge_port_door_open")
|
|
|
|
self.scoped = any(
|
|
|
|
scope in scopes
|
|
|
|
for scope in (Scope.VEHICLE_CMDS, Scope.VEHICLE_CHARGING_CMDS)
|
|
|
|
)
|
|
|
|
self._attr_supported_features = (
|
|
|
|
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
|
|
|
)
|
2024-05-24 10:09:23 +00:00
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update the entity attributes."""
|
2025-01-17 16:38:03 +00:00
|
|
|
self._attr_is_closed = not self._value
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryStreamingChargePortEntity(
|
|
|
|
TeslemetryVehicleStreamEntity, TeslemetryChargePortEntity, CoverRestoreEntity
|
|
|
|
):
|
|
|
|
"""Streaming cover entity for the charge port."""
|
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(
|
|
|
|
vehicle,
|
|
|
|
"charge_state_charge_port_door_open",
|
|
|
|
)
|
|
|
|
self.scoped = any(
|
|
|
|
scope in scopes
|
|
|
|
for scope in (Scope.VEHICLE_CMDS, Scope.VEHICLE_CHARGING_CMDS)
|
|
|
|
)
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
self._attr_is_closed = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
|
|
self.vehicle.stream_vehicle.listen_ChargePortDoorOpen(
|
|
|
|
self._async_value_from_stream
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _async_value_from_stream(self, value: bool | None) -> None:
|
|
|
|
"""Update the value of the entity."""
|
|
|
|
self._attr_is_closed = None if value is None else not value
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryFrontTrunkEntity(TeslemetryRootEntity, CoverEntity):
|
|
|
|
"""Base class for the front trunk cover entities."""
|
|
|
|
|
|
|
|
_attr_device_class = CoverDeviceClass.DOOR
|
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Open front trunk."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(self.api.actuate_trunk(Trunk.FRONT))
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
# In the future this could be extended to add aftermarket close support through a option flow
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class TeslemetryPollingFrontTrunkEntity(
|
|
|
|
TeslemetryVehicleEntity, TeslemetryFrontTrunkEntity
|
|
|
|
):
|
|
|
|
"""Polling cover entity for the front trunk."""
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the cover."""
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
2025-01-17 16:38:03 +00:00
|
|
|
super().__init__(vehicle, "vehicle_state_ft")
|
2024-05-24 10:09:23 +00:00
|
|
|
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update the entity attributes."""
|
2024-11-07 09:36:43 +00:00
|
|
|
self._attr_is_closed = self._value == CLOSED
|
2024-05-24 10:09:23 +00:00
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
|
|
|
|
class TeslemetryStreamingFrontTrunkEntity(
|
|
|
|
TeslemetryVehicleStreamEntity, TeslemetryFrontTrunkEntity, CoverRestoreEntity
|
|
|
|
):
|
|
|
|
"""Streaming cover entity for the front trunk."""
|
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(vehicle, "vehicle_state_ft")
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
self._attr_is_closed = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
|
|
self.vehicle.stream_vehicle.listen_TrunkFront(self._async_value_from_stream)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _async_value_from_stream(self, value: bool | None) -> None:
|
|
|
|
"""Update the entity attributes."""
|
|
|
|
|
|
|
|
self._attr_is_closed = None if value is None else not value
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryRearTrunkEntity(TeslemetryRootEntity, CoverEntity):
|
|
|
|
"""Cover entity for the rear trunk."""
|
|
|
|
|
|
|
|
_attr_device_class = CoverDeviceClass.DOOR
|
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
|
|
|
|
2024-05-24 10:09:23 +00:00
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Open rear trunk."""
|
|
|
|
if self.is_closed is not False:
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(self.api.actuate_trunk(Trunk.REAR))
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close rear trunk."""
|
|
|
|
if self.is_closed is not True:
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2025-01-17 16:38:03 +00:00
|
|
|
|
2024-06-10 19:12:09 +00:00
|
|
|
await handle_vehicle_command(self.api.actuate_trunk(Trunk.REAR))
|
2024-05-24 10:09:23 +00:00
|
|
|
self._attr_is_closed = True
|
|
|
|
self.async_write_ha_state()
|
2024-07-09 06:36:34 +00:00
|
|
|
|
|
|
|
|
2025-01-17 16:38:03 +00:00
|
|
|
class TeslemetryPollingRearTrunkEntity(
|
|
|
|
TeslemetryVehicleEntity, TeslemetryRearTrunkEntity
|
|
|
|
):
|
|
|
|
"""Base class for the rear trunk cover entities."""
|
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
super().__init__(vehicle, "vehicle_state_rt")
|
|
|
|
|
|
|
|
def _async_update_attrs(self) -> None:
|
|
|
|
"""Update the entity attributes."""
|
|
|
|
self._attr_is_closed = self._value == CLOSED
|
|
|
|
|
|
|
|
|
|
|
|
class TeslemetryStreamingRearTrunkEntity(
|
|
|
|
TeslemetryVehicleStreamEntity, TeslemetryRearTrunkEntity, CoverRestoreEntity
|
|
|
|
):
|
|
|
|
"""Polling cover entity for the rear trunk."""
|
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
|
|
|
"""Initialize the cover."""
|
|
|
|
super().__init__(vehicle, "vehicle_state_rt")
|
|
|
|
self.scoped = Scope.VEHICLE_CMDS in scopes
|
|
|
|
if not self.scoped:
|
|
|
|
self._attr_supported_features = CoverEntityFeature(0)
|
|
|
|
self._attr_is_closed = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""When entity is added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
|
|
self.vehicle.stream_vehicle.listen_TrunkRear(self._async_value_from_stream)
|
|
|
|
)
|
|
|
|
|
|
|
|
def _async_value_from_stream(self, value: bool | None) -> None:
|
|
|
|
"""Update the entity attributes."""
|
|
|
|
|
|
|
|
self._attr_is_closed = None if value is None else not value
|
|
|
|
|
|
|
|
|
2024-07-09 06:36:34 +00:00
|
|
|
class TeslemetrySunroofEntity(TeslemetryVehicleEntity, CoverEntity):
|
|
|
|
"""Cover entity for the sunroof."""
|
|
|
|
|
|
|
|
_attr_device_class = CoverDeviceClass.WINDOW
|
|
|
|
_attr_supported_features = (
|
|
|
|
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
|
|
|
|
)
|
|
|
|
_attr_entity_registry_enabled_default = False
|
|
|
|
|
|
|
|
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
|
2025-01-17 16:38:03 +00:00
|
|
|
"""Initialize the cover."""
|
2024-07-09 06:36:34 +00:00
|
|
|
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 in (None, "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."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2024-07-09 06:36:34 +00:00
|
|
|
await handle_vehicle_command(self.api.sun_roof_control(SunRoofCommand.VENT))
|
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close sunroof."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2024-07-09 06:36:34 +00:00
|
|
|
await handle_vehicle_command(self.api.sun_roof_control(SunRoofCommand.CLOSE))
|
|
|
|
self._attr_is_closed = True
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close sunroof."""
|
2024-09-27 13:06:09 +00:00
|
|
|
self.raise_for_scope(Scope.VEHICLE_CMDS)
|
2024-07-09 06:36:34 +00:00
|
|
|
await handle_vehicle_command(self.api.sun_roof_control(SunRoofCommand.STOP))
|
|
|
|
self._attr_is_closed = False
|
|
|
|
self.async_write_ha_state()
|