Move lock service registration to services module (#182815)

This commit is contained in:
epenet
2026-09-21 18:43:55 +02:00
committed by GitHub
parent ed63699a4b
commit 602d564903
3 changed files with 50 additions and 21 deletions
+9 -20
View File
@@ -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
+9 -1
View File
@@ -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):
+32
View File
@@ -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],
)