mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Add charge cable lock to Tessie (#107212)
* Add cable lock * Translate exception * Use ServiceValidationError
This commit is contained in:
parent
7db8a52c23
commit
6cbf1da76a
@ -53,3 +53,10 @@ class TessieCoverStates(IntEnum):
|
|||||||
|
|
||||||
CLOSED = 0
|
CLOSED = 0
|
||||||
OPEN = 1
|
OPEN = 1
|
||||||
|
|
||||||
|
|
||||||
|
class TessieChargeCableLockStates(StrEnum):
|
||||||
|
"""Tessie Charge Cable Lock states."""
|
||||||
|
|
||||||
|
ENGAGED = "Engaged"
|
||||||
|
DISENGAGED = "Disengaged"
|
||||||
|
@ -3,14 +3,15 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from tessie_api import lock, unlock
|
from tessie_api import lock, open_unlock_charge_port, unlock
|
||||||
|
|
||||||
from homeassistant.components.lock import LockEntity
|
from homeassistant.components.lock import LockEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN, TessieChargeCableLockStates
|
||||||
from .coordinator import TessieStateUpdateCoordinator
|
from .coordinator import TessieStateUpdateCoordinator
|
||||||
from .entity import TessieEntity
|
from .entity import TessieEntity
|
||||||
|
|
||||||
@ -21,11 +22,15 @@ async def async_setup_entry(
|
|||||||
"""Set up the Tessie sensor platform from a config entry."""
|
"""Set up the Tessie sensor platform from a config entry."""
|
||||||
data = hass.data[DOMAIN][entry.entry_id]
|
data = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(TessieLockEntity(vehicle.state_coordinator) for vehicle in data)
|
async_add_entities(
|
||||||
|
klass(vehicle.state_coordinator)
|
||||||
|
for klass in (TessieLockEntity, TessieCableLockEntity)
|
||||||
|
for vehicle in data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TessieLockEntity(TessieEntity, LockEntity):
|
class TessieLockEntity(TessieEntity, LockEntity):
|
||||||
"""Lock entity for current charge."""
|
"""Lock entity for Tessie."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -48,3 +53,32 @@ class TessieLockEntity(TessieEntity, LockEntity):
|
|||||||
"""Set new value."""
|
"""Set new value."""
|
||||||
await self.run(unlock)
|
await self.run(unlock)
|
||||||
self.set((self.key, False))
|
self.set((self.key, False))
|
||||||
|
|
||||||
|
|
||||||
|
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(
|
||||||
|
"Insert cable to lock",
|
||||||
|
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))
|
||||||
|
@ -56,6 +56,9 @@
|
|||||||
"lock": {
|
"lock": {
|
||||||
"vehicle_state_locked": {
|
"vehicle_state_locked": {
|
||||||
"name": "[%key:component::lock::title%]"
|
"name": "[%key:component::lock::title%]"
|
||||||
|
},
|
||||||
|
"charge_state_charge_port_latch": {
|
||||||
|
"name": "Charge cable lock"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"media_player": {
|
"media_player": {
|
||||||
@ -315,5 +318,10 @@
|
|||||||
"name": "[%key:component::update::title%]"
|
"name": "[%key:component::update::title%]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"exceptions": {
|
||||||
|
"no_cable": {
|
||||||
|
"message": "Insert cable to lock"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.lock import (
|
from homeassistant.components.lock import (
|
||||||
DOMAIN as LOCK_DOMAIN,
|
DOMAIN as LOCK_DOMAIN,
|
||||||
SERVICE_LOCK,
|
SERVICE_LOCK,
|
||||||
@ -9,6 +11,7 @@ from homeassistant.components.lock import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
|
||||||
from .common import TEST_VEHICLE_STATE_ONLINE, setup_platform
|
from .common import TEST_VEHICLE_STATE_ONLINE, setup_platform
|
||||||
|
|
||||||
@ -20,7 +23,7 @@ async def test_locks(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
await setup_platform(hass)
|
await setup_platform(hass)
|
||||||
|
|
||||||
assert len(hass.states.async_all("lock")) == 1
|
assert len(hass.states.async_all("lock")) == 2
|
||||||
|
|
||||||
entity_id = "lock.test_lock"
|
entity_id = "lock.test_lock"
|
||||||
|
|
||||||
@ -48,3 +51,25 @@ async def test_locks(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
assert hass.states.get(entity_id).state == STATE_UNLOCKED
|
assert hass.states.get(entity_id).state == STATE_UNLOCKED
|
||||||
mock_run.assert_called_once()
|
mock_run.assert_called_once()
|
||||||
|
|
||||||
|
# Test charge cable lock set value functions
|
||||||
|
entity_id = "lock.test_charge_cable_lock"
|
||||||
|
with pytest.raises(ServiceValidationError):
|
||||||
|
await hass.services.async_call(
|
||||||
|
LOCK_DOMAIN,
|
||||||
|
SERVICE_LOCK,
|
||||||
|
{ATTR_ENTITY_ID: [entity_id]},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tessie.lock.open_unlock_charge_port"
|
||||||
|
) as mock_run:
|
||||||
|
await hass.services.async_call(
|
||||||
|
LOCK_DOMAIN,
|
||||||
|
SERVICE_UNLOCK,
|
||||||
|
{ATTR_ENTITY_ID: [entity_id]},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
assert hass.states.get(entity_id).state == STATE_UNLOCKED
|
||||||
|
mock_run.assert_called_once()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user