diff --git a/homeassistant/components/tessie/__init__.py b/homeassistant/components/tessie/__init__.py index 608be7692dc..ece8a3b7f4a 100644 --- a/homeassistant/components/tessie/__init__.py +++ b/homeassistant/components/tessie/__init__.py @@ -19,6 +19,7 @@ PLATFORMS = [ Platform.BUTTON, Platform.CLIMATE, Platform.DEVICE_TRACKER, + Platform.LOCK, Platform.SELECT, Platform.SENSOR, Platform.SWITCH, diff --git a/homeassistant/components/tessie/lock.py b/homeassistant/components/tessie/lock.py new file mode 100644 index 00000000000..3342747a2f9 --- /dev/null +++ b/homeassistant/components/tessie/lock.py @@ -0,0 +1,52 @@ +"""Lock platform for Tessie integration.""" +from __future__ import annotations + +from typing import Any + +from tessie_api import lock, unlock + +from homeassistant.components.lock import LockEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN +from .coordinator import TessieDataUpdateCoordinator +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.""" + coordinators = hass.data[DOMAIN][entry.entry_id] + + async_add_entities(TessieLockEntity(coordinator) for coordinator in coordinators) + + +class TessieLockEntity(TessieEntity, LockEntity): + """Lock entity for current charge.""" + + _attr_name = None + + def __init__( + self, + coordinator: TessieDataUpdateCoordinator, + ) -> 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)) diff --git a/tests/components/tessie/test_lock.py b/tests/components/tessie/test_lock.py new file mode 100644 index 00000000000..38601de52c9 --- /dev/null +++ b/tests/components/tessie/test_lock.py @@ -0,0 +1,50 @@ +"""Test the Tessie lock platform.""" + +from unittest.mock import patch + +from homeassistant.components.lock import ( + DOMAIN as LOCK_DOMAIN, + SERVICE_LOCK, + SERVICE_UNLOCK, +) +from homeassistant.const import ATTR_ENTITY_ID, STATE_LOCKED, STATE_UNLOCKED +from homeassistant.core import HomeAssistant + +from .common import TEST_VEHICLE_STATE_ONLINE, setup_platform + + +async def test_locks(hass: HomeAssistant) -> None: + """Tests that the lock entity is correct.""" + + assert len(hass.states.async_all("lock")) == 0 + + await setup_platform(hass) + + assert len(hass.states.async_all("lock")) == 1 + + entity_id = "lock.test" + + assert ( + hass.states.get(entity_id).state == STATE_LOCKED + ) == TEST_VEHICLE_STATE_ONLINE["vehicle_state"]["locked"] + + # Test lock set value functions + with patch("homeassistant.components.tessie.lock.lock") as mock_run: + await hass.services.async_call( + LOCK_DOMAIN, + SERVICE_LOCK, + {ATTR_ENTITY_ID: [entity_id]}, + blocking=True, + ) + assert hass.states.get(entity_id).state == STATE_LOCKED + mock_run.assert_called_once() + + with patch("homeassistant.components.tessie.lock.unlock") 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()