Add lock to switchbot_cloud (#115128)
Co-authored-by: Ravaka Razafimanantsoa <3774520+SeraphicRav@users.noreply.github.com> Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
parent
fee1bde231
commit
2da0a91a36
4 changed files with 123 additions and 0 deletions
|
@ -17,6 +17,7 @@ from .coordinator import SwitchBotCoordinator
|
|||
_LOGGER = getLogger(__name__)
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.CLIMATE,
|
||||
Platform.LOCK,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
Platform.VACUUM,
|
||||
|
@ -31,6 +32,7 @@ class SwitchbotDevices:
|
|||
switches: list[Device | Remote] = field(default_factory=list)
|
||||
sensors: list[Device] = field(default_factory=list)
|
||||
vacuums: list[Device] = field(default_factory=list)
|
||||
locks: list[Device] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -97,6 +99,10 @@ def make_device_data(
|
|||
prepare_device(hass, api, device, coordinators_by_id)
|
||||
)
|
||||
|
||||
if isinstance(device, Device) and device.device_type.startswith("Smart Lock"):
|
||||
devices_data.locks.append(
|
||||
prepare_device(hass, api, device, coordinators_by_id)
|
||||
)
|
||||
return devices_data
|
||||
|
||||
|
||||
|
|
53
homeassistant/components/switchbot_cloud/lock.py
Normal file
53
homeassistant/components/switchbot_cloud/lock.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
"""Support for the Switchbot lock."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from switchbot_api import LockCommands
|
||||
|
||||
from homeassistant.components.lock import LockEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import SwitchbotCloudData
|
||||
from .const import DOMAIN
|
||||
from .entity import SwitchBotCloudEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up SwitchBot Cloud entry."""
|
||||
data: SwitchbotCloudData = hass.data[DOMAIN][config.entry_id]
|
||||
async_add_entities(
|
||||
SwitchBotCloudLock(data.api, device, coordinator)
|
||||
for device, coordinator in data.devices.locks
|
||||
)
|
||||
|
||||
|
||||
class SwitchBotCloudLock(SwitchBotCloudEntity, LockEntity):
|
||||
"""Representation of a SwitchBot lock."""
|
||||
|
||||
_attr_name = None
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
if coord_data := self.coordinator.data:
|
||||
self._attr_is_locked = coord_data["lockState"] == "locked"
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_lock(self, **kwargs: Any) -> None:
|
||||
"""Lock the lock."""
|
||||
await self.send_api_command(LockCommands.LOCK)
|
||||
self._attr_is_locked = True
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def async_unlock(self, **kwargs: Any) -> None:
|
||||
"""Unlock the lock."""
|
||||
|
||||
await self.send_api_command(LockCommands.UNLOCK)
|
||||
self._attr_is_locked = False
|
||||
self.async_write_ha_state()
|
|
@ -5,6 +5,8 @@ from unittest.mock import AsyncMock, patch
|
|||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.switchbot_cloud import SwitchBotAPI
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
|
@ -14,3 +16,17 @@ def mock_setup_entry() -> Generator[AsyncMock]:
|
|||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_list_devices():
|
||||
"""Mock list_devices."""
|
||||
with patch.object(SwitchBotAPI, "list_devices") as mock_list_devices:
|
||||
yield mock_list_devices
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_get_status():
|
||||
"""Mock get_status."""
|
||||
with patch.object(SwitchBotAPI, "get_status") as mock_get_status:
|
||||
yield mock_get_status
|
||||
|
|
48
tests/components/switchbot_cloud/test_lock.py
Normal file
48
tests/components/switchbot_cloud/test_lock.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
"""Test for the switchbot_cloud lock."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from switchbot_api import Device
|
||||
|
||||
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN, LockState
|
||||
from homeassistant.components.switchbot_cloud import SwitchBotAPI
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_LOCK, SERVICE_UNLOCK
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import configure_integration
|
||||
|
||||
|
||||
async def test_lock(hass: HomeAssistant, mock_list_devices, mock_get_status) -> None:
|
||||
"""Test locking and unlocking."""
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
deviceId="lock-id-1",
|
||||
deviceName="lock-1",
|
||||
deviceType="Smart Lock",
|
||||
hubDeviceId="test-hub-id",
|
||||
),
|
||||
]
|
||||
|
||||
mock_get_status.return_value = {"lockState": "locked"}
|
||||
|
||||
entry = configure_integration(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
lock_id = "lock.lock_1"
|
||||
assert hass.states.get(lock_id).state == LockState.LOCKED
|
||||
|
||||
with patch.object(SwitchBotAPI, "send_command"):
|
||||
await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_UNLOCK, {ATTR_ENTITY_ID: lock_id}, blocking=True
|
||||
)
|
||||
assert hass.states.get(lock_id).state == LockState.UNLOCKED
|
||||
|
||||
with patch.object(SwitchBotAPI, "send_command"):
|
||||
await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_LOCK, {ATTR_ENTITY_ID: lock_id}, blocking=True
|
||||
)
|
||||
assert hass.states.get(lock_id).state == LockState.LOCKED
|
Loading…
Add table
Reference in a new issue