mirror of
https://github.com/elisspace/core.git
synced 2026-08-29 15:43:55 +00:00
Add optional name prefix to Wyoming services
This commit is contained in:
@@ -15,7 +15,11 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Load Wyoming."""
|
||||
service = await WyomingService.create(entry.data["host"], entry.data["port"])
|
||||
service = await WyomingService.create(
|
||||
entry.data["host"],
|
||||
entry.data["port"],
|
||||
entry.data.get("name"),
|
||||
)
|
||||
|
||||
if service is None:
|
||||
raise ConfigEntryNotReady("Unable to connect")
|
||||
|
||||
@@ -8,7 +8,7 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.hassio import HassioServiceInfo
|
||||
from homeassistant.const import CONF_HOST, CONF_PORT
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
from .const import DOMAIN
|
||||
@@ -16,6 +16,7 @@ from .data import WyomingService
|
||||
|
||||
STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_NAME): str,
|
||||
vol.Required(CONF_HOST): str,
|
||||
vol.Required(CONF_PORT): int,
|
||||
}
|
||||
@@ -41,6 +42,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
service = await WyomingService.create(
|
||||
user_input[CONF_HOST],
|
||||
user_input[CONF_PORT],
|
||||
user_input.get(CONF_NAME),
|
||||
)
|
||||
|
||||
if service is None:
|
||||
@@ -55,13 +57,18 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
tts_installed = [tts for tts in service.info.tts if tts.installed]
|
||||
|
||||
if asr_installed:
|
||||
name = asr_installed[0].name
|
||||
service_name = asr_installed[0].name
|
||||
elif tts_installed:
|
||||
name = tts_installed[0].name
|
||||
service_name = tts_installed[0].name
|
||||
else:
|
||||
return self.async_abort(reason="no_services")
|
||||
|
||||
return self.async_create_entry(title=name, data=user_input)
|
||||
title = service_name
|
||||
name_prefix = user_input.get(CONF_NAME)
|
||||
if name_prefix:
|
||||
title = f"{name_prefix} {service_name}"
|
||||
|
||||
return self.async_create_entry(title=title, data=user_input)
|
||||
|
||||
async def async_step_hassio(self, discovery_info: HassioServiceInfo) -> FlowResult:
|
||||
"""Handle Supervisor add-on discovery."""
|
||||
@@ -79,7 +86,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
if user_input is not None:
|
||||
uri = urlparse(self._hassio_discovery.config["uri"])
|
||||
if service := await WyomingService.create(uri.hostname, uri.port):
|
||||
if service := await WyomingService.create(
|
||||
uri.hostname,
|
||||
uri.port,
|
||||
None,
|
||||
):
|
||||
if not any(asr for asr in service.info.asr if asr.installed):
|
||||
return self.async_abort(reason="no_services")
|
||||
|
||||
|
||||
@@ -19,11 +19,14 @@ _INFO_RETRIES = 3
|
||||
class WyomingService:
|
||||
"""Hold info for Wyoming service."""
|
||||
|
||||
def __init__(self, host: str, port: int, info: Info) -> None:
|
||||
def __init__(
|
||||
self, host: str, port: int, info: Info, name_prefix: str | None = None
|
||||
) -> None:
|
||||
"""Initialize Wyoming service."""
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.info = info
|
||||
self.name_prefix = name_prefix
|
||||
platforms = []
|
||||
if any(asr.installed for asr in info.asr):
|
||||
platforms.append(Platform.STT)
|
||||
@@ -32,13 +35,15 @@ class WyomingService:
|
||||
self.platforms = platforms
|
||||
|
||||
@classmethod
|
||||
async def create(cls, host: str, port: int) -> WyomingService | None:
|
||||
async def create(
|
||||
cls, host: str, port: int, name_prefix: str | None
|
||||
) -> WyomingService | None:
|
||||
"""Create a Wyoming service."""
|
||||
info = await load_wyoming_info(host, port)
|
||||
if info is None:
|
||||
return None
|
||||
|
||||
return cls(host, port, info)
|
||||
return cls(host, port, info, name_prefix)
|
||||
|
||||
|
||||
async def load_wyoming_info(
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"name": "[%key:common::config_flow::data::name%]",
|
||||
"host": "[%key:common::config_flow::data::host%]",
|
||||
"port": "[%key:common::config_flow::data::port%]"
|
||||
}
|
||||
|
||||
@@ -50,7 +50,12 @@ class WyomingSttProvider(stt.SpeechToTextEntity):
|
||||
model_languages.update(asr_model.languages)
|
||||
|
||||
self._supported_languages = list(model_languages)
|
||||
self._attr_name = asr_service.name
|
||||
|
||||
if service.name_prefix:
|
||||
self._attr_name = f"{service.name_prefix}_{asr_service.name}"
|
||||
else:
|
||||
self._attr_name = asr_service.name
|
||||
|
||||
self._attr_unique_id = f"{config_entry.entry_id}-stt"
|
||||
|
||||
@property
|
||||
|
||||
@@ -44,11 +44,11 @@ class WyomingTtsProvider(tts.TextToSpeechEntity):
|
||||
) -> None:
|
||||
"""Set up provider."""
|
||||
self.service = service
|
||||
self._tts_service = next(tts for tts in service.info.tts if tts.installed)
|
||||
tts_service = next(tts for tts in service.info.tts if tts.installed)
|
||||
|
||||
voice_languages: set[str] = set()
|
||||
self._voices: dict[str, list[tts.Voice]] = defaultdict(list)
|
||||
for voice in self._tts_service.voices:
|
||||
for voice in tts_service.voices:
|
||||
if not voice.installed:
|
||||
continue
|
||||
|
||||
@@ -63,7 +63,11 @@ class WyomingTtsProvider(tts.TextToSpeechEntity):
|
||||
|
||||
self._supported_languages: list[str] = list(voice_languages)
|
||||
|
||||
self._attr_name = self._tts_service.name
|
||||
if service.name_prefix:
|
||||
self._attr_name = f"{service.name_prefix}_{tts_service.name}"
|
||||
else:
|
||||
self._attr_name = tts_service.name
|
||||
|
||||
self._attr_unique_id = f"{config_entry.entry_id}-tts"
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user