Add 'select' to configure Schlage locks "Auto Lock Time" (#123758)

pull/129070/head
Max R 2024-10-24 03:26:43 -04:00 committed by GitHub
parent c460e1bbbe
commit bdbe9255a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 125 additions and 0 deletions

View File

@ -16,6 +16,7 @@ from .coordinator import SchlageDataUpdateCoordinator
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.LOCK,
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
]

View File

@ -0,0 +1,78 @@
"""Platform for Schlage select integration."""
from __future__ import annotations
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import LockData, SchlageDataUpdateCoordinator
from .entity import SchlageEntity
_DESCRIPTIONS = (
SelectEntityDescription(
key="auto_lock_time",
translation_key="auto_lock_time",
entity_category=EntityCategory.CONFIG,
# valid values are from Schlage UI and validated by pyschlage
options=[
"0",
"15",
"30",
"60",
"120",
"240",
"300",
],
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up selects based on a config entry."""
coordinator: SchlageDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
def _add_new_locks(locks: dict[str, LockData]) -> None:
async_add_entities(
SchlageSelect(
coordinator=coordinator,
description=description,
device_id=device_id,
)
for device_id in locks
for description in _DESCRIPTIONS
)
_add_new_locks(coordinator.data.locks)
coordinator.new_locks_callbacks.append(_add_new_locks)
class SchlageSelect(SchlageEntity, SelectEntity):
"""Schlage select entity."""
def __init__(
self,
coordinator: SchlageDataUpdateCoordinator,
description: SelectEntityDescription,
device_id: str,
) -> None:
"""Initialize a SchlageSelect."""
super().__init__(coordinator, device_id)
self.entity_description = description
self._attr_unique_id = f"{device_id}_{self.entity_description.key}"
@property
def current_option(self) -> str:
"""Return the current option."""
return str(self._lock_data.lock.auto_lock_time)
def select_option(self, option: str) -> None:
"""Set the current option."""
self._lock.set_auto_lock_time(int(option))

View File

@ -31,6 +31,20 @@
"name": "Keypad disabled"
}
},
"select": {
"auto_lock_time": {
"name": "Auto-Lock time",
"state": {
"0": "Disabled",
"15": "15 seconds",
"30": "30 seconds",
"60": "1 minute",
"120": "2 minutes",
"240": "4 minutes",
"300": "5 minutes"
}
}
},
"switch": {
"beeper": {
"name": "Keypress Beep"

View File

@ -91,6 +91,7 @@ def mock_lock_attrs() -> dict[str, Any]:
"is_locked": False,
"is_jammed": False,
"battery_level": 20,
"auto_lock_time": 15,
"firmware_version": "1.0",
"lock_and_leave_enabled": True,
"beeper_enabled": True,

View File

@ -0,0 +1,31 @@
"""Test Schlage select."""
from unittest.mock import Mock
from homeassistant.components.select import (
ATTR_OPTION,
DOMAIN as SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
async def test_select(
hass: HomeAssistant, mock_lock: Mock, mock_added_config_entry: ConfigEntry
) -> None:
"""Test the auto-lock time select entity."""
entity_id = "select.vault_door_auto_lock_time"
select = hass.states.get(entity_id)
assert select is not None
assert select.state == "15"
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "30"},
blocking=True,
)
mock_lock.set_auto_lock_time.assert_called_once_with(30)