Home Assistant Unofficial Reference 2024.12.1
assist_pipeline.py
Go to the documentation of this file.
1 """Handle Cloud assist pipelines."""
2 
3 import asyncio
4 from typing import Any
5 
7  async_create_default_pipeline,
8  async_get_pipelines,
9  async_setup_pipeline_store,
10  async_update_pipeline,
11 )
12 from homeassistant.components.conversation import HOME_ASSISTANT_AGENT
13 from homeassistant.components.stt import DOMAIN as STT_DOMAIN
14 from homeassistant.components.tts import DOMAIN as TTS_DOMAIN
15 from homeassistant.const import Platform
16 from homeassistant.core import HomeAssistant
18 
19 from .const import (
20  DATA_PLATFORMS_SETUP,
21  DOMAIN,
22  STT_ENTITY_UNIQUE_ID,
23  TTS_ENTITY_UNIQUE_ID,
24 )
25 
26 
27 async def async_create_cloud_pipeline(hass: HomeAssistant) -> str | None:
28  """Create a cloud assist pipeline."""
29  # Wait for stt and tts platforms to set up and entities to be added
30  # before creating the pipeline.
31  platforms_setup = hass.data[DATA_PLATFORMS_SETUP]
32  await asyncio.gather(*(event.wait() for event in platforms_setup.values()))
33  # Make sure the pipeline store is loaded, needed because assist_pipeline
34  # is an after dependency of cloud
35  await async_setup_pipeline_store(hass)
36 
37  entity_registry = er.async_get(hass)
38  new_stt_engine_id = entity_registry.async_get_entity_id(
39  STT_DOMAIN, DOMAIN, STT_ENTITY_UNIQUE_ID
40  )
41  new_tts_engine_id = entity_registry.async_get_entity_id(
42  TTS_DOMAIN, DOMAIN, TTS_ENTITY_UNIQUE_ID
43  )
44  if new_stt_engine_id is None or new_tts_engine_id is None:
45  # If there's no cloud stt or tts entity, we can't create a cloud pipeline.
46  return None
47 
48  def cloud_assist_pipeline(hass: HomeAssistant) -> str | None:
49  """Return the ID of a cloud-enabled assist pipeline or None.
50 
51  Check if a cloud pipeline already exists with either
52  legacy or current cloud engine ids.
53  """
54  for pipeline in async_get_pipelines(hass):
55  if (
56  pipeline.conversation_engine == HOME_ASSISTANT_AGENT
57  and pipeline.stt_engine in (DOMAIN, new_stt_engine_id)
58  and pipeline.tts_engine in (DOMAIN, new_tts_engine_id)
59  ):
60  return pipeline.id
61  return None
62 
63  if (cloud_assist_pipeline(hass)) is not None or (
64  cloud_pipeline := await async_create_default_pipeline(
65  hass,
66  stt_engine_id=new_stt_engine_id,
67  tts_engine_id=new_tts_engine_id,
68  pipeline_name="Home Assistant Cloud",
69  )
70  ) is None:
71  return None
72 
73  return cloud_pipeline.id
74 
75 
77  hass: HomeAssistant, platform: Platform, engine_id: str
78 ) -> None:
79  """Migrate the pipeline engines in the cloud assist pipeline."""
80  # Migrate existing pipelines with cloud stt or tts to use new cloud engine id.
81  # Added in 2024.02.0. Can be removed in 2025.02.0.
82 
83  # We need to make sure that both stt and tts are loaded before this migration.
84  # Assist pipeline will call default engine when setting up the store.
85  # Wait for the stt or tts platform loaded event here.
86  if platform == Platform.STT:
87  wait_for_platform = Platform.TTS
88  pipeline_attribute = "stt_engine"
89  elif platform == Platform.TTS:
90  wait_for_platform = Platform.STT
91  pipeline_attribute = "tts_engine"
92  else:
93  raise ValueError(f"Invalid platform {platform}")
94 
95  platforms_setup = hass.data[DATA_PLATFORMS_SETUP]
96  await platforms_setup[wait_for_platform].wait()
97 
98  # Make sure the pipeline store is loaded, needed because assist_pipeline
99  # is an after dependency of cloud
100  await async_setup_pipeline_store(hass)
101 
102  kwargs: dict[str, Any] = {pipeline_attribute: engine_id}
103  pipelines = async_get_pipelines(hass)
104  for pipeline in pipelines:
105  if getattr(pipeline, pipeline_attribute) == DOMAIN:
106  await async_update_pipeline(hass, pipeline, **kwargs)
Pipeline|None async_create_default_pipeline(HomeAssistant hass, str stt_engine_id, str tts_engine_id, str pipeline_name)
Definition: pipeline.py:243
None async_update_pipeline(HomeAssistant hass, Pipeline pipeline, *str|UndefinedType conversation_engine=UNDEFINED, str|UndefinedType conversation_language=UNDEFINED, str|UndefinedType language=UNDEFINED, str|UndefinedType name=UNDEFINED, str|None|UndefinedType stt_engine=UNDEFINED, str|None|UndefinedType stt_language=UNDEFINED, str|None|UndefinedType tts_engine=UNDEFINED, str|None|UndefinedType tts_language=UNDEFINED, str|None|UndefinedType tts_voice=UNDEFINED, str|None|UndefinedType wake_word_entity=UNDEFINED, str|None|UndefinedType wake_word_id=UNDEFINED, bool|UndefinedType prefer_local_intents=UNDEFINED)
Definition: pipeline.py:328
PipelineData async_setup_pipeline_store(HomeAssistant hass)
Definition: pipeline.py:1854
list[Pipeline] async_get_pipelines(HomeAssistant hass)
Definition: pipeline.py:305
str|None async_create_cloud_pipeline(HomeAssistant hass)
None async_migrate_cloud_pipeline_engine(HomeAssistant hass, Platform platform, str engine_id)