2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

98_GOOGLECAST: support TTS via Google Translate

git-svn-id: https://svn.fhem.de/fhem/trunk@15818 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
dominikkarall 2018-01-07 19:02:40 +00:00
parent 48534593ca
commit f6bcabffd1
2 changed files with 55 additions and 9 deletions

View File

@ -1,5 +1,7 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- feature: 98_GOOGLECAST: support speak command for TTS
set castdevice speak "Hallo"
- feature: 51_MOBILEALERTS: Added Sensor WL2000 - feature: 51_MOBILEALERTS: Added Sensor WL2000
- change: 93_DbRep: V7.3.0, charfilter avoid control characters in datasets - change: 93_DbRep: V7.3.0, charfilter avoid control characters in datasets
to export, function exportToFile importFromFile improved to export, function exportToFile importFromFile improved

View File

@ -7,10 +7,18 @@
# FHEM module to communicate with Google Cast devices # FHEM module to communicate with Google Cast devices
# e.g. Chromecast Video, Chromecast Audio, Google Home # e.g. Chromecast Video, Chromecast Audio, Google Home
# #
# Version: 2.0.1 # Version: 2.0.2
# #
############################################################# #############################################################
# #
# v2.0.2 - 20180106
# - FEATURE: support speak command for TTS
# set castdevice speak "Hallo"
# - BUGFIX: fix issues with umlauts in device name
# - BUGFIX: fix one socket issue
# - BUGFIX: fix delay for non youtube-dl links
# - BUGFIX: optimize delay for youtube links
#
# v2.0.1 - 20171209 # v2.0.1 - 20171209
# - FEATURE: support skip/rewind # - FEATURE: support skip/rewind
# - FEATURE: support displaying websites on Chromecast # - FEATURE: support displaying websites on Chromecast
@ -126,7 +134,7 @@ sub GOOGLECAST_Initialize($) {
$hash->{AttrList} = "favoriteURL_1 favoriteURL_2 favoriteURL_3 favoriteURL_4 ". $hash->{AttrList} = "favoriteURL_1 favoriteURL_2 favoriteURL_3 favoriteURL_4 ".
"favoriteURL_5 ".$readingFnAttributes; "favoriteURL_5 ".$readingFnAttributes;
Log3 $hash, 3, "GOOGLECAST: GoogleCast v2.0.1"; Log3 $hash, 3, "GOOGLECAST: GoogleCast v2.0.2";
return undef; return undef;
} }
@ -158,7 +166,7 @@ sub GOOGLECAST_findChromecasts {
my @ccResult = GOOGLECAST_findChromecastsPython(); my @ccResult = GOOGLECAST_findChromecastsPython();
foreach my $ref_cc (@ccResult) { foreach my $ref_cc (@ccResult) {
my @cc = @$ref_cc; my @cc = @$ref_cc;
$result .= "|CCDEVICE|".$cc[0]."|".$cc[1]."|".$cc[2]."|".$cc[3]."|".$cc[4]; $result .= "|CCDEVICE|".$cc[0]."|".$cc[1]."|".$cc[2]."|".$cc[3]."|".Encode::encode('UTF-8', $cc[4]);
} }
Log3 $name, 4, "GOOGLECAST: search result: $result"; Log3 $name, 4, "GOOGLECAST: search result: $result";
@ -223,7 +231,15 @@ sub GOOGLECAST_Attribute($$$$) {
sub GOOGLECAST_Set($@) { sub GOOGLECAST_Set($@) {
my ($hash, $name, @params) = @_; my ($hash, $name, @params) = @_;
my $workType = shift(@params); my $workType = shift(@params);
my $list = "stop:noArg pause:noArg rewind:noArg skip:noArg quitApp:noArg play playFavorite:1,2,3,4,5 volume:slider,0,1,100 displayWebsite"; my $list = "stop:noArg pause:noArg rewind:noArg skip:noArg quitApp:noArg play playFavorite:1,2,3,4,5 volume:slider,0,1,100 displayWebsite speak";
#get quoted text from params
my $blankParams = join(" ", @params);
my @params2;
while($blankParams =~ /"?((?<!")\S+(?<!")|[^"]+)"?\s*/g) {
push(@params2, $1);
}
@params = @params2;
# check parameters for set function # check parameters for set function
if($workType eq "?") { if($workType eq "?") {
@ -248,6 +264,8 @@ sub GOOGLECAST_Set($@) {
GOOGLECAST_setRewind($hash); GOOGLECAST_setRewind($hash);
} elsif($workType eq "skip") { } elsif($workType eq "skip") {
GOOGLECAST_setSkip($hash); GOOGLECAST_setSkip($hash);
} elsif($workType eq "speak") {
GOOGLECAST_setSpeak($hash, $params[0]);
} else { } else {
return SetExtensions($hash, $list, $name, $workType, @params); return SetExtensions($hash, $list, $name, $workType, @params);
} }
@ -274,13 +292,36 @@ sub GOOGLECAST_setWebsite {
}; };
} }
### speak ###
sub GOOGLECAST_setSpeak {
my ($hash, $ttsText) = @_;
my $ttsLang = AttrVal($hash->{NAME}, "ttsLanguage", "de");
return "GOOGLECAST: Maximum text length is 100 characters." if(length($ttsText) > 100);
$ttsText = uri_escape($ttsText);
my $ttsUrl = "http://translate.google.com/translate_tts?tl=$ttsLang&client=tw-ob&q=$ttsText";
eval {
$hash->{helper}{ccdevice}->{media_controller}->play_media($ttsUrl, "audio/mpeg");
};
return undef;
}
### playType ### ### playType ###
sub GOOGLECAST_setPlayType { sub GOOGLECAST_setPlayType {
my ($hash, $url, $mime) = @_; my ($hash, $url, $mime) = @_;
Log3 $hash, 4, "GOOGLECAST($hash->{NAME}): setPlayType($url, $mime)";
if($mime =~ m/text\/html/) {
GOOGLECAST_setPlayYtDl($hash, $url);
} else {
eval { eval {
Log3 $hash, 4, "GOOGLECAST($hash->{NAME}): start play_media";
$hash->{helper}{ccdevice}->{media_controller}->play_media($url, $mime); $hash->{helper}{ccdevice}->{media_controller}->play_media($url, $mime);
}; };
}
return undef; return undef;
} }
@ -309,6 +350,8 @@ sub GOOGLECAST_setPlayMedia_String {
my ($name, $videoUrl, $origUrl) = split("\\|", $string); my ($name, $videoUrl, $origUrl) = split("\\|", $string);
my $hash = $main::defs{$name}; my $hash = $main::defs{$name};
Log3 $hash, 4, "GOOGLECAST($name): setPlayMedia_String($string)";
if($videoUrl ne "") { if($videoUrl ne "") {
GOOGLECAST_setPlayMedia($hash, $videoUrl); GOOGLECAST_setPlayMedia($hash, $videoUrl);
} else { } else {
@ -391,7 +434,7 @@ sub GOOGLECAST_setPlay {
if($url =~ /^http/) { if($url =~ /^http/) {
#support streams are listed here #support streams are listed here
#https://github.com/rg3/youtube-dl/blob/master/docs/supportedsites.md #https://github.com/rg3/youtube-dl/blob/master/docs/supportedsites.md
GOOGLECAST_setPlayYtDl($hash, $url); GOOGLECAST_setPlayMedia($hash, $url);
} else { } else {
GOOGLECAST_playYouTube($hash->{helper}{ccdevice}, $url); GOOGLECAST_playYouTube($hash->{helper}{ccdevice}, $url);
} }
@ -514,6 +557,7 @@ sub GOOGLECAST_checkConnection {
if($@ || !defined($selectlist{"GOOGLECAST-".$hash->{NAME}})) { if($@ || !defined($selectlist{"GOOGLECAST-".$hash->{NAME}})) {
Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): checkConnection, connection failure, reconnect..."; Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): checkConnection, connection failure, reconnect...";
delete($selectlist{"GOOGLECAST-".$hash->{NAME}});
GOOGLECAST_initDevice($hash); GOOGLECAST_initDevice($hash);
GOOGLECAST_updateReading($hash, "presence", "offline"); GOOGLECAST_updateReading($hash, "presence", "offline");
return undef; return undef;
@ -617,7 +661,7 @@ def GOOGLECAST_createChromecastPython(ip, port, uuid, model_name, friendly_name)
return cast return cast
def GOOGLECAST_getYTVideoURLPython(yt_url): def GOOGLECAST_getYTVideoURLPython(yt_url):
ydl = youtube_dl.YoutubeDL({'quiet': '1', 'no_warnings': '1'}) ydl = youtube_dl.YoutubeDL({'forceurl': True, 'simulate': True, 'quiet': '1', 'no_warnings': '1', 'skip_download': True, 'format': 'best', 'youtube_include_dash_manifest': False})
with ydl: with ydl:
result = ydl.extract_info( result = ydl.extract_info(