2023-12-22 09:11:48 +00:00
|
|
|
"""Lock platform for Tessie integration."""
|
2024-03-08 15:35:23 +00:00
|
|
|
|
2023-12-22 09:11:48 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
2024-02-03 21:21:19 +00:00
|
|
|
from tessie_api import (
|
|
|
|
disable_speed_limit,
|
|
|
|
enable_speed_limit,
|
|
|
|
lock,
|
|
|
|
open_unlock_charge_port,
|
|
|
|
unlock,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.components.lock import ATTR_CODE, LockEntity
|
2023-12-22 09:11:48 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-01-13 18:03:10 +00:00
|
|
|
from homeassistant.exceptions import ServiceValidationError
|
2023-12-22 09:11:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2024-01-13 18:03:10 +00:00
|
|
|
from .const import DOMAIN, TessieChargeCableLockStates
|
2023-12-25 12:01:13 +00:00
|
|
|
from .coordinator import TessieStateUpdateCoordinator
|
2023-12-22 09:11:48 +00:00
|
|
|
from .entity import TessieEntity
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Tessie sensor platform from a config entry."""
|
2023-12-25 12:01:13 +00:00
|
|
|
data = hass.data[DOMAIN][entry.entry_id]
|
2023-12-22 09:11:48 +00:00
|
|
|
|
2024-01-13 18:03:10 +00:00
|
|
|
async_add_entities(
|
|
|
|
klass(vehicle.state_coordinator)
|
2024-02-03 21:21:19 +00:00
|
|
|
for klass in (TessieLockEntity, TessieCableLockEntity, TessieSpeedLimitEntity)
|
2024-01-13 18:03:10 +00:00
|
|
|
for vehicle in data
|
|
|
|
)
|
2023-12-22 09:11:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TessieLockEntity(TessieEntity, LockEntity):
|
2024-01-13 18:03:10 +00:00
|
|
|
"""Lock entity for Tessie."""
|
2023-12-22 09:11:48 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-12-25 12:01:13 +00:00
|
|
|
coordinator: TessieStateUpdateCoordinator,
|
2023-12-22 09:11:48 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator, "vehicle_state_locked")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool | None:
|
|
|
|
"""Return the state of the Lock."""
|
|
|
|
return self._value
|
|
|
|
|
|
|
|
async def async_lock(self, **kwargs: Any) -> None:
|
|
|
|
"""Set new value."""
|
|
|
|
await self.run(lock)
|
|
|
|
self.set((self.key, True))
|
|
|
|
|
|
|
|
async def async_unlock(self, **kwargs: Any) -> None:
|
|
|
|
"""Set new value."""
|
|
|
|
await self.run(unlock)
|
|
|
|
self.set((self.key, False))
|
2024-01-13 18:03:10 +00:00
|
|
|
|
|
|
|
|
2024-02-03 21:21:19 +00:00
|
|
|
class TessieSpeedLimitEntity(TessieEntity, LockEntity):
|
|
|
|
"""Speed Limit with PIN entity for Tessie."""
|
|
|
|
|
|
|
|
_attr_code_format = r"^\d\d\d\d$"
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: TessieStateUpdateCoordinator,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator, "vehicle_state_speed_limit_mode_active")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool | None:
|
|
|
|
"""Return the state of the Lock."""
|
|
|
|
return self._value
|
|
|
|
|
|
|
|
async def async_lock(self, **kwargs: Any) -> None:
|
|
|
|
"""Enable speed limit with pin."""
|
|
|
|
code: str | None = kwargs.get(ATTR_CODE)
|
|
|
|
if code:
|
|
|
|
await self.run(enable_speed_limit, pin=code)
|
|
|
|
self.set((self.key, True))
|
|
|
|
|
|
|
|
async def async_unlock(self, **kwargs: Any) -> None:
|
|
|
|
"""Disable speed limit with pin."""
|
|
|
|
code: str | None = kwargs.get(ATTR_CODE)
|
|
|
|
if code:
|
|
|
|
await self.run(disable_speed_limit, pin=code)
|
|
|
|
self.set((self.key, False))
|
|
|
|
|
|
|
|
|
2024-01-13 18:03:10 +00:00
|
|
|
class TessieCableLockEntity(TessieEntity, LockEntity):
|
|
|
|
"""Cable Lock entity for Tessie."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: TessieStateUpdateCoordinator,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(coordinator, "charge_state_charge_port_latch")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_locked(self) -> bool | None:
|
|
|
|
"""Return the state of the Lock."""
|
|
|
|
return self._value == TessieChargeCableLockStates.ENGAGED
|
|
|
|
|
|
|
|
async def async_lock(self, **kwargs: Any) -> None:
|
|
|
|
"""Charge cable Lock cannot be manually locked."""
|
|
|
|
raise ServiceValidationError(
|
|
|
|
translation_domain=DOMAIN,
|
|
|
|
translation_key="no_cable",
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_unlock(self, **kwargs: Any) -> None:
|
|
|
|
"""Unlock charge cable lock."""
|
|
|
|
await self.run(open_unlock_charge_port)
|
|
|
|
self.set((self.key, TessieChargeCableLockStates.DISENGAGED))
|