diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 66c6566ff0d5..0b07d6c978dd 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -6,7 +6,6 @@ import logging import re from typing import TYPE_CHECKING, Any, final, override -import probatio from propcache.api import cached_property from homeassistant.config_entries import ConfigEntry @@ -23,13 +22,18 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType, StateType -from homeassistant.util.hass_dict import HassKey -from .const import DOMAIN, LockEntityFeature, LockEntityStateAttribute, LockState +from .const import ( + DATA_COMPONENT, + DOMAIN, + LockEntityFeature, + LockEntityStateAttribute, + LockState, +) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[LockEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE @@ -40,10 +44,6 @@ CONF_DEFAULT_CODE = "default_code" MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10) -LOCK_SERVICE_SCHEMA = cv.make_entity_service_schema( - {probatio.Optional(ATTR_CODE): cv.string} -) - PROP_TO_ATTR = { "changed_by": LockEntityStateAttribute.CHANGED_BY, @@ -61,18 +61,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: await component.async_setup(config) - component.async_register_entity_service( - SERVICE_UNLOCK, LOCK_SERVICE_SCHEMA, "async_handle_unlock_service" - ) - component.async_register_entity_service( - SERVICE_LOCK, LOCK_SERVICE_SCHEMA, "async_handle_lock_service" - ) - component.async_register_entity_service( - SERVICE_OPEN, - LOCK_SERVICE_SCHEMA, - "async_handle_open_service", - [LockEntityFeature.OPEN], - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/lock/const.py b/homeassistant/components/lock/const.py index 8e8983258116..fee089854add 100644 --- a/homeassistant/components/lock/const.py +++ b/homeassistant/components/lock/const.py @@ -1,9 +1,17 @@ """Constants for the lock entity platform.""" from enum import IntFlag, StrEnum -from typing import Final +from typing import TYPE_CHECKING, Final + +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from homeassistant.helpers.entity_component import EntityComponent + + from . import LockEntity DOMAIN: Final = "lock" +DATA_COMPONENT: HassKey[EntityComponent[LockEntity]] = HassKey(DOMAIN) class LockEntityStateAttribute(StrEnum): diff --git a/homeassistant/components/lock/services.py b/homeassistant/components/lock/services.py new file mode 100644 index 000000000000..5ebd0bed9578 --- /dev/null +++ b/homeassistant/components/lock/services.py @@ -0,0 +1,32 @@ +"""Services for the Lock integration.""" + +import probatio + +from homeassistant.const import ATTR_CODE, SERVICE_LOCK, SERVICE_OPEN, SERVICE_UNLOCK +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv + +from .const import DATA_COMPONENT, LockEntityFeature + +LOCK_SERVICE_SCHEMA = cv.make_entity_service_schema( + {probatio.Optional(ATTR_CODE): cv.string} +) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the lock services.""" + component = hass.data[DATA_COMPONENT] + + component.async_register_entity_service( + SERVICE_UNLOCK, LOCK_SERVICE_SCHEMA, "async_handle_unlock_service" + ) + component.async_register_entity_service( + SERVICE_LOCK, LOCK_SERVICE_SCHEMA, "async_handle_lock_service" + ) + component.async_register_entity_service( + SERVICE_OPEN, + LOCK_SERVICE_SCHEMA, + "async_handle_open_service", + [LockEntityFeature.OPEN], + )