From 2fff836bd4b61afefe9ba7a9c54f4f589436046a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 Feb 2023 17:59:28 -0600 Subject: [PATCH] Fix lock services not removing entity fields (#88805) --- homeassistant/components/lock/__init__.py | 7 ++++--- homeassistant/helpers/service.py | 16 +++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 86a63538a68..c68d99bfb22 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -33,6 +33,7 @@ from homeassistant.helpers.config_validation import ( # noqa: F401 ) from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.service import remove_entity_service_fields from homeassistant.helpers.typing import ConfigType, StateType _LOGGER = logging.getLogger(__name__) @@ -92,7 +93,7 @@ async def _async_lock(entity: LockEntity, service_call: ServiceCall) -> None: raise ValueError( f"Code '{code}' for locking {entity.entity_id} doesn't match pattern {entity.code_format}" ) - await entity.async_lock(**service_call.data) + await entity.async_lock(**remove_entity_service_fields(service_call)) async def _async_unlock(entity: LockEntity, service_call: ServiceCall) -> None: @@ -102,7 +103,7 @@ async def _async_unlock(entity: LockEntity, service_call: ServiceCall) -> None: raise ValueError( f"Code '{code}' for unlocking {entity.entity_id} doesn't match pattern {entity.code_format}" ) - await entity.async_unlock(**service_call.data) + await entity.async_unlock(**remove_entity_service_fields(service_call)) async def _async_open(entity: LockEntity, service_call: ServiceCall) -> None: @@ -112,7 +113,7 @@ async def _async_open(entity: LockEntity, service_call: ServiceCall) -> None: raise ValueError( f"Code '{code}' for opening {entity.entity_id} doesn't match pattern {entity.code_format}" ) - await entity.async_open(**service_call.data) + await entity.async_open(**remove_entity_service_fields(service_call)) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 3c3da10db7c..9f6f65f1d2d 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -513,6 +513,16 @@ async def async_get_all_descriptions( return descriptions +@callback +def remove_entity_service_fields(call: ServiceCall) -> dict[Any, Any]: + """Remove entity service fields.""" + return { + key: val + for key, val in call.data.items() + if key not in cv.ENTITY_SERVICE_FIELDS + } + + @callback @bind_hass def async_set_service_schema( @@ -567,11 +577,7 @@ async def entity_service_call( # noqa: C901 # If the service function is a string, we'll pass it the service call data if isinstance(func, str): - data: dict | ServiceCall = { - key: val - for key, val in call.data.items() - if key not in cv.ENTITY_SERVICE_FIELDS - } + data: dict | ServiceCall = remove_entity_service_fields(call) # If the service function is not a string, we pass the service call else: data = call