mirror of
https://github.com/home-assistant/core.git
synced 2025-11-16 22:40:44 +00:00
Drop hass argument from service extraction helpers (#152738)
This commit is contained in:
@@ -41,7 +41,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
if call.data.get(ATTR_ENTITY_ID) == ENTITY_MATCH_NONE:
|
||||
return []
|
||||
|
||||
call_ids = await async_extract_entity_ids(hass, call)
|
||||
call_ids = await async_extract_entity_ids(call)
|
||||
entity_ids = []
|
||||
for entity_id in hass.data[DATA_AMCREST][CAMERAS]:
|
||||
if entity_id not in call_ids:
|
||||
|
||||
@@ -31,11 +31,12 @@ SERVICE_SCHEMA_SET_GUEST_WIFI_PW = vol.Schema(
|
||||
|
||||
async def _async_set_guest_wifi_password(service_call: ServiceCall) -> None:
|
||||
"""Call Fritz set guest wifi password service."""
|
||||
hass = service_call.hass
|
||||
target_entry_ids = await async_extract_config_entry_ids(hass, service_call)
|
||||
target_entry_ids = await async_extract_config_entry_ids(service_call)
|
||||
target_entries: list[FritzConfigEntry] = [
|
||||
loaded_entry
|
||||
for loaded_entry in hass.config_entries.async_loaded_entries(DOMAIN)
|
||||
for loaded_entry in service_call.hass.config_entries.async_loaded_entries(
|
||||
DOMAIN
|
||||
)
|
||||
if loaded_entry.entry_id in target_entry_ids
|
||||
]
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ async def _extract_gmail_config_entries(
|
||||
) -> list[GoogleMailConfigEntry]:
|
||||
return [
|
||||
entry
|
||||
for entry_id in await async_extract_config_entry_ids(call.hass, call)
|
||||
for entry_id in await async_extract_config_entry_ids(call)
|
||||
if (entry := call.hass.config_entries.async_get_entry(entry_id))
|
||||
and entry.domain == DOMAIN
|
||||
]
|
||||
|
||||
@@ -339,7 +339,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
|
||||
reload_entries: set[str] = set()
|
||||
if ATTR_ENTRY_ID in call.data:
|
||||
reload_entries.add(call.data[ATTR_ENTRY_ID])
|
||||
reload_entries.update(await async_extract_config_entry_ids(hass, call))
|
||||
reload_entries.update(await async_extract_config_entry_ids(call))
|
||||
if not reload_entries:
|
||||
raise ValueError("There were no matching config entries to reload")
|
||||
await asyncio.gather(
|
||||
|
||||
@@ -272,7 +272,7 @@ async def async_setup_platform(
|
||||
|
||||
async def delete_service(call: ServiceCall) -> None:
|
||||
"""Delete a dynamically created scene."""
|
||||
entity_ids = await async_extract_entity_ids(hass, call)
|
||||
entity_ids = await async_extract_entity_ids(call)
|
||||
|
||||
for entity_id in entity_ids:
|
||||
scene = platform.entities.get(entity_id)
|
||||
|
||||
@@ -58,11 +58,12 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def _extract_config_entry(service_call: ServiceCall) -> MieleConfigEntry:
|
||||
"""Extract config entry from the service call."""
|
||||
hass = service_call.hass
|
||||
target_entry_ids = await async_extract_config_entry_ids(hass, service_call)
|
||||
target_entry_ids = await async_extract_config_entry_ids(service_call)
|
||||
target_entries: list[MieleConfigEntry] = [
|
||||
loaded_entry
|
||||
for loaded_entry in hass.config_entries.async_loaded_entries(DOMAIN)
|
||||
for loaded_entry in service_call.hass.config_entries.async_loaded_entries(
|
||||
DOMAIN
|
||||
)
|
||||
if loaded_entry.entry_id in target_entry_ids
|
||||
]
|
||||
if not target_entries:
|
||||
|
||||
@@ -89,8 +89,7 @@ SERVICE_GET_STATISTICS_SCHEMA = vol.Schema(
|
||||
|
||||
async def _async_handle_purge_service(service: ServiceCall) -> None:
|
||||
"""Handle calls to the purge service."""
|
||||
hass = service.hass
|
||||
instance = hass.data[DATA_INSTANCE]
|
||||
instance = service.hass.data[DATA_INSTANCE]
|
||||
kwargs = service.data
|
||||
keep_days = kwargs.get(ATTR_KEEP_DAYS, instance.keep_days)
|
||||
repack = cast(bool, kwargs[ATTR_REPACK])
|
||||
@@ -101,14 +100,15 @@ async def _async_handle_purge_service(service: ServiceCall) -> None:
|
||||
|
||||
async def _async_handle_purge_entities_service(service: ServiceCall) -> None:
|
||||
"""Handle calls to the purge entities service."""
|
||||
hass = service.hass
|
||||
entity_ids = await async_extract_entity_ids(hass, service)
|
||||
entity_ids = await async_extract_entity_ids(service)
|
||||
domains = service.data.get(ATTR_DOMAINS, [])
|
||||
keep_days = service.data.get(ATTR_KEEP_DAYS, 0)
|
||||
entity_globs = service.data.get(ATTR_ENTITY_GLOBS, [])
|
||||
entity_filter = generate_filter(domains, list(entity_ids), [], [], entity_globs)
|
||||
purge_before = dt_util.utcnow() - timedelta(days=keep_days)
|
||||
hass.data[DATA_INSTANCE].queue_task(PurgeEntitiesTask(entity_filter, purge_before))
|
||||
service.hass.data[DATA_INSTANCE].queue_task(
|
||||
PurgeEntitiesTask(entity_filter, purge_before)
|
||||
)
|
||||
|
||||
|
||||
async def _async_handle_enable_service(service: ServiceCall) -> None:
|
||||
|
||||
@@ -43,7 +43,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
entities = await service.async_extract_entities(
|
||||
hass, platform_entities.values(), service_call
|
||||
platform_entities.values(), service_call
|
||||
)
|
||||
|
||||
if not entities:
|
||||
|
||||
@@ -240,7 +240,7 @@ class EntityComponent[_EntityT: entity.Entity = entity.Entity]:
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return await service.async_extract_entities(
|
||||
self.hass, self.entities, service_call, expand_group
|
||||
self.entities, service_call, expand_group
|
||||
)
|
||||
|
||||
@callback
|
||||
|
||||
@@ -1068,7 +1068,7 @@ class EntityPlatform:
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
return await service.async_extract_entities(
|
||||
self.hass, self.entities.values(), service_call, expand_group
|
||||
self.entities.values(), service_call, expand_group
|
||||
)
|
||||
|
||||
@callback
|
||||
|
||||
@@ -379,22 +379,21 @@ def async_prepare_call_from_config(
|
||||
}
|
||||
|
||||
|
||||
@bind_hass
|
||||
@deprecated_hass_argument(breaks_in_ha_version="2026.10")
|
||||
def extract_entity_ids(
|
||||
hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True
|
||||
service_call: ServiceCall, expand_group: bool = True
|
||||
) -> set[str]:
|
||||
"""Extract a list of entity ids from a service call.
|
||||
|
||||
Will convert group entity ids to the entity ids it represents.
|
||||
"""
|
||||
return asyncio.run_coroutine_threadsafe(
|
||||
async_extract_entity_ids(hass, service_call, expand_group), hass.loop
|
||||
async_extract_entity_ids(service_call, expand_group), service_call.hass.loop
|
||||
).result()
|
||||
|
||||
|
||||
@bind_hass
|
||||
@deprecated_hass_argument(breaks_in_ha_version="2026.10")
|
||||
async def async_extract_entities[_EntityT: Entity](
|
||||
hass: HomeAssistant,
|
||||
entities: Iterable[_EntityT],
|
||||
service_call: ServiceCall,
|
||||
expand_group: bool = True,
|
||||
@@ -410,7 +409,7 @@ async def async_extract_entities[_EntityT: Entity](
|
||||
|
||||
selector_data = target_helpers.TargetSelectorData(service_call.data)
|
||||
referenced = target_helpers.async_extract_referenced_entity_ids(
|
||||
hass, selector_data, expand_group
|
||||
service_call.hass, selector_data, expand_group
|
||||
)
|
||||
combined = referenced.referenced | referenced.indirectly_referenced
|
||||
|
||||
@@ -432,9 +431,9 @@ async def async_extract_entities[_EntityT: Entity](
|
||||
return found
|
||||
|
||||
|
||||
@bind_hass
|
||||
@deprecated_hass_argument(breaks_in_ha_version="2026.10")
|
||||
async def async_extract_entity_ids(
|
||||
hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True
|
||||
service_call: ServiceCall, expand_group: bool = True
|
||||
) -> set[str]:
|
||||
"""Extract a set of entity ids from a service call.
|
||||
|
||||
@@ -442,7 +441,7 @@ async def async_extract_entity_ids(
|
||||
"""
|
||||
selector_data = target_helpers.TargetSelectorData(service_call.data)
|
||||
referenced = target_helpers.async_extract_referenced_entity_ids(
|
||||
hass, selector_data, expand_group
|
||||
service_call.hass, selector_data, expand_group
|
||||
)
|
||||
return referenced.referenced | referenced.indirectly_referenced
|
||||
|
||||
@@ -463,17 +462,17 @@ def async_extract_referenced_entity_ids(
|
||||
return SelectedEntities(**dataclasses.asdict(selected))
|
||||
|
||||
|
||||
@bind_hass
|
||||
@deprecated_hass_argument(breaks_in_ha_version="2026.10")
|
||||
async def async_extract_config_entry_ids(
|
||||
hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True
|
||||
service_call: ServiceCall, expand_group: bool = True
|
||||
) -> set[str]:
|
||||
"""Extract referenced config entry ids from a service call."""
|
||||
selector_data = target_helpers.TargetSelectorData(service_call.data)
|
||||
referenced = target_helpers.async_extract_referenced_entity_ids(
|
||||
hass, selector_data, expand_group
|
||||
service_call.hass, selector_data, expand_group
|
||||
)
|
||||
ent_reg = entity_registry.async_get(hass)
|
||||
dev_reg = device_registry.async_get(hass)
|
||||
ent_reg = entity_registry.async_get(service_call.hass)
|
||||
dev_reg = device_registry.async_get(service_call.hass)
|
||||
config_entry_ids: set[str] = set()
|
||||
|
||||
# Some devices may have no entities
|
||||
|
||||
Reference in New Issue
Block a user