Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Erik
7cf9e7294b Make remove_entity_service_fields accept service call data 2023-09-14 16:09:04 +02:00
3 changed files with 9 additions and 14 deletions

View file

@ -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

View file

@ -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:

View file

@ -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