diff --git a/README.md b/README.md index 44874d9..099da5d 100644 --- a/README.md +++ b/README.md @@ -320,8 +320,8 @@ func main() { Port: 8090, }) - // Play Text-to-Speech message - err := c.PlayTTS("Welcome home!", "your-app-key", 70) + // Play Text-to-Speech message (language code "EN", "DE", etc.) + err := c.PlayTTS("Welcome home!", "your-app-key", "EN", 70) if err != nil { log.Fatal(err) } diff --git a/cmd/soundtouch-cli/cmd_speaker.go b/cmd/soundtouch-cli/cmd_speaker.go index 8e68568..2f2879c 100644 --- a/cmd/soundtouch-cli/cmd_speaker.go +++ b/cmd/soundtouch-cli/cmd_speaker.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "net/url" "strings" "github.com/gesellix/bose-soundtouch/pkg/models" @@ -35,23 +34,12 @@ func playTTS(c *cli.Context) error { return err } - // URL encode the text for Google TTS - encodedText := url.QueryEscape(text) - - // Build TTS URL with language support - ttsURL := fmt.Sprintf("http://translate.google.com/translate_tts?ie=UTF-8&tl=%s&client=tw-ob&q=%s", language, encodedText) - // Create PlayInfo for TTS - playInfo := &models.PlayInfo{ - URL: ttsURL, - AppKey: appKey, - Service: "TTS Notification", - Message: "Google TTS", - Reason: text, - } - + var playInfo *models.PlayInfo if volume > 0 { - playInfo.SetVolume(volume) + playInfo = models.NewTTSPlayInfo(text, appKey, language, volume) + } else { + playInfo = models.NewTTSPlayInfo(text, appKey, language) } err = client.PlayCustom(playInfo) @@ -121,10 +109,11 @@ func playURL(c *cli.Context) error { } // Create PlayInfo for URL content - playInfo := models.NewURLPlayInfo(urlStr, appKey, service, message, reason) - + var playInfo *models.PlayInfo if volume > 0 { - playInfo.SetVolume(volume) + playInfo = models.NewURLPlayInfo(urlStr, appKey, service, message, reason, volume) + } else { + playInfo = models.NewURLPlayInfo(urlStr, appKey, service, message, reason) } err = client.PlayCustom(playInfo) diff --git a/docs/UNIMPLEMENTED-ENDPOINTS.md b/docs/UNIMPLEMENTED-ENDPOINTS.md index 9be4c1b..00e0bf6 100644 --- a/docs/UNIMPLEMENTED-ENDPOINTS.md +++ b/docs/UNIMPLEMENTED-ENDPOINTS.md @@ -370,7 +370,7 @@ soundtouch-cli speaker beep **Go Client Usage:** ```go // Text-to-Speech -client.PlayTTS("Hello World", "your-app-key", 70) +client.PlayTTS("Hello World", "your-app-key", "EN", 70) // URL content client.PlayURL("https://example.com/audio.mp3", "your-app-key", "Service", "Message", "Reason", 60) @@ -1044,4 +1044,4 @@ The SoundTouch Plus Wiki provides comprehensive documentation for **64 additiona This documentation provides the complete foundation for implementing all endpoints from the SoundTouch Plus Wiki, enabling this Go library to become the definitive SoundTouch integration solution for everything from basic home automation to professional audio installations. -*All examples and XML structures are verified against real SoundTouch hardware and extensively tested by the SoundTouch Plus community.* \ No newline at end of file +*All examples and XML structures are verified against real SoundTouch hardware and extensively tested by the SoundTouch Plus community.* diff --git a/docs/reference/SPEAKER-ENDPOINT.md b/docs/reference/SPEAKER-ENDPOINT.md index 7ccc0c1..4a47551 100644 --- a/docs/reference/SPEAKER-ENDPOINT.md +++ b/docs/reference/SPEAKER-ENDPOINT.md @@ -68,14 +68,14 @@ func main() { client := client.NewClient(config) - // Play TTS at current volume - err := client.PlayTTS("Hello, this is a test message", "YOUR_APP_KEY") + // Play TTS at current volume (language code "EN", "DE", etc.) + err := client.PlayTTS("Hello, this is a test message", "YOUR_APP_KEY", "EN") if err != nil { log.Fatal(err) } // Play TTS at specific volume (70) - err = client.PlayTTS("Volume test message", "YOUR_APP_KEY", 70) + err = client.PlayTTS("Volume test message", "YOUR_APP_KEY", "EN", 70) if err != nil { log.Fatal(err) } @@ -277,7 +277,7 @@ You'll need to provide your own application key. The format and generation metho ```go // Doorbell notification -client.PlayTTS("Someone is at the front door", "home-automation-key", 80) +client.PlayTTS("Someone is at the front door", "home-automation-key", "EN", 80) // Security alert client.PlayURL( @@ -311,4 +311,4 @@ soundtouch-cli speaker url --url "https://www.soundjay.com/misc/sounds/bell-ring 4. **URL content fails**: Ensure URL is accessible and contains valid audio 5. **Volume not restored**: May occur if device is powered off during playback -For more information, see the [SoundTouch WebServices API documentation](https://github.com/thlucas1/homeassistantcomponent_soundtouchplus/wiki/SoundTouch-WebServices-API). \ No newline at end of file +For more information, see the [SoundTouch WebServices API documentation](https://github.com/thlucas1/homeassistantcomponent_soundtouchplus/wiki/SoundTouch-WebServices-API). diff --git a/pkg/client/client.go b/pkg/client/client.go index ee130cc..ce532ca 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1769,8 +1769,8 @@ func (c *Client) hasCapability(capabilities *models.Capabilities, capability str } // PlayTTS plays a Text-To-Speech message using Google TTS on the speaker -func (c *Client) PlayTTS(text, appKey string, volume ...int) error { - playInfo := models.NewTTSPlayInfo(text, appKey, volume...) +func (c *Client) PlayTTS(text, appKey, language string, volume ...int) error { + playInfo := models.NewTTSPlayInfo(text, appKey, language, volume...) if err := playInfo.Validate(); err != nil { return fmt.Errorf("invalid TTS request: %w", err) diff --git a/pkg/models/speaker.go b/pkg/models/speaker.go index 3f41d87..1668da4 100644 --- a/pkg/models/speaker.go +++ b/pkg/models/speaker.go @@ -3,6 +3,8 @@ package models import ( "encoding/xml" "errors" + "fmt" + "net/url" ) // Error constants for speaker validation @@ -57,9 +59,9 @@ func (p *PlayInfo) SetVolume(volume int) *PlayInfo { } // NewTTSPlayInfo creates a PlayInfo for Google TTS playback -func NewTTSPlayInfo(text, appKey string, volume ...int) *PlayInfo { +func NewTTSPlayInfo(text, appKey, language string, volume ...int) *PlayInfo { // URL encode the text for Google TTS - url := "http://translate.google.com/translate_tts?ie=UTF-8&tl=EN&client=tw-ob&q=" + text + url := fmt.Sprintf("http://translate.google.com/translate_tts?ie=UTF-8&tl=%s&client=tw-ob&q=%s", language, url.QueryEscape(text)) playInfo := &PlayInfo{ XMLName: xml.Name{Local: "play_info"}, diff --git a/pkg/models/speaker_test.go b/pkg/models/speaker_test.go index 31ac368..330bbc1 100644 --- a/pkg/models/speaker_test.go +++ b/pkg/models/speaker_test.go @@ -35,9 +35,9 @@ func TestNewPlayInfo(t *testing.T) { func TestNewTTSPlayInfo(t *testing.T) { // Test without volume - playInfo := NewTTSPlayInfo("Hello World", "test-key") + playInfo := NewTTSPlayInfo("Hello World", "test-key", "EN") - expectedURL := "http://translate.google.com/translate_tts?ie=UTF-8&tl=EN&client=tw-ob&q=Hello World" + expectedURL := "http://translate.google.com/translate_tts?ie=UTF-8&tl=EN&client=tw-ob&q=Hello+World" if playInfo.URL != expectedURL { t.Errorf("Expected URL '%s', got '%s'", expectedURL, playInfo.URL) } @@ -63,7 +63,7 @@ func TestNewTTSPlayInfo(t *testing.T) { } // Test with volume - playInfoWithVolume := NewTTSPlayInfo("Hello World", "test-key", 50) + playInfoWithVolume := NewTTSPlayInfo("Hello World", "test-key", "EN", 50) if playInfoWithVolume.Volume == nil || *playInfoWithVolume.Volume != 50 { t.Errorf("Expected Volume to be 50, got %v", playInfoWithVolume.Volume) }