diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index f7f0150bdd2..5e7d5b0f8f9 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -29,6 +29,7 @@ from homeassistant.helpers.config_validation import ( # noqa: F401 ) from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription from homeassistant.helpers.entity_component import EntityComponent +from homeassistant.helpers.service import remove_entity_service_fields from homeassistant.helpers.typing import ConfigType from homeassistant.loader import bind_hass import homeassistant.util.color as color_util @@ -397,11 +398,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: def preprocess_data(data: dict[str, Any]) -> dict[str | vol.Optional, Any]: """Preprocess the service data.""" - base: dict[str | vol.Optional, Any] = { - entity_field: data.pop(entity_field) - for entity_field in cv.ENTITY_SERVICE_FIELDS - if entity_field in data - } + base: dict[str | vol.Optional, Any] = remove_entity_service_fields(data) preprocess_turn_on_alternatives(hass, data) base["params"] = data diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 8cbce69dc7c..3b8617e357e 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -96,7 +96,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(**remove_entity_service_fields(service_call)) + await entity.async_lock(**remove_entity_service_fields(service_call.data)) async def _async_unlock(entity: LockEntity, service_call: ServiceCall) -> None: @@ -108,7 +108,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(**remove_entity_service_fields(service_call)) + await entity.async_unlock(**remove_entity_service_fields(service_call.data)) async def _async_open(entity: LockEntity, service_call: ServiceCall) -> None: @@ -120,7 +120,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(**remove_entity_service_fields(service_call)) + await entity.async_open(**remove_entity_service_fields(service_call.data)) async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 4532e1a00ae..244e2f73ad7 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -2,7 +2,7 @@ from __future__ import annotations import asyncio -from collections.abc import Awaitable, Callable, Coroutine, Iterable +from collections.abc import Awaitable, Callable, Coroutine, Iterable, Mapping import dataclasses from enum import Enum from functools import cache, partial, wraps @@ -690,12 +690,10 @@ async def async_get_all_descriptions( @callback -def remove_entity_service_fields(call: ServiceCall) -> dict[Any, Any]: +def remove_entity_service_fields(data: Mapping[Any, Any]) -> 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 + key: val for key, val in data.items() if key not in cv.ENTITY_SERVICE_FIELDS } @@ -817,7 +815,7 @@ async def entity_service_call( # If the service function is a string, we'll pass it the service call data if isinstance(func, str): - data: dict | ServiceCall = remove_entity_service_fields(call) + data: dict | ServiceCall = remove_entity_service_fields(call.data) # If the service function is not a string, we pass the service call else: data = call