Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ wget -O - https://hacs.vip/get | DOMAIN=edge_tts REPO_PATH=hasscc/hass-edge-tts

## Config

```yaml
# configuration.yaml
tts:
- platform: edge_tts
language: zh-CN # Default language or voice (Optional)
```
[UI: config - integrations - add integration - Microsoft Edge TTS ]

<img width="500" height="300" alt="add integration" src="https://github.com/user-attachments/assets/24eacc80-fe2c-4b1f-8434-3cea472c503b" />

<img width="500" height="300" alt="config option" src="https://github.com/user-attachments/assets/97e02a2f-6005-460d-ab61-4f31bcca97f5" />


<img width="500" height="300" alt="entity" src="https://github.com/user-attachments/assets/ef8bfcf0-54b1-4225-902f-46cb61aca43f" />


<img width="500" height="300" alt="call service" src="https://github.com/user-attachments/assets/6fc089d1-aa3a-4bc9-86cf-82adeb2b3ed3" />

#### Configure default options:
```yaml
tts:
- platform: edge_tts
service_name: xiaomo_say # service: tts.xiaomo_say
language: zh-CN-XiaoxiaoNeural
volume: +10%
```

#### Supported languages

Expand All @@ -39,9 +36,10 @@ tts:

## Using

- [![Call service: tts.edge_tts_say](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=tts.edge_tts_say)
- [![Call service: tts.speak](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=tts.speak)
- [REST API: /api/tts_get_url](https://www.home-assistant.io/integrations/tts#post-apitts_get_url)


### Options

- [`voice`](https://docs.microsoft.com/zh-CN/azure/cognitive-services/speech-service/speech-synthesis-markup?tabs=csharp#use-multiple-voices)
Expand Down
38 changes: 36 additions & 2 deletions custom_components/edge_tts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
"""The Edge TTS component."""
"""The Edge TTS integration."""
import logging
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv
from homeassistant.components.tts import (
ATTR_MESSAGE,
DOMAIN as TTS_DOMAIN,
ATTR_VOICE
)
from homeassistant.components.media_player import (
DOMAIN as MEDIA_PLAYER_DOMAIN,
SERVICE_PLAY_MEDIA,
ATTR_MEDIA_CONTENT_ID,
ATTR_MEDIA_CONTENT_TYPE,
)
from .const import DOMAIN

DOMAIN = "edge_tts"
_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Edge TTS from a config entry."""
await hass.config_entries.async_forward_entry_setups(entry, [TTS_DOMAIN])

entry.async_on_unload(entry.add_update_listener(options_update_listener))

return True

async def options_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
hass.services.async_remove(DOMAIN, SERVICE_EDGE_SAY)
return await hass.config_entries.async_forward_entry_unload(entry, TTS_DOMAIN)
65 changes: 65 additions & 0 deletions custom_components/edge_tts/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.components.tts import CONF_LANG
from .const import DOMAIN, CONF_VOICE, SUPPORTED_VOICES
from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig, SelectSelectorMode

SUPPORTED_LANGUAGES_LIST = {
**dict(zip(SUPPORTED_VOICES.values(), SUPPORTED_VOICES.keys())),
'zh-CN': 'zh-CN-XiaoxiaoNeural',
}

SUPPORTED_LANGUAGES = list([*SUPPORTED_LANGUAGES_LIST.keys(), *SUPPORTED_VOICES.keys()])

DEFAULT_LANG = 'zh-CN'


class EdgeTTSConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Edge TTS config flow."""

async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user."""
# 检查是否已经有配置条目
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

# 创建一个空的配置条目
return self.async_create_entry(title="Edge TTS", data={})

@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return EdgeTTSOptionsFlowHandler(config_entry)

class EdgeTTSOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle an options flow for Edge TTS."""

def __init__(self, config_entry):
"""Initialize options flow."""
self._config_entry = config_entry

async def async_step_init(self, user_input=None):
"""Manage the options."""
if user_input is not None:
# 更新配置到 hass.data
self.hass.data[DOMAIN] = user_input
return self.async_create_entry(title="Edge TTS Options", data=user_input)

# 默认值从现有配置中读取,如果不存在则使用默认值
default_language = self._config_entry.options.get(CONF_LANG, "zh-CN")

return self.async_show_form(
step_id="init",
data_schema=vol.Schema({
vol.Required(
CONF_LANG,
default=default_language): SelectSelector(
SelectSelectorConfig(
options=SUPPORTED_LANGUAGES,
multiple=False,translation_key=CONF_LANG
)
),
})
)
Loading
Loading