1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-18 06:20:17 +01:00
Files
core/homeassistant/components/switchbot_cloud/lock.py
T
epenet 39a2c08d4e Use runtime_data in switchbot_cloud integration (#167879)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 10:59:20 +02:00

67 lines
2.2 KiB
Python

"""Support for the Switchbot lock."""
from typing import Any
from switchbot_api import Device, LockCommands, LockV2Commands, Remote, SwitchBotAPI
from homeassistant.components.lock import LockEntity, LockEntityFeature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import SwitchbotCloudConfigEntry, SwitchBotCoordinator
from .entity import SwitchBotCloudEntity
async def async_setup_entry(
hass: HomeAssistant,
config: SwitchbotCloudConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up SwitchBot Cloud entry."""
data = config.runtime_data
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
def __init__(
self,
api: SwitchBotAPI,
device: Device | Remote,
coordinator: SwitchBotCoordinator,
) -> None:
"""Init devices."""
super().__init__(api, device, coordinator)
self.__model = device.device_type
def _set_attributes(self) -> None:
"""Set attributes from coordinator data."""
if coord_data := self.coordinator.data:
self._attr_is_locked = coord_data["lockState"] == "locked"
if self.__model != "Smart Lock Lite":
self._attr_supported_features = LockEntityFeature.OPEN
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()
async def async_open(self, **kwargs: Any) -> None:
"""Latch open the lock."""
await self.send_api_command(LockV2Commands.DEADBOLT)
self._attr_is_locked = False
self.async_write_ha_state()