forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow picking a pipeline for voip devices (home-assistant#91524)
* Allow picking a pipeline for voip device * Add tests * Fix test * Adjust on new pipeline data
- Loading branch information
Showing
12 changed files
with
323 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
"""Select entities for a pipeline.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Iterable | ||
|
||
from homeassistant.components.select import SelectEntity, SelectEntityDescription | ||
from homeassistant.const import EntityCategory, Platform | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers import collection, entity_registry as er, restore_state | ||
|
||
from .const import DOMAIN | ||
from .pipeline import PipelineStorageCollection | ||
|
||
OPTION_PREFERRED = "preferred" | ||
|
||
|
||
@callback | ||
def get_chosen_pipeline( | ||
hass: HomeAssistant, domain: str, unique_id_prefix: str | ||
) -> str | None: | ||
"""Get the chosen pipeline for a domain.""" | ||
ent_reg = er.async_get(hass) | ||
pipeline_entity_id = ent_reg.async_get_entity_id( | ||
Platform.SELECT, domain, f"{unique_id_prefix}-pipeline" | ||
) | ||
if pipeline_entity_id is None: | ||
return None | ||
|
||
state = hass.states.get(pipeline_entity_id) | ||
if state is None or state.state == OPTION_PREFERRED: | ||
return None | ||
|
||
pipeline_store: PipelineStorageCollection = hass.data[DOMAIN] | ||
return next( | ||
(item.id for item in pipeline_store.async_items() if item.name == state.state), | ||
None, | ||
) | ||
|
||
|
||
class AssistPipelineSelect(SelectEntity, restore_state.RestoreEntity): | ||
"""Entity to represent a pipeline selector.""" | ||
|
||
entity_description = SelectEntityDescription( | ||
key="pipeline", | ||
translation_key="pipeline", | ||
entity_category=EntityCategory.CONFIG, | ||
) | ||
_attr_should_poll = False | ||
_attr_current_option = OPTION_PREFERRED | ||
_attr_options = [OPTION_PREFERRED] | ||
|
||
def __init__(self, hass: HomeAssistant, unique_id_prefix: str) -> None: | ||
"""Initialize a pipeline selector.""" | ||
self._attr_unique_id = f"{unique_id_prefix}-pipeline" | ||
self.hass = hass | ||
self._update_options() | ||
|
||
async def async_added_to_hass(self) -> None: | ||
"""When entity is added to Home Assistant.""" | ||
await super().async_added_to_hass() | ||
|
||
pipeline_store: PipelineStorageCollection = self.hass.data[ | ||
DOMAIN | ||
].pipeline_store | ||
pipeline_store.async_add_change_set_listener(self._pipelines_updated) | ||
|
||
state = await self.async_get_last_state() | ||
if state is not None and state.state in self.options: | ||
self._attr_current_option = state.state | ||
|
||
async def async_select_option(self, option: str) -> None: | ||
"""Select an option.""" | ||
self._attr_current_option = option | ||
self.async_write_ha_state() | ||
|
||
async def _pipelines_updated( | ||
self, change_sets: Iterable[collection.CollectionChangeSet] | ||
) -> None: | ||
"""Handle pipeline update.""" | ||
self._update_options() | ||
self.async_write_ha_state() | ||
|
||
@callback | ||
def _update_options(self) -> None: | ||
"""Handle pipeline update.""" | ||
pipeline_store: PipelineStorageCollection = self.hass.data[ | ||
DOMAIN | ||
].pipeline_store | ||
options = [OPTION_PREFERRED] | ||
options.extend(sorted(item.name for item in pipeline_store.async_items())) | ||
self._attr_options = options | ||
|
||
if self._attr_current_option not in options: | ||
self._attr_current_option = OPTION_PREFERRED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"entity": { | ||
"select": { | ||
"pipeline": { | ||
"name": "Assist Pipeline", | ||
"state": { | ||
"preferred": "Preferred" | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Select entities for VoIP integration.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from homeassistant.components.assist_pipeline.select import AssistPipelineSelect | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN | ||
from .devices import VoIPDevice | ||
from .entity import VoIPEntity | ||
|
||
if TYPE_CHECKING: | ||
from . import DomainData | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up VoIP switch entities.""" | ||
domain_data: DomainData = hass.data[DOMAIN] | ||
|
||
@callback | ||
def async_add_device(device: VoIPDevice) -> None: | ||
"""Add device.""" | ||
async_add_entities([VoipPipelineSelect(hass, device)]) | ||
|
||
domain_data.devices.async_add_new_device_listener(async_add_device) | ||
|
||
async_add_entities( | ||
[VoipPipelineSelect(hass, device) for device in domain_data.devices] | ||
) | ||
|
||
|
||
class VoipPipelineSelect(VoIPEntity, AssistPipelineSelect): | ||
"""Pipeline selector for VoIP devices.""" | ||
|
||
def __init__(self, hass: HomeAssistant, device: VoIPDevice) -> None: | ||
"""Initialize a pipeline selector.""" | ||
VoIPEntity.__init__(self, device) | ||
AssistPipelineSelect.__init__(self, hass, device.voip_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.