From 07c7081adec21fcaa14038acabe8227ca25dc53f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 27 May 2022 10:30:40 -0700 Subject: [PATCH] Revert "Add service entity context (#71558)" (#72610) --- homeassistant/helpers/service.py | 11 ----------- tests/helpers/test_service.py | 16 +--------------- 2 files changed, 1 insertion(+), 26 deletions(-) diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 9a1e6caa27e..bc3451c24c0 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio from collections.abc import Awaitable, Callable, Iterable -from contextvars import ContextVar import dataclasses from functools import partial, wraps import logging @@ -64,15 +63,6 @@ _LOGGER = logging.getLogger(__name__) SERVICE_DESCRIPTION_CACHE = "service_description_cache" -_current_entity: ContextVar[str | None] = ContextVar("current_entity", default=None) - - -@callback -def async_get_current_entity() -> str | None: - """Get the current entity on which the service is called.""" - return _current_entity.get() - - class ServiceParams(TypedDict): """Type for service call parameters.""" @@ -716,7 +706,6 @@ async def _handle_entity_call( ) -> None: """Handle calling service method.""" entity.async_set_context(context) - _current_entity.set(entity.entity_id) if isinstance(func, str): result = hass.async_run_job(partial(getattr(entity, func), **data)) # type: ignore[arg-type] diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 76cf83e31bf..d08477dc917 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -19,12 +19,12 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.helpers import ( - config_validation as cv, device_registry as dev_reg, entity_registry as ent_reg, service, template, ) +import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import EntityCategory from homeassistant.setup import async_setup_component @@ -1206,17 +1206,3 @@ async def test_async_extract_config_entry_ids(hass): ) assert await service.async_extract_config_entry_ids(hass, call) == {"abc"} - - -async def test_current_entity_context(hass, mock_entities): - """Test we set the current entity context var.""" - - async def mock_service(entity, call): - assert entity.entity_id == service.async_get_current_entity() - - await service.entity_service_call( - hass, - [Mock(entities=mock_entities)], - mock_service, - ha.ServiceCall("test_domain", "test_service", {"entity_id": "light.kitchen"}), - )