mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Add lock platform to Teslemetry (#117344)
* Add lock platform * Tests and fixes * Fix json * Review Feedback * Apply suggestions from code review Co-authored-by: G Johansson <goran.johansson@shiftit.se> * wording * Fix rebase --------- Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
parent
dc47792ff2
commit
3c7857f0f0
@ -28,6 +28,7 @@ from .models import TeslemetryData, TeslemetryEnergyData, TeslemetryVehicleData
|
|||||||
|
|
||||||
PLATFORMS: Final = [
|
PLATFORMS: Final = [
|
||||||
Platform.CLIMATE,
|
Platform.CLIMATE,
|
||||||
|
Platform.LOCK,
|
||||||
Platform.SELECT,
|
Platform.SELECT,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
Platform.SWITCH,
|
Platform.SWITCH,
|
||||||
|
@ -14,6 +14,20 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"lock": {
|
||||||
|
"charge_state_charge_port_latch": {
|
||||||
|
"default": "mdi:ev-plug-tesla"
|
||||||
|
},
|
||||||
|
"vehicle_state_locked": {
|
||||||
|
"state": {
|
||||||
|
"locked": "mdi:car-door-lock",
|
||||||
|
"unlocked": "mdi:car-door-lock-open"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"vehicle_state_speed_limit_mode_active": {
|
||||||
|
"default": "mdi:car-speed-limiter"
|
||||||
|
}
|
||||||
|
},
|
||||||
"select": {
|
"select": {
|
||||||
"climate_state_seat_heater_left": {
|
"climate_state_seat_heater_left": {
|
||||||
"default": "mdi:car-seat-heater",
|
"default": "mdi:car-seat-heater",
|
||||||
|
98
homeassistant/components/teslemetry/lock.py
Normal file
98
homeassistant/components/teslemetry/lock.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
"""Lock platform for Teslemetry integration."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from tesla_fleet_api.const import Scope
|
||||||
|
|
||||||
|
from homeassistant.components.lock import LockEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .entity import TeslemetryVehicleEntity
|
||||||
|
from .models import TeslemetryVehicleData
|
||||||
|
|
||||||
|
ENGAGED = "Engaged"
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Teslemetry lock platform from a config entry."""
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
klass(vehicle, Scope.VEHICLE_CMDS in entry.runtime_data.scopes)
|
||||||
|
for klass in (
|
||||||
|
TeslemetryVehicleLockEntity,
|
||||||
|
TeslemetryCableLockEntity,
|
||||||
|
)
|
||||||
|
for vehicle in entry.runtime_data.vehicles
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TeslemetryVehicleLockEntity(TeslemetryVehicleEntity, LockEntity):
|
||||||
|
"""Lock entity for Teslemetry."""
|
||||||
|
|
||||||
|
def __init__(self, data: TeslemetryVehicleData, scoped: bool) -> None:
|
||||||
|
"""Initialize the lock."""
|
||||||
|
super().__init__(data, "vehicle_state_locked")
|
||||||
|
self.scoped = scoped
|
||||||
|
|
||||||
|
def _async_update_attrs(self) -> None:
|
||||||
|
"""Update entity attributes."""
|
||||||
|
self._attr_is_locked = self._value
|
||||||
|
|
||||||
|
async def async_lock(self, **kwargs: Any) -> None:
|
||||||
|
"""Lock the doors."""
|
||||||
|
self.raise_for_scope()
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await self.handle_command(self.api.door_lock())
|
||||||
|
self._attr_is_locked = True
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_unlock(self, **kwargs: Any) -> None:
|
||||||
|
"""Unlock the doors."""
|
||||||
|
self.raise_for_scope()
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await self.handle_command(self.api.door_unlock())
|
||||||
|
self._attr_is_locked = False
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
|
||||||
|
class TeslemetryCableLockEntity(TeslemetryVehicleEntity, LockEntity):
|
||||||
|
"""Cable Lock entity for Teslemetry."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
data: TeslemetryVehicleData,
|
||||||
|
scoped: bool,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the lock."""
|
||||||
|
super().__init__(data, "charge_state_charge_port_latch")
|
||||||
|
self.scoped = scoped
|
||||||
|
|
||||||
|
def _async_update_attrs(self) -> None:
|
||||||
|
"""Update entity attributes."""
|
||||||
|
if self._value is None:
|
||||||
|
self._attr_is_locked = None
|
||||||
|
self._attr_is_locked = self._value == 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."""
|
||||||
|
self.raise_for_scope()
|
||||||
|
await self.wake_up_if_asleep()
|
||||||
|
await self.handle_command(self.api.charge_port_door_open())
|
||||||
|
self._attr_is_locked = False
|
||||||
|
self.async_write_ha_state()
|
@ -31,6 +31,17 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"lock": {
|
||||||
|
"charge_state_charge_port_latch": {
|
||||||
|
"name": "Charge cable lock"
|
||||||
|
},
|
||||||
|
"vehicle_state_locked": {
|
||||||
|
"name": "[%key:component::lock::title%]"
|
||||||
|
},
|
||||||
|
"vehicle_state_speed_limit_mode_active": {
|
||||||
|
"name": "Speed limit"
|
||||||
|
}
|
||||||
|
},
|
||||||
"select": {
|
"select": {
|
||||||
"climate_state_seat_heater_left": {
|
"climate_state_seat_heater_left": {
|
||||||
"name": "Seat heater front left",
|
"name": "Seat heater front left",
|
||||||
@ -303,5 +314,10 @@
|
|||||||
"name": "Valet mode"
|
"name": "Valet mode"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"exceptions": {
|
||||||
|
"no_cable": {
|
||||||
|
"message": "Charge cable will lock automatically when connected"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
95
tests/components/teslemetry/snapshots/test_lock.ambr
Normal file
95
tests/components/teslemetry/snapshots/test_lock.ambr
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_lock[lock.test_charge_cable_lock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'lock',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'lock.test_charge_cable_lock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Charge cable lock',
|
||||||
|
'platform': 'teslemetry',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'charge_state_charge_port_latch',
|
||||||
|
'unique_id': 'VINVINVIN-charge_state_charge_port_latch',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_lock[lock.test_charge_cable_lock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Test Charge cable lock',
|
||||||
|
'supported_features': <LockEntityFeature: 0>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'lock.test_charge_cable_lock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'locked',
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_lock[lock.test_lock-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'lock',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'lock.test_lock',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': None,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Lock',
|
||||||
|
'platform': 'teslemetry',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'vehicle_state_locked',
|
||||||
|
'unique_id': 'VINVINVIN-vehicle_state_locked',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_lock[lock.test_lock-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'friendly_name': 'Test Lock',
|
||||||
|
'supported_features': <LockEntityFeature: 0>,
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'lock.test_lock',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'unlocked',
|
||||||
|
})
|
||||||
|
# ---
|
111
tests/components/teslemetry/test_lock.py
Normal file
111
tests/components/teslemetry/test_lock.py
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
"""Test the Teslemetry lock platform."""
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from syrupy import SnapshotAssertion
|
||||||
|
from tesla_fleet_api.exceptions import VehicleOffline
|
||||||
|
|
||||||
|
from homeassistant.components.lock import (
|
||||||
|
DOMAIN as LOCK_DOMAIN,
|
||||||
|
SERVICE_LOCK,
|
||||||
|
SERVICE_UNLOCK,
|
||||||
|
)
|
||||||
|
from homeassistant.const import (
|
||||||
|
ATTR_ENTITY_ID,
|
||||||
|
STATE_LOCKED,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
STATE_UNLOCKED,
|
||||||
|
Platform,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from . import assert_entities, setup_platform
|
||||||
|
from .const import COMMAND_OK
|
||||||
|
|
||||||
|
|
||||||
|
async def test_lock(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the lock entities are correct."""
|
||||||
|
|
||||||
|
entry = await setup_platform(hass, [Platform.LOCK])
|
||||||
|
assert_entities(hass, entry.entry_id, entity_registry, snapshot)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_lock_offline(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_vehicle_data,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the lock entities are correct when offline."""
|
||||||
|
|
||||||
|
mock_vehicle_data.side_effect = VehicleOffline
|
||||||
|
await setup_platform(hass, [Platform.LOCK])
|
||||||
|
state = hass.states.get("lock.test_lock")
|
||||||
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
|
async def test_lock_services(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
) -> None:
|
||||||
|
"""Tests that the lock services work."""
|
||||||
|
|
||||||
|
await setup_platform(hass, [Platform.LOCK])
|
||||||
|
|
||||||
|
entity_id = "lock.test_lock"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.teslemetry.VehicleSpecific.door_lock",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
LOCK_DOMAIN,
|
||||||
|
SERVICE_LOCK,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_LOCKED
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.teslemetry.VehicleSpecific.door_unlock",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
LOCK_DOMAIN,
|
||||||
|
SERVICE_UNLOCK,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_UNLOCKED
|
||||||
|
call.assert_called_once()
|
||||||
|
|
||||||
|
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.teslemetry.VehicleSpecific.charge_port_door_open",
|
||||||
|
return_value=COMMAND_OK,
|
||||||
|
) as call:
|
||||||
|
await hass.services.async_call(
|
||||||
|
LOCK_DOMAIN,
|
||||||
|
SERVICE_UNLOCK,
|
||||||
|
{ATTR_ENTITY_ID: entity_id},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state.state == STATE_UNLOCKED
|
||||||
|
call.assert_called_once()
|
Loading…
x
Reference in New Issue
Block a user