Drop hass argument from service extraction helpers (#152738)

This commit is contained in:
epenet
2025-09-22 16:08:07 +02:00
committed by GitHub
parent 4b6dd0eb8f
commit 018d59a892
11 changed files with 33 additions and 32 deletions

View File

@@ -41,7 +41,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
if call.data.get(ATTR_ENTITY_ID) == ENTITY_MATCH_NONE: if call.data.get(ATTR_ENTITY_ID) == ENTITY_MATCH_NONE:
return [] return []
call_ids = await async_extract_entity_ids(hass, call) call_ids = await async_extract_entity_ids(call)
entity_ids = [] entity_ids = []
for entity_id in hass.data[DATA_AMCREST][CAMERAS]: for entity_id in hass.data[DATA_AMCREST][CAMERAS]:
if entity_id not in call_ids: if entity_id not in call_ids:

View File

@@ -31,11 +31,12 @@ SERVICE_SCHEMA_SET_GUEST_WIFI_PW = vol.Schema(
async def _async_set_guest_wifi_password(service_call: ServiceCall) -> None: async def _async_set_guest_wifi_password(service_call: ServiceCall) -> None:
"""Call Fritz set guest wifi password service.""" """Call Fritz set guest wifi password service."""
hass = service_call.hass target_entry_ids = await async_extract_config_entry_ids(service_call)
target_entry_ids = await async_extract_config_entry_ids(hass, service_call)
target_entries: list[FritzConfigEntry] = [ target_entries: list[FritzConfigEntry] = [
loaded_entry 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 loaded_entry.entry_id in target_entry_ids
] ]

View File

@@ -51,7 +51,7 @@ async def _extract_gmail_config_entries(
) -> list[GoogleMailConfigEntry]: ) -> list[GoogleMailConfigEntry]:
return [ return [
entry 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)) if (entry := call.hass.config_entries.async_get_entry(entry_id))
and entry.domain == DOMAIN and entry.domain == DOMAIN
] ]

View File

