98_GOOGLECAST.pm 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. #############################################################
  2. #
  3. # GOOGLECAST.pm (c) by Dominik Karall, 2016-2018
  4. # dominik karall at gmail dot com
  5. # $Id: 98_GOOGLECAST.pm 16386 2018-03-11 19:37:47Z dominik $
  6. #
  7. # FHEM module to communicate with Google Cast devices
  8. # e.g. Chromecast Video, Chromecast Audio, Google Home
  9. #
  10. # Version: 2.1.3
  11. #
  12. #############################################################
  13. #
  14. # v2.1.3 - 20180311
  15. # - BUGFIX: fix umlauts for device name readings
  16. #
  17. # v2.1.2 - 20180310
  18. # - BUGFIX: fix speak, play, displayWebsite
  19. # - FEATURE: added German commandref (thx@Sailor)
  20. #
  21. # v2.1.1 - 20180303
  22. # - BUGFIX: fix crash on connection failures
  23. # - BUGFIX: update FHEM socket on pychromecast reconnect
  24. #
  25. # v2.1.0 - 20180218
  26. # - BUGFIX: one more socket_client fix
  27. # - BUGFIX: offline state fix
  28. #
  29. # v2.0.3 - 20180217
  30. # - CHANGE: increase speak limit to 500 characters
  31. #
  32. # v2.0.2 - 20180106
  33. # - FEATURE: support speak command for TTS
  34. # set castdevice speak "Hallo"
  35. # - BUGFIX: fix issues with umlauts in device name
  36. # - BUGFIX: fix one socket issue
  37. # - BUGFIX: fix delay for non youtube-dl links
  38. # - BUGFIX: optimize delay for youtube links
  39. #
  40. # v2.0.1 - 20171209
  41. # - FEATURE: support skip/rewind
  42. # - FEATURE: support displaying websites on Chromecast
  43. #
  44. # v2.0.0 - 20170812
  45. # - CHANGE: renamed to 98_GOOGLECAST.pm
  46. # - CHANGE: removed favoriteName_X attribute, it was never used
  47. # - BUGFIX: updated commandref with further required packages
  48. # - FEATURE: state reading now represents status (online, offline,
  49. # playing, paused, buffering)
  50. # - FEATURE: new readings mediaContentId, mediaCurrentPosition,
  51. # mediaDuration, mediaPlayerState, mediaStreamType
  52. # - BUGFIX: change volume to represent integer values only
  53. #
  54. # v1.0.7 - 20170804
  55. # - BUGFIX: fix reconnection in some cases
  56. #
  57. # v1.0.6 - 20170705
  58. # - BUGFIX: speed up youtube video URL extraction with youtube_dl
  59. # - BUGFIX: fixed one more issue when chromecast offline
  60. # - BUGFIX: improved performance by adding socket to FHEM main loop
  61. #
  62. # v1.0.5 - 20170704
  63. # - BUGFIX: hopefuly fixed the annoying hangs when chromecast offline
  64. # - FEATURE: add presence reading (online/offline)
  65. #
  66. # v1.0.4 - 20170101
  67. # - FEATURE: support all services supported by youtube-dl
  68. # https://github.com/rg3/youtube-dl/blob/master/docs/supportedsites.md
  69. # playlists not yet supported!
  70. # - BUGFIX: support non-blocking chromecast search
  71. #
  72. # v1.0.3 - 20161219
  73. # - FEATURE: support volume
  74. # - FEATURE: add new readings and removed
  75. # castStatus, mediaStatus reading
  76. # - FEATURE: add attribute favoriteURL_[1-5]
  77. # - FEATURE: add playFavorite [1-5] set function
  78. # - FEATURE: retry init chromecast every 10s if not found on startup
  79. # - BUGFIX: support special characters for device name
  80. #
  81. # v1.0.2 - 20161216
  82. # - FEATURE: support play of every mime type which is supported
  83. # by Chromecast (see https://developers.google.com/cast/docs/media)
  84. # including youtube URLs
  85. # - CHANGE: change play* methods to play <url>
  86. # - FEATURE: support very simple .m3u which contain only URL
  87. # - BUGFIX: non-blocking playYoutube
  88. # - BUGFIX: fix play if media player is already running
  89. #
  90. # v1.0.1 - 20161211
  91. # - FEATURE: support playYoutube <youtubelink>
  92. #
  93. # v1.0.0 - 20161015
  94. # - FEATURE: first public release
  95. #
  96. # TODO
  97. # - check spotify integration
  98. # - support youtube playlists
  99. #
  100. # NOTES
  101. # def play_media(self, url, content_type, title=None, thumb=None,
  102. # current_time=0, autoplay=True,
  103. # stream_type=STREAM_TYPE_BUFFERED,
  104. # metadata=None, subtitles=None, subtitles_lang='en-US',
  105. # subtitles_mime='text/vtt', subtitle_id=1):
  106. # """
  107. # Plays media on the Chromecast. Start default media receiver if not
  108. # already started.
  109. # Parameters:
  110. # url: str - url of the media.
  111. # content_type: str - mime type. Example: 'video/mp4'.
  112. # title: str - title of the media.
  113. # thumb: str - thumbnail image url.
  114. # current_time: float - seconds from the beginning of the media
  115. # to start playback.
  116. # autoplay: bool - whether the media will automatically play.
  117. # stream_type: str - describes the type of media artifact as one of the
  118. # following: "NONE", "BUFFERED", "LIVE".
  119. # subtitles: str - url of subtitle file to be shown on chromecast.
  120. # subtitles_lang: str - language for subtitles.
  121. # subtitles_mime: str - mimetype of subtitles.
  122. # subtitle_id: int - id of subtitle to be loaded.
  123. # metadata: dict - media metadata object, one of the following:
  124. # GenericMediaMetadata, MovieMediaMetadata, TvShowMediaMetadata,
  125. # MusicTrackMediaMetadata, PhotoMediaMetadata.
  126. # Docs:
  127. # https://developers.google.com/cast/docs/reference/messages#MediaData
  128. # """
  129. #
  130. #############################################################
  131. package main;
  132. use strict;
  133. use warnings;
  134. use Blocking;
  135. use Encode;
  136. use SetExtensions;
  137. use URI::Escape;
  138. use LWP::UserAgent;
  139. sub GOOGLECAST_Initialize($) {
  140. my ($hash) = @_;
  141. $hash->{DefFn} = 'GOOGLECAST_Define';
  142. $hash->{UndefFn} = 'GOOGLECAST_Undef';
  143. $hash->{GetFn} = 'GOOGLECAST_Get';
  144. $hash->{SetFn} = 'GOOGLECAST_Set';
  145. $hash->{ReadFn} = 'GOOGLECAST_Read';
  146. $hash->{AttrFn} = 'GOOGLECAST_Attribute';
  147. $hash->{AttrList} = "favoriteURL_1 favoriteURL_2 favoriteURL_3 favoriteURL_4 ".
  148. "favoriteURL_5 ".$readingFnAttributes;
  149. Log3 $hash, 3, "GOOGLECAST: GoogleCast v2.1.3";
  150. return undef;
  151. }
  152. sub GOOGLECAST_Define($$) {
  153. my ($hash, $def) = @_;
  154. my @a = split("[ \t]+", $def);
  155. my $name = $a[0];
  156. $hash->{STATE} = "initialized";
  157. if (int(@a) > 3) {
  158. return 'GOOGLECAST: Wrong syntax, must be define <name> GOOGLECAST <device name>';
  159. } elsif(int(@a) == 3) {
  160. Log3 $hash, 3, "GOOGLECAST: $a[2] initializing...";
  161. $hash->{CCNAME} = $a[2];
  162. GOOGLECAST_updateReading($hash, "presence", "offline");
  163. GOOGLECAST_updateReading($hash, "state", "offline");
  164. GOOGLECAST_initDevice($hash);
  165. }
  166. return undef;
  167. }
  168. sub GOOGLECAST_findChromecasts {
  169. my ($string) = @_;
  170. my ($name) = split("\\|", $string);
  171. my $result = "$name";
  172. my @ccResult = GOOGLECAST_PyFindChromecasts();
  173. foreach my $ref_cc (@ccResult) {
  174. my @cc = @$ref_cc;
  175. $result .= "|CCDEVICE|".$cc[0]."|".$cc[1]."|".$cc[2]."|".$cc[3]."|".Encode::encode('UTF-8', $cc[4]);
  176. }
  177. Log3 $name, 4, "GOOGLECAST: search result: $result";
  178. return $result;
  179. }
  180. sub GOOGLECAST_initDevice {
  181. my ($hash) = @_;
  182. my $devName = $hash->{CCNAME};
  183. BlockingCall("GOOGLECAST_findChromecasts", $hash->{NAME}, "GOOGLECAST_findChromecastsResult");
  184. return undef;
  185. }
  186. sub GOOGLECAST_findChromecastsResult {
  187. my ($string) = @_;
  188. my ($name, @ccResult) = split("\\|", $string);
  189. my $hash = $main::defs{$name};
  190. my $devName = $hash->{CCNAME};
  191. $hash->{helper}{ccdevice} = "";
  192. for my $i (0..$#ccResult) {
  193. if($ccResult[$i] eq "CCDEVICE" and $ccResult[$i+5] eq $devName) {
  194. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): init cast device $devName";
  195. eval {
  196. $hash->{helper}{ccdevice} = GOOGLECAST_PyCreateChromecast($ccResult[$i+1],$ccResult[$i+2],$ccResult[$i+3],$ccResult[$i+4],$ccResult[$i+5]);
  197. };
  198. if($@) {
  199. $hash->{helper}{ccdevice} = "";
  200. }
  201. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): device initialized";
  202. }
  203. }
  204. if($hash->{helper}{ccdevice} eq "") {
  205. Log3 $hash, 4, "GOOGLECAST: $devName not found, retry in 10s.";
  206. InternalTimer(gettimeofday()+10, "GOOGLECAST_initDevice", $hash, 0);
  207. return undef;
  208. }
  209. Log3 $hash, 3, "GOOGLECAST: $devName initialized successfully";
  210. GOOGLECAST_addSocketToMainloop($hash);
  211. GOOGLECAST_checkConnection($hash);
  212. return undef;
  213. }
  214. sub GOOGLECAST_Attribute($$$$) {
  215. my ($mode, $devName, $attrName, $attrValue) = @_;
  216. if($mode eq "set") {
  217. } elsif($mode eq "del") {
  218. }
  219. return undef;
  220. }
  221. sub GOOGLECAST_Set($@) {
  222. my ($hash, $name, @params) = @_;
  223. my $workType = shift(@params);
  224. 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";
  225. #get quoted text from params
  226. my $blankParams = join(" ", @params);
  227. my @params2;
  228. while($blankParams =~ /"?((?<!")\S+(?<!")|[^"]+)"?\s*/g) {
  229. push(@params2, $1);
  230. }
  231. @params = @params2;
  232. # check parameters for set function
  233. if($workType eq "?") {
  234. return SetExtensions($hash, $list, $name, $workType, @params);
  235. }
  236. if($workType eq "stop") {
  237. GOOGLECAST_setStop($hash);
  238. } elsif($workType eq "pause") {
  239. GOOGLECAST_setPause($hash);
  240. } elsif($workType eq "play") {
  241. GOOGLECAST_setPlay($hash, $params[0]);
  242. } elsif($workType eq "playFavorite") {
  243. GOOGLECAST_setPlayFavorite($hash, $params[0]);
  244. } elsif($workType eq "quitApp") {
  245. GOOGLECAST_setQuitApp($hash);
  246. } elsif($workType eq "volume") {
  247. GOOGLECAST_setVolume($hash, $params[0]);
  248. } elsif($workType eq "displayWebsite") {
  249. GOOGLECAST_setWebsite($hash, $params[0]);
  250. } elsif($workType eq "rewind") {
  251. GOOGLECAST_setRewind($hash);
  252. } elsif($workType eq "skip") {
  253. GOOGLECAST_setSkip($hash);
  254. } elsif($workType eq "speak") {
  255. GOOGLECAST_setSpeak($hash, $params[0]);
  256. } else {
  257. return SetExtensions($hash, $list, $name, $workType, @params);
  258. }
  259. return undef;
  260. }
  261. ### volume ###
  262. sub GOOGLECAST_setVolume {
  263. my ($hash, $volume) = @_;
  264. $volume = $volume/100;
  265. eval {
  266. $hash->{helper}{ccdevice}->set_volume($volume);
  267. };
  268. }
  269. ### dashcast ###
  270. sub GOOGLECAST_setWebsite {
  271. my ($hash, $url) = @_;
  272. eval {
  273. GOOGLECAST_PyLoadDashCast($hash->{helper}{ccdevice}, $url);
  274. };
  275. }
  276. ### speak ###
  277. sub GOOGLECAST_setSpeak {
  278. my ($hash, $ttsText) = @_;
  279. my $ttsLang = AttrVal($hash->{NAME}, "ttsLanguage", "de");
  280. return "GOOGLECAST: Maximum text length is 500 characters." if(length($ttsText) > 500);
  281. $ttsText = uri_escape($ttsText);
  282. my $ttsUrl = "http://translate.google.com/translate_tts?tl=$ttsLang&client=tw-ob&q=$ttsText";
  283. Log3 $hash, 4, "GOOGLECAST($hash->{NAME}): setSpeak $ttsUrl";
  284. eval {
  285. GOOGLECAST_PyPlayMedia($hash->{helper}{ccdevice}, $ttsUrl, "audio/mpeg");
  286. };
  287. return undef;
  288. }
  289. ### playType ###
  290. sub GOOGLECAST_setPlayType {
  291. my ($hash, $url, $mime) = @_;
  292. Log3 $hash, 4, "GOOGLECAST($hash->{NAME}): setPlayType($url, $mime)";
  293. if($mime =~ m/text\/html/) {
  294. GOOGLECAST_setPlayYtDl($hash, $url);
  295. } else {
  296. eval {
  297. Log3 $hash, 4, "GOOGLECAST($hash->{NAME}): start play_media";
  298. GOOGLECAST_PyPlayMedia($hash->{helper}{ccdevice}, $url, $mime);
  299. };
  300. }
  301. return undef;
  302. }
  303. sub GOOGLECAST_setPlayType_String {
  304. my ($string) = @_;
  305. my ($name, $url, $mime) = split("\\|", $string);
  306. my $hash = $main::defs{$name};
  307. if($mime ne "" && $url ne "") {
  308. GOOGLECAST_setPlayType($hash, $url, $mime);
  309. }
  310. }
  311. ### playMedia ###
  312. sub GOOGLECAST_setPlayMedia {
  313. my ($hash, $url) = @_;
  314. BlockingCall("GOOGLECAST_setPlayMediaBlocking", $hash->{NAME}."|".$url, "GOOGLECAST_setPlayType_String");
  315. return undef;
  316. }
  317. sub GOOGLECAST_setPlayMedia_String {
  318. my ($string) = @_;
  319. my ($name, $videoUrl, $origUrl) = split("\\|", $string);
  320. my $hash = $main::defs{$name};
  321. Log3 $hash, 4, "GOOGLECAST($name): setPlayMedia_String($string)";
  322. if($videoUrl ne "") {
  323. GOOGLECAST_setPlayMedia($hash, $videoUrl);
  324. } else {
  325. Log3 $hash, 3, "GOOGLECAST($name): setPlayMedia_String, youtube-dl couldn't find video";
  326. #GOOGLECAST_setPlayMedia($hash, $origUrl);
  327. }
  328. }
  329. sub GOOGLECAST_setPlayMediaBlocking {
  330. my ($string) = @_;
  331. my ($name, $url) = split("\\|", $string);
  332. #$url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
  333. #$url = "http://swr-mp3-m-swr3.akacast.akamaistream.net:80/7/720/137136/v1/gnl.akacast.akamaistream.net/swr-mp3-m-swr3";
  334. my $ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');
  335. $ua->max_size(0);
  336. my $resp = $ua->get($url);
  337. my $mime = $resp->header('Content-Type');
  338. if($mime eq "audio/x-mpegurl") {
  339. $mime = "audio/mpeg";
  340. $url = $resp->decoded_content;
  341. $url =~ s/\R//g;
  342. }
  343. return $name."|".$url."|".$mime;
  344. }
  345. ### playYoutue ###
  346. sub GOOGLECAST_setPlayYtDl {
  347. my ($hash, $ytUrl) = @_;
  348. BlockingCall("GOOGLECAST_setPlayYtDlBlocking", $hash->{NAME}."|".$ytUrl, "GOOGLECAST_setPlayMedia_String");
  349. return undef;
  350. }
  351. sub GOOGLECAST_setPlayYtDlBlocking {
  352. my ($string) = @_;
  353. my ($name, $ytUrl) = split("\\|", $string);
  354. my $videoUrl = "";
  355. eval {
  356. $videoUrl = GOOGLECAST_PyGetYTVideoURL($ytUrl);
  357. };
  358. return $name."|".$videoUrl."|".$ytUrl;
  359. }
  360. ### stop ###
  361. sub GOOGLECAST_setStop {
  362. my ($hash) = @_;
  363. eval {
  364. $hash->{helper}{ccdevice}->{media_controller}->stop();
  365. };
  366. return undef;
  367. }
  368. ### playFavorite ###
  369. sub GOOGLECAST_setPlayFavorite {
  370. my ($hash, $favoriteNr) = @_;
  371. GOOGLECAST_setPlay($hash, AttrVal($hash->{NAME}, "favoriteURL_".$favoriteNr, ""));
  372. return undef;
  373. }
  374. ### play ###
  375. sub GOOGLECAST_setPlay {
  376. my ($hash, $url) = @_;
  377. if(!defined($url)) {
  378. eval {
  379. $hash->{helper}{ccdevice}->{media_controller}->play();
  380. };
  381. return undef;
  382. }
  383. if($url =~ /^http/) {
  384. #support streams are listed here
  385. #https://github.com/rg3/youtube-dl/blob/master/docs/supportedsites.md
  386. GOOGLECAST_setPlayMedia($hash, $url);
  387. } else {
  388. GOOGLECAST_PyPlayYouTube($hash->{helper}{ccdevice}, $url);
  389. }
  390. return undef;
  391. }
  392. ### pause ###
  393. sub GOOGLECAST_setPause {
  394. my ($hash) = @_;
  395. eval {
  396. $hash->{helper}{ccdevice}->{media_controller}->pause();
  397. };
  398. return undef;
  399. }
  400. ### rewind ###
  401. sub GOOGLECAST_setRewind {
  402. my ($hash) = @_;
  403. eval {
  404. $hash->{helper}{ccdevice}->{media_controller}->rewind();
  405. };
  406. return undef;
  407. }
  408. ### skip ###
  409. sub GOOGLECAST_setSkip {
  410. my ($hash) = @_;
  411. eval {
  412. $hash->{helper}{ccdevice}->{media_controller}->seek($hash->{helper}{ccdevice}->{media_controller}->{status}->{duration});
  413. };
  414. return undef;
  415. }
  416. ### quitApp ###
  417. sub GOOGLECAST_setQuitApp {
  418. my ($hash) = @_;
  419. eval {
  420. $hash->{helper}{ccdevice}->quit_app();
  421. };
  422. return undef;
  423. }
  424. sub GOOGLECAST_Undef($) {
  425. my ($hash) = @_;
  426. #remove internal timer
  427. RemoveInternalTimer($hash);
  428. return undef;
  429. }
  430. sub GOOGLECAST_Get($$) {
  431. return undef;
  432. }
  433. sub GOOGLECAST_updateReading {
  434. my ($hash, $readingName, $value) = @_;
  435. my $oldValue = ReadingsVal($hash->{NAME}, $readingName, "");
  436. if(!defined($value)) {
  437. $value = "";
  438. }
  439. if($oldValue ne $value) {
  440. readingsSingleUpdate($hash, $readingName, $value, 1);
  441. }
  442. }
  443. sub GOOGLECAST_newChash {
  444. my ($hash, $socket, $chash) = @_;
  445. $chash->{TYPE} = $hash->{TYPE};
  446. $chash->{UDN} = -1;
  447. $chash->{NR} = $devcount++;
  448. $chash->{phash} = $hash;
  449. $chash->{PNAME} = $hash->{NAME};
  450. $chash->{CD} = $socket;
  451. $chash->{FD} = $socket->fileno();
  452. #$chash->{PORT} = $socket->sockport if( $socket->sockport );
  453. $chash->{TEMPORARY} = 1;
  454. $attr{$chash->{NAME}}{room} = 'hidden';
  455. $defs{$chash->{NAME}} = $chash;
  456. $selectlist{$chash->{NAME}} = $chash;
  457. }
  458. sub GOOGLECAST_addSocketToMainloop {
  459. my ($hash) = @_;
  460. my $sock;
  461. #delete any previous sockets
  462. delete($selectlist{"GOOGLECAST-".$hash->{NAME}});
  463. eval {
  464. $sock = $hash->{helper}{ccdevice}->{socket_client}->get_socket();
  465. $hash->{helper}{currentsock} = $sock;
  466. };
  467. my $chash = GOOGLECAST_newChash($hash, $sock, {NAME => "GOOGLECAST-".$hash->{NAME}});
  468. return undef;
  469. }
  470. sub GOOGLECAST_checkConnection {
  471. my ($hash) = @_;
  472. eval {
  473. Log3 $hash, 5, "GOOGLECAST ($hash->{NAME}): run_once";
  474. $hash->{helper}{ccdevice}->{socket_client}->run_once();
  475. };
  476. if($@ || !defined($selectlist{"GOOGLECAST-".$hash->{NAME}})) {
  477. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): checkConnection, connection failure, reconnect...";
  478. delete($selectlist{"GOOGLECAST-".$hash->{NAME}});
  479. $hash->{helper}{ccdevice}->{socket_client}->_cleanup() if(defined($hash->{helper}{ccdevice}->{socket_client}->get_socket()));
  480. GOOGLECAST_initDevice($hash);
  481. GOOGLECAST_updateReading($hash, "presence", "offline");
  482. GOOGLECAST_updateReading($hash, "state", "offline");
  483. return undef;
  484. } else {
  485. #update socket
  486. my $sock = $hash->{helper}{ccdevice}->{socket_client}->get_socket();
  487. if(defined($sock) && $hash->{helper}{currentsock} ne $sock) {
  488. GOOGLECAST_addSocketToMainloop($hash);
  489. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): checkConnection, pychromecast reconnected-update fhem socket";
  490. } elsif(!defined($sock)) {
  491. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): checkConnection, pychromecast socket=NULL";
  492. }
  493. }
  494. InternalTimer(gettimeofday()+10, "GOOGLECAST_checkConnection", $hash, 0);
  495. return undef;
  496. }
  497. sub GOOGLECAST_Read {
  498. my ($hash) = @_;
  499. my $name = $hash->{NAME};
  500. $hash = $hash->{phash};
  501. eval {
  502. Log3 $hash, 5, "GOOGLECAST ($hash->{NAME}): run_once";
  503. $hash->{helper}{ccdevice}->{socket_client}->run_once();
  504. };
  505. if($@) {
  506. Log3 $hash, 4, "GOOGLECAST ($hash->{NAME}): connection failure, reconnect...";
  507. eval {
  508. delete($selectlist{$name});
  509. };
  510. $hash->{helper}{ccdevice}->{socket_client}->_cleanup() if(defined($hash->{helper}{ccdevice}->{socket_client}->get_socket()));
  511. GOOGLECAST_initDevice($hash);
  512. GOOGLECAST_updateReading($hash, "presence", "offline");
  513. GOOGLECAST_updateReading($hash, "state", "offline");
  514. return undef;
  515. }
  516. GOOGLECAST_updateReading($hash, "presence", "online");
  517. GOOGLECAST_updateReading($hash, "name", Encode::encode("UTF-8", $hash->{helper}{ccdevice}->{name}));
  518. GOOGLECAST_updateReading($hash, "model", $hash->{helper}{ccdevice}->{model_name});
  519. GOOGLECAST_updateReading($hash, "uuid", $hash->{helper}{ccdevice}->{uuid});
  520. GOOGLECAST_updateReading($hash, "castType", $hash->{helper}{ccdevice}->{cast_type});
  521. GOOGLECAST_updateReading($hash, "model", $hash->{helper}{ccdevice}->{model_name});
  522. GOOGLECAST_updateReading($hash, "appId", $hash->{helper}{ccdevice}->{app_id});
  523. GOOGLECAST_updateReading($hash, "appName", $hash->{helper}{ccdevice}->{app_display_name});
  524. GOOGLECAST_updateReading($hash, "idle", $hash->{helper}{ccdevice}->{is_idle});
  525. my $newStatus = $hash->{helper}{ccdevice}->{media_controller}->{status};
  526. if(defined($newStatus)) {
  527. #GOOGLECAST_updateReading($hash, "mediaStatus", $newStatus);
  528. GOOGLECAST_updateReading($hash, "mediaPlayerState", $newStatus->{player_state});
  529. GOOGLECAST_updateReading($hash, "mediaContentId", $newStatus->{content_id});
  530. GOOGLECAST_updateReading($hash, "mediaDuration", $newStatus->{duration});
  531. GOOGLECAST_updateReading($hash, "mediaCurrentPosition", $newStatus->{current_time});
  532. GOOGLECAST_updateReading($hash, "mediaStreamType", $newStatus->{stream_type});
  533. GOOGLECAST_updateReading($hash, "mediaTitle", $newStatus->{title});
  534. GOOGLECAST_updateReading($hash, "mediaSeriesTitle", $newStatus->{series_title});
  535. GOOGLECAST_updateReading($hash, "mediaSeason", $newStatus->{season});
  536. GOOGLECAST_updateReading($hash, "mediaEpisode", $newStatus->{episode});
  537. GOOGLECAST_updateReading($hash, "mediaArtist", $newStatus->{artist});
  538. GOOGLECAST_updateReading($hash, "mediaAlbum", $newStatus->{album_name});
  539. GOOGLECAST_updateReading($hash, "mediaAlbumArtist", $newStatus->{album_artist});
  540. GOOGLECAST_updateReading($hash, "mediaTrack", $newStatus->{track});
  541. if(length($newStatus->{images}) > 0) {
  542. GOOGLECAST_updateReading($hash, "mediaImage", $newStatus->{images}[0]->{url});
  543. } else {
  544. GOOGLECAST_updateReading($hash, "mediaImage", "");
  545. }
  546. }
  547. my $newCastStatus = $hash->{helper}{ccdevice}->{status};
  548. if(defined($newCastStatus)) {
  549. #GOOGLECAST_updateReading($hash, "castStatus", $newCastStatus);
  550. GOOGLECAST_updateReading($hash, "volume", int($newCastStatus->{volume_level}*100));
  551. }
  552. my $curStatus = ReadingsVal($hash->{NAME}, "mediaPlayerState", "UNKNOWN");
  553. if($curStatus eq "PLAYING") {
  554. GOOGLECAST_updateReading($hash, "state", "playing");
  555. } elsif($curStatus eq "BUFFERING") {
  556. GOOGLECAST_updateReading($hash, "state", "buffering");
  557. } elsif($curStatus eq "PAUSED") {
  558. GOOGLECAST_updateReading($hash, "state", "paused");
  559. } else {
  560. GOOGLECAST_updateReading($hash, "state", ReadingsVal($hash->{NAME}, "presence", "offline"));
  561. }
  562. return undef;
  563. }
  564. use Inline Python => <<'PYTHON_CODE_END';
  565. from __future__ import unicode_literals
  566. import pychromecast
  567. import time
  568. import logging
  569. import youtube_dl
  570. import pychromecast.controllers.dashcast as dashcast
  571. import pychromecast.controllers.youtube as youtube
  572. def GOOGLECAST_PyFindChromecasts():
  573. logging.basicConfig(level=logging.CRITICAL)
  574. return pychromecast.discovery.discover_chromecasts()
  575. def GOOGLECAST_PyCreateChromecast(ip, port, uuid, model_name, friendly_name):
  576. logging.basicConfig(level=logging.CRITICAL)
  577. cast = pychromecast._get_chromecast_from_host((ip.decode("utf-8"), int(port), uuid.decode("utf-8"), model_name.decode("utf-8"), friendly_name.decode("utf-8")), blocking=False, timeout=0.1, tries=1, retry_wait=0.1)
  578. return cast
  579. def GOOGLECAST_PyPlayMedia(cast, url, mime):
  580. logging.basicConfig(level=logging.CRITICAL)
  581. cast.play_media(url.decode("utf-8"), mime.decode("utf-8"))
  582. return undef
  583. def GOOGLECAST_PyGetYTVideoURL(yt_url):
  584. yt_url = yt_url.decode("utf-8")
  585. ydl = youtube_dl.YoutubeDL({'forceurl': True, 'simulate': True, 'quiet': '1', 'no_warnings': '1', 'skip_download': True, 'format': 'best', 'youtube_include_dash_manifest': False})
  586. result = ydl.extract_info(yt_url, download=False)
  587. if 'entries' in result:
  588. # Can be a playlist or a list of videos
  589. video = result['entries'][0]
  590. else:
  591. # Just a video
  592. video = result
  593. video_url = video['url']
  594. return video_url
  595. def GOOGLECAST_PyLoadDashCast(cast, url):
  596. url = url.decode("utf-8")
  597. d = dashcast.DashCastController()
  598. cast.register_handler(d)
  599. d.load_url(url,reload_seconds=60)
  600. def GOOGLECAST_PyPlayYouTube(cast, videoId):
  601. videoId = videoId.decode("utf-8")
  602. yt = youtube.YouTubeController()
  603. cast.register_handler(yt)
  604. yt.play_video(videoId)
  605. PYTHON_CODE_END
  606. 1;
  607. =pod
  608. =item device
  609. =item summary Easily control your Google Cast devices (Video, Audio, Google Home)
  610. =item summary_DE Einfache Steuerung deiner Google Cast Geräte (Video, Audio, Google Home)
  611. =begin html
  612. <a name="GOOGLECAST"></a>
  613. <h3>GOOGLECAST</h3>
  614. <ul>
  615. GOOGLECAST is used to control your Google Cast device<br><br>
  616. <b>Note</b><br>Make sure that python3 is installed. Following packages are required:
  617. <ul>
  618. <li>sudo apt-get install libwww-perl python-enum34 python-dev libextutils-makemaker-cpanfile-perl python3-pip cpanminus</li>
  619. <li>sudo pip3 install pychromecast --upgrade</li>
  620. <li>sudo pip3 install youtube-dl --upgrade</li>
  621. <li>sudo INLINE_PYTHON_EXECUTABLE=/usr/bin/python3 cpanm Inline::Python</li>
  622. </ul>
  623. <br>
  624. <br>
  625. <a name="GOOGLECASTdefine" id="GOOGLECASTdefine"></a>
  626. <b>Define</b>
  627. <ul>
  628. <code>define &lt;name&gt; GOOGLECAST &lt;name&gt;</code><br>
  629. <br>
  630. Example:
  631. <ul>
  632. <code>define livingroom.chromecast GOOGLECAST livingroom</code><br><br>
  633. Wait a few seconds till presence switches to online...<br><br>
  634. <code>set livingroom.chromecast play https://www.youtube.com/watch?v=YE7VzlLtp-4</code><br>
  635. </ul>
  636. <br>
  637. Following media types are supported:<br>
  638. <a href="https://developers.google.com/cast/docs/media">Supported media formats</a><br>
  639. Play with youtube-dl works for following URLs:<br>
  640. <a href="https://rg3.github.io/youtube-dl/supportedsites.html">Supported youtube-dl sites</a><br>
  641. <br>
  642. </ul>
  643. <br>
  644. <a name="GOOGLECASTset" id="GOOGLECASTset"></a>
  645. <b>Set</b>
  646. <ul>
  647. <code>set &lt;name&gt; &lt;command&gt; [&lt;parameter&gt;]</code><br>
  648. The following commands are defined:<br><br>
  649. <ul>
  650. <li><code><b>play</b> URL</code> &nbsp;&nbsp;-&nbsp;&nbsp; play from URL</li>
  651. <li><code><b>play</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; play, like resume if paused previsously</li>
  652. <li><code><b>playFavorite</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; plays URL from favoriteURL_[1-5]</li>
  653. <li><code><b>stop</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; stop, stops current playback</li>
  654. <li><code><b>pause</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; pause</li>
  655. <li><code><b>quitApp</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; quit current application, like YouTube</li>
  656. <li><code><b>skip</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; skip track and play next</li>
  657. <li><code><b>rewind</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; rewind track and play it again</li>
  658. <li><code><b>displayWebsite</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; displayWebsite on Chromecast Video</li>
  659. </ul>
  660. <br>
  661. </ul>
  662. <a name="GOOGLECASTattr" id="GOOGLECASTattr"></a>
  663. <b>Attributes</b>
  664. <ul>
  665. <li><code><b>favoriteURL_[1-5]</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; save URL to play afterwards with playFavorite [1-5]</li>
  666. </ul>
  667. <br>
  668. <a name="GOOGLECASTget" id="GOOGLECASTget"></a>
  669. <b>Get</b>
  670. <ul>
  671. <code>n/a</code>
  672. </ul>
  673. <br>
  674. </ul>
  675. =end html
  676. =begin html_DE
  677. <a name="GOOGLECAST"></a>
  678. <h3>GOOGLECAST</h3>
  679. <ul>
  680. GOOGLECAST wird zur Steueung deines Google Cast Devices verwendet<br><br>
  681. <b>Note</b><br>Es ist sicherzustellen, dass python3 installiert ist. Zus&auml;tzlich werden folgende Pakete ben&ouml;tigt:
  682. <ul>
  683. <li>sudo apt-get install libwww-perl python-enum34 python-dev libextutils-makemaker-cpanfile-perl python3-pip cpanminus</li>
  684. <li>sudo pip3 install pychromecast --upgrade</li>
  685. <li>sudo pip3 install youtube-dl --upgrade</li>
  686. <li>sudo INLINE_PYTHON_EXECUTABLE=/usr/bin/python3 cpanm Inline::Python</li>
  687. </ul>
  688. <br>
  689. <br>
  690. <a name="GOOGLECASTdefine" id="GOOGLECASTdefine"></a>
  691. <b>Define</b>
  692. <ul>
  693. <code>define &lt;name&gt; GOOGLECAST &lt;name&gt;</code><br>
  694. <br>
  695. Beispiel:
  696. <ul>
  697. <code>define livingroom.chromecast GOOGLECAST livingroom</code><br><br>
  698. Warte ein paar Sekunden bis das Ger&auml;t als ONLINE angezeigt wird...<br><br>
  699. <code>set livingroom.chromecast play https://www.youtube.com/watch?v=YE7VzlLtp-4</code><br>
  700. </ul>
  701. <br>
  702. Die folgenden Medienformate werden unterst&uuml;tzt:<br>
  703. <a href="https://developers.google.com/cast/docs/media">Unterst&uuml;tzte Medienformate</a><br>
  704. Das Abspielen mittels youtube-dl funktioniert f&uuml;r die folgenden URLs:<br>
  705. <a href="https://rg3.github.io/youtube-dl/supportedsites.html">Unterst&uuml;tzte youtube-dl - Seiten</a><br>
  706. <br>
  707. </ul>
  708. <br>
  709. <a name="GOOGLECASTset" id="GOOGLECASTset"></a>
  710. <b>Set</b>
  711. <ul>
  712. <code>set &lt;name&gt; &lt;command&gt; [&lt;parameter&gt;]</code><br>
  713. Die folgenden Befehle sind definiert:<br><br>
  714. <ul>
  715. <li><code><b>play</b> URL</code> &nbsp;&nbsp;-&nbsp;&nbsp; Abspielen einer URL</li>
  716. <li><code><b>play</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; Abspielen im Sinne von Wiederaufnahme eines zuvor pausierten Abspielvorgangs</li>
  717. <li><code><b>playFavorite</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; spielt die URL aus den Favoriten favoriteURL_[1-5] ab</li>
  718. <li><code><b>stop</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; unterbricht den augenblicklichen Abspielvorgang</li>
  719. <li><code><b>pause</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; pause</li>
  720. <li><code><b>quitApp</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; schlie&szlig;t die gegenw&auml;rtige Applikation wie beispielsweise YouTube</li>
  721. <li><code><b>skip</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; unterbricht das gegenw&auml;rtige Kapitel bzw. Lied und springt zum N&auml;chsten</li>
  722. <li><code><b>rewind</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; springt zum Anfang des gegenw&auml;rtigen Kapitels bzw. Liedes</li>
  723. <li><code><b>displayWebsite</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; anzeigen einer Webseite auf Chromecast Video</li>
  724. </ul>
  725. <br>
  726. </ul>
  727. <a name="GOOGLECASTattr" id="GOOGLECASTattr"></a>
  728. <b>Attribute</b>
  729. <ul>
  730. <li><code><b>favoriteURL_[1-5]</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; Abspeichern von URL - Favoriten um mittels playFavorite [1-5] - Befehl abgespielt zu werden.</li>
  731. </ul>
  732. <br>
  733. <a name="GOOGLECASTget" id="GOOGLECASTget"></a>
  734. <b>Get</b>
  735. <ul>
  736. <code>n/a</code>
  737. </ul>
  738. <br>
  739. </ul>
  740. =end html_DE
  741. =cut