Home Assistant Unofficial Reference 2024.12.1
select.py
Go to the documentation of this file.
1 """Select entities for a pipeline."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Iterable
6 
7 from homeassistant.components.select import SelectEntity, SelectEntityDescription
8 from homeassistant.const import EntityCategory, Platform
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers import collection, entity_registry as er, restore_state
11 
12 from .const import DOMAIN, OPTION_PREFERRED
13 from .pipeline import AssistDevice, PipelineData, PipelineStorageCollection
14 from .vad import VadSensitivity
15 
16 
17 @callback
19  hass: HomeAssistant, domain: str, unique_id_prefix: str
20 ) -> str | None:
21  """Get the chosen pipeline for a domain."""
22  ent_reg = er.async_get(hass)
23  pipeline_entity_id = ent_reg.async_get_entity_id(
24  Platform.SELECT, domain, f"{unique_id_prefix}-pipeline"
25  )
26  if pipeline_entity_id is None:
27  return None
28 
29  state = hass.states.get(pipeline_entity_id)
30  if state is None or state.state == OPTION_PREFERRED:
31  return None
32 
33  pipeline_store: PipelineStorageCollection = hass.data[DOMAIN].pipeline_store
34  return next(
35  (item.id for item in pipeline_store.async_items() if item.name == state.state),
36  None,
37  )
38 
39 
40 @callback
42  hass: HomeAssistant, domain: str, unique_id_prefix: str
43 ) -> VadSensitivity:
44  """Get the chosen vad sensitivity for a domain."""
45  ent_reg = er.async_get(hass)
46  sensitivity_entity_id = ent_reg.async_get_entity_id(
47  Platform.SELECT, domain, f"{unique_id_prefix}-vad_sensitivity"
48  )
49  if sensitivity_entity_id is None:
50  return VadSensitivity.DEFAULT
51 
52  state = hass.states.get(sensitivity_entity_id)
53  if state is None:
54  return VadSensitivity.DEFAULT
55 
56  return VadSensitivity(state.state)
57 
58 
59 class AssistPipelineSelect(SelectEntity, restore_state.RestoreEntity):
60  """Entity to represent a pipeline selector."""
61 
62  entity_description = SelectEntityDescription(
63  key="pipeline",
64  translation_key="pipeline",
65  entity_category=EntityCategory.CONFIG,
66  )
67  _attr_should_poll = False
68  _attr_current_option = OPTION_PREFERRED
69  _attr_options = [OPTION_PREFERRED]
70 
71  def __init__(self, hass: HomeAssistant, domain: str, unique_id_prefix: str) -> None:
72  """Initialize a pipeline selector."""
73  self._domain_domain = domain
74  self._unique_id_prefix_unique_id_prefix = unique_id_prefix
75  self._attr_unique_id_attr_unique_id = f"{unique_id_prefix}-pipeline"
76  self.hasshasshass = hass
77  self._update_options_update_options()
78 
79  async def async_added_to_hass(self) -> None:
80  """When entity is added to Home Assistant."""
81  await super().async_added_to_hass()
82 
83  pipeline_data: PipelineData = self.hasshasshass.data[DOMAIN]
84  pipeline_store = pipeline_data.pipeline_store
85  self.async_on_removeasync_on_remove(
86  pipeline_store.async_add_change_set_listener(self._pipelines_updated_pipelines_updated)
87  )
88 
89  state = await self.async_get_last_state()
90  if state is not None and state.state in self.optionsoptions:
91  self._attr_current_option_attr_current_option = state.state
92 
93  if self.registry_entryregistry_entry and (device_id := self.registry_entryregistry_entry.device_id):
94  pipeline_data.pipeline_devices[device_id] = AssistDevice(
95  self._domain_domain, self._unique_id_prefix_unique_id_prefix
96  )
97 
98  def cleanup() -> None:
99  """Clean up registered device."""
100  pipeline_data.pipeline_devices.pop(device_id)
101 
102  self.async_on_removeasync_on_remove(cleanup)
103 
104  async def async_select_option(self, option: str) -> None:
105  """Select an option."""
106  self._attr_current_option_attr_current_option = option
107  self.async_write_ha_stateasync_write_ha_state()
108 
110  self, change_set: Iterable[collection.CollectionChange]
111  ) -> None:
112  """Handle pipeline update."""
113  self._update_options_update_options()
114  self.async_write_ha_stateasync_write_ha_state()
115 
116  @callback
117  def _update_options(self) -> None:
118  """Handle pipeline update."""
119  pipeline_store: PipelineStorageCollection = self.hasshasshass.data[
120  DOMAIN
121  ].pipeline_store
122  options = [OPTION_PREFERRED]
123  options.extend(sorted(item.name for item in pipeline_store.async_items()))
124  self._attr_options_attr_options_attr_options = options
125 
126  if self._attr_current_option_attr_current_option not in options:
127  self._attr_current_option_attr_current_option = OPTION_PREFERRED
128 
129 
130 class VadSensitivitySelect(SelectEntity, restore_state.RestoreEntity):
131  """Entity to represent VAD sensitivity."""
132 
133  entity_description = SelectEntityDescription(
134  key="vad_sensitivity",
135  translation_key="vad_sensitivity",
136  entity_category=EntityCategory.CONFIG,
137  )
138  _attr_should_poll = False
139  _attr_current_option = VadSensitivity.DEFAULT.value
140  _attr_options = [vs.value for vs in VadSensitivity]
141 
142  def __init__(self, hass: HomeAssistant, unique_id_prefix: str) -> None:
143  """Initialize a pipeline selector."""
144  self._attr_unique_id_attr_unique_id = f"{unique_id_prefix}-vad_sensitivity"
145  self.hasshasshass = hass
146 
147  async def async_added_to_hass(self) -> None:
148  """When entity is added to Home Assistant."""
149  await super().async_added_to_hass()
150 
151  state = await self.async_get_last_state()
152  if state is not None and state.state in self.optionsoptions:
153  self._attr_current_option_attr_current_option = state.state
154 
155  async def async_select_option(self, option: str) -> None:
156  """Select an option."""
157  self._attr_current_option_attr_current_option = option
158  self.async_write_ha_stateasync_write_ha_state()
None _pipelines_updated(self, Iterable[collection.CollectionChange] change_set)
Definition: select.py:111
None __init__(self, HomeAssistant hass, str domain, str unique_id_prefix)
Definition: select.py:71
None __init__(self, HomeAssistant hass, str unique_id_prefix)
Definition: select.py:142
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
VadSensitivity get_vad_sensitivity(HomeAssistant hass, str domain, str unique_id_prefix)
Definition: select.py:43
str|None get_chosen_pipeline(HomeAssistant hass, str domain, str unique_id_prefix)
Definition: select.py:20