@@ -339,7 +339,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
reload_entries: set[str] = set() reload_entries: set[str] = set()
if ATTR_ENTRY_ID in call.data: if ATTR_ENTRY_ID in call.data:
reload_entries.add(call.data[ATTR_ENTRY_ID]) 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: if not reload_entries:
raise ValueError("There were no matching config entries to reload") raise ValueError("There were no matching config entries to reload")
await asyncio.gather( await asyncio.gather(

View File

@@ -272,7 +272,7 @@ async def async_setup_platform(
async def delete_service(call: ServiceCall) -> None: async def delete_service(call: ServiceCall) -> None:
"""Delete a dynamically created scene.""" """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: for entity_id in entity_ids:
scene = platform.entities.get(entity_id) scene = platform.entities.get(entity_id)

View File

@@ -58,11 +58,12 @@ _LOGGER = logging.getLogger(__name__)
async def _extract_config_entry(service_call: ServiceCall) -> MieleConfigEntry: async def _extract_config_entry(service_call: ServiceCall) -> MieleConfigEntry:
"""Extract config entry from the service call.""" """Extract config entry from the service call."""
hass = service_call.hass target_entry_ids = await async_extract_config_entry_ids(service_call)
target_entry_ids = await async_extract_config_entry_ids(hass, service_call)
target_entries: list[MieleConfigEntry] = [ target_entries: list[MieleConfigEntry] = [
loaded_entry 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 loaded_entry.entry_id in target_entry_ids
] ]
if not target_entries: if not target_entries:

View File

@@ -89,8 +89,7 @@ SERVICE_GET_STATISTICS_SCHEMA = vol.Schema(
async def _async_handle_purge_service(service: ServiceCall) -> None: async def _async_handle_purge_service(service: ServiceCall) -> None:
"""Handle calls to the purge service.""" """Handle calls to the purge service."""
hass = service.hass instance = service.hass.data[DATA_INSTANCE]
instance = hass.data[DATA_INSTANCE]
kwargs = service.data kwargs = service.data
keep_days = kwargs.get(ATTR_KEEP_DAYS, instance.keep_days) keep_days = kwargs.get(ATTR_KEEP_DAYS, instance.keep_days)
repack = cast(bool, kwargs[ATTR_REPACK]) 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: async def _async_handle_purge_entities_service(service: ServiceCall) -> None:
"""Handle calls to the purge entities service.""" """Handle calls to the purge entities service."""
hass = service.hass entity_ids = await async_extract_entity_ids(service)
entity_ids = await async_extract_entity_ids(hass, service)
domains = service.data.get(ATTR_DOMAINS, []) domains = service.data.get(ATTR_DOMAINS, [])
keep_days = service.data.get(ATTR_KEEP_DAYS, 0) keep_days = service.data.get(ATTR_KEEP_DAYS, 0)
entity_globs = service.data.get(ATTR_ENTITY_GLOBS, []) entity_globs = service.data.get(ATTR_ENTITY_GLOBS, [])
entity_filter = generate_filter(domains, list(entity_ids), [], [], entity_globs) entity_filter = generate_filter(domains, list(entity_ids), [], [], entity_globs)
purge_before = dt_util.utcnow() - timedelta(days=keep_days) 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: async def _async_handle_enable_service(service: ServiceCall) -> None:

View File

@@ -43,7 +43,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
) )
entities = await service.async_extract_entities( entities = await service.async_extract_entities(
hass, platform_entities.values(), service_call platform_entities.values(), service_call
) )
if not entities: if not entities:

View File

@@ -240,7 +240,7 @@ class EntityComponent[_EntityT: entity.Entity = entity.Entity]:
This method must be run in the event loop. This method must be run in the event loop.
""" """
return await service.async_extract_entities( return await service.async_extract_entities(
self.hass, self.entities, service_call, expand_group self.entities, service_call, expand_group
) )
@callback @callback

View File

@@ -1068,7 +1068,7 @@ class EntityPlatform:
This method must be run in the event loop. This method must be run in the event loop.
""" """
return await service.async_extract_entities( return await service.async_extract_entities(
self.hass, self.entities.values(), service_call, expand_group self.entities.values(), service_call, expand_group
) )
@callback @callback

View File

@@ -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( def extract_entity_ids(
hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True service_call: ServiceCall, expand_group: bool = True
) -> set[str]: ) -> set[str]:
"""Extract a list of entity ids from a service call. """Extract a list of entity ids from a service call.
Will convert group entity ids to the entity ids it represents. Will convert group entity ids to the entity ids it represents.
""" """
return asyncio.run_coroutine_threadsafe( 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() ).result()
@bind_hass @deprecated_hass_argument(breaks_in_ha_version="2026.10")
async def async_extract_entities[_EntityT: Entity]( async def async_extract_entities[_EntityT: Entity](
hass: HomeAssistant,
entities: Iterable[_EntityT], entities: Iterable[_EntityT],
service_call: ServiceCall, service_call: ServiceCall,
expand_group: bool = True, expand_group: bool = True,
@@ -410,7 +409,7 @@ async def async_extract_entities[_EntityT: Entity](
selector_data = target_helpers.TargetSelectorData(service_call.data) selector_data = target_helpers.TargetSelectorData(service_call.data)
referenced = target_helpers.async_extract_referenced_entity_ids( 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 combined = referenced.referenced | referenced.indirectly_referenced
@@ -432,9 +431,9 @@ async def async_extract_entities[_EntityT: Entity](
return found return found
@bind_hass @deprecated_hass_argument(breaks_in_ha_version="2026.10")
async def async_extract_entity_ids( async def async_extract_entity_ids(
hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True service_call: ServiceCall, expand_group: bool = True
) -> set[str]: ) -> set[str]:
"""Extract a set of entity ids from a service call. """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) selector_data = target_helpers.TargetSelectorData(service_call.data)
referenced = target_helpers.async_extract_referenced_entity_ids( 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 return referenced.referenced | referenced.indirectly_referenced
@@ -463,17 +462,17 @@ def async_extract_referenced_entity_ids(
return SelectedEntities(**dataclasses.asdict(selected)) return SelectedEntities(**dataclasses.asdict(selected))
@bind_hass @deprecated_hass_argument(breaks_in_ha_version="2026.10")
async def async_extract_config_entry_ids( 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]: ) -> set[str]:
"""Extract referenced config entry ids from a service call.""" """Extract referenced config entry ids from a service call."""
selector_data = target_helpers.TargetSelectorData(service_call.data) selector_data = target_helpers.TargetSelectorData(service_call.data)
referenced = target_helpers.async_extract_referenced_entity_ids( 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) ent_reg = entity_registry.async_get(service_call.hass)
dev_reg = device_registry.async_get(hass) dev_reg = device_registry.async_get(service_call.hass)
config_entry_ids: set[str] = set() config_entry_ids: set[str] = set()
# Some devices may have no entities # Some devices may have no entities