def get_rss_news_channels(self, call): """Load news channels of the for the selected category.""" if "rss_news_category" not in call.data: _LOGGER.error("No rss_news_category") return [] if call.data["rss_news_category"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.rss_news_channel", "options": [ais_global.G_EMPTY_OPTION] }) return ws_resp = self.cloud.audio_name(ais_global.G_AN_NEWS, call.data["rss_news_category"]) json_ws_resp = ws_resp.json() names = [ais_global.G_EMPTY_OPTION] self.news_channels = [] for item in json_ws_resp["data"]: names.append(item["NAME"]) self.news_channels.append(item) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.rss_news_channel", "options": names }) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == 'input_select.rss_news_category' and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(self.hass, 'input_select.rss_news_channel') self.hass.services.call('ais_ai_service', 'say_it', {"text": "Wybierz kanał wiadomości"})
def get_podcast_names(self, call): """Load podcasts names for the selected type.""" if "podcast_type" not in call.data: _LOGGER.error("No podcast_type") return [] if call.data["podcast_type"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.podcast_name", "options": [ais_global.G_EMPTY_OPTION] }) return ws_resp = self.cloud.audio_name(ais_global.G_AN_PODCAST, call.data["podcast_type"]) json_ws_resp = ws_resp.json() names = [ais_global.G_EMPTY_OPTION] self.podcast_names = [] for item in json_ws_resp["data"]: names.append(item["NAME"]) self.podcast_names.append(item) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.podcast_name", "options": names }) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == 'input_select.podcast_type' and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(self.hass, 'input_select.podcast_name') self.hass.services.call('ais_ai_service', 'say_it', {"text": "Wybierz audycję"})
def get_chapters(self, call): """Load chapters for the selected book.""" global G_SELECTED_TRACKS if ("book" not in call.data): _LOGGER.error("No book") return [] if call.data["book"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.book_chapter", "options": [ais_global.G_EMPTY_OPTION] }) return G_SELECTED_TRACKS = [] tracks = [] for ch in self.selected_books: if (ch["book"] == call.data["book"]): G_SELECTED_TRACKS.append(ch) tracks.append({"no": int(ch["track_no"]), "name": ch["name"]}) t = [ais_global.G_EMPTY_OPTION] tracks = sorted(tracks, key=itemgetter('no')) for st in tracks: t.append(st["name"]) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.book_chapter", "options": t }) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if ais_ai.CURR_ENTITIE == 'input_select.book_name': ais_ai.set_curr_entity(self.hass, 'input_select.book_chapter') self.hass.services.call('ais_ai_service', 'say_it', {"text": "Wybierz rozdział"})
def get_books(self, call): """Load books for the selected author.""" if ("author" not in call.data): _LOGGER.error("No author") return [] if call.data["author"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.book_name", "options": [ais_global.G_EMPTY_OPTION] }) return books = [ais_global.G_EMPTY_OPTION] self.selected_books = [] for chapters in self.all_gm_tracks: if (chapters["artist"] == call.data["author"]): self.selected_books.append(chapters) if (chapters["book"] not in books): books.append(chapters["book"]) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.book_name", "options": sorted(books) }) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if ais_ai.CURR_ENTITIE == 'input_select.book_autor': ais_ai.set_curr_entity(self.hass, 'input_select.book_name') self.hass.services.call('ais_ai_service', 'say_it', {"text": "Wybierz książkę"})
def get_chapters(self, call): """Load chapters for the selected book.""" if "id" not in call.data: _LOGGER.error("No book id") return state = self.hass.states.get("sensor.audiobookslist") attr = state.attributes track = attr.get(int(call.data["id"])) self.hass.states.async_set("sensor.audiobookslist", call.data["id"], attr) try: ws_resp = requests.get(track["lookup_url"] + "?format=json", timeout=10) data = ws_resp.json() except Exception as e: _LOGGER.error("Can't load chapters: " + str(e)) self.hass.services.call( "ais_ai_service", "say_it", {"text": "Nie można pobrać rozdziałów"} ) return list_info = {} list_idx = 0 for item in data["media"]: if item["type"] == "ogg": list_info[list_idx] = {} try: list_info[list_idx]["thumbnail"] = data["cover"] except Exception: list_info[list_idx]["thumbnail"] = data["simple_cover"] list_info[list_idx]["title"] = item["name"] list_info[list_idx]["name"] = item["name"] list_info[list_idx]["uri"] = item["url"] list_info[list_idx]["media_source"] = ais_global.G_AN_AUDIOBOOK_CHAPTER list_info[list_idx]["audio_type"] = ais_global.G_AN_AUDIOBOOK_CHAPTER list_info[list_idx]["icon"] = "mdi:play" list_idx = list_idx + 1 self.hass.states.async_set("sensor.audiobookschapterslist", 0, list_info) self.hass.services.call("ais_audiobooks_service", "select_chapter", {"id": 0}) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if ais_ai.CURR_ENTITIE == "sensor.audiobookslist": ais_ai.set_curr_entity(self.hass, "sensor.audiobookschapterslist") if len(list_info) > 0: self.hass.services.call( "ais_ai_service", "say_it", {"text": "Włączam pierwszy rozdział z " + str(len(list_info))}, ) else: self.hass.services.call( "ais_ai_service", "say_it", {"text": "Odtwarzam " + str(len(list_info))}, )
def get_radio_names(self, call): """Load stations of the for the selected type.""" if "radio_type" not in call.data: _LOGGER.error("No radio_type") return [] if call.data["radio_type"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.radio_station_name", "options": [ais_global.G_EMPTY_OPTION] }) return ws_resp = self.cache.audio_name(ais_global.G_AN_RADIO, call.data["radio_type"]) if ws_resp is None: ws_resp = self.cloud.audio_name(ais_global.G_AN_RADIO, call.data["radio_type"]) json_ws_resp = ws_resp.json() self.radio_names = [] names = [ais_global.G_EMPTY_OPTION] for item in json_ws_resp["data"]: names.append(item["NAME"]) self.radio_names.append(item) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.radio_station_name", "options": names }) # select the radio name if self.audio_name is not None: self.hass.block_till_done() self.hass.services.call( 'input_select', 'select_option', { "entity_id": "input_select.radio_station_name", "option": self.audio_name }) # this name will be set after the list refresh self.audio_name = None # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == 'input_select.radio_type' and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(self.hass, 'input_select.radio_station_name') self.hass.services.call('ais_ai_service', 'say_it', {"text": "Wybierz stację"})
def get_books(self, call): """Load books for the selected author.""" if "author" not in call.data: _LOGGER.error("No author") return [] if call.data["author"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.states.async_set("sensor.audiobookslist", -1, {}) self.hass.states.async_set("sensor.audiobookschapterslist", -1, {}) return list_info = {} list_idx = 0 for item in self.all_books: if item["author"] == call.data["author"]: list_info[list_idx] = {} try: list_info[list_idx]["thumbnail"] = ( "https://wolnelektury.pl/media/" + item["cover_thumb"]) except Exception: list_info[list_idx]["thumbnail"] = item["simple_thumb"] list_info[list_idx]["title"] = item["title"] list_info[list_idx]["name"] = item["title"] list_info[list_idx]["uri"] = item["url"] list_info[list_idx]["media_source"] = ais_global.G_AN_AUDIOBOOK list_info[list_idx]["audio_type"] = ais_global.G_AN_AUDIOBOOK list_info[list_idx]["icon"] = "mdi:book-play" list_info[list_idx]["lookup_url"] = item["href"] list_info[list_idx]["lookup_name"] = item["title"] list_idx = list_idx + 1 self.hass.states.async_set("sensor.audiobookslist", -1, list_info) import homeassistant.components.ais_ai_service as ais_ai if ais_ai.CURR_ENTITIE == "input_select.book_autor": ais_ai.set_curr_entity(self.hass, "sensor.audiobookslist") if ais_global.G_AIS_START_IS_DONE: self.hass.services.call( "ais_ai_service", "say_it", { "text": "Mamy " + str(len(list_info)) + " , wybierz książkę" }, )
def get_rss_news_items(self, call): import feedparser if "rss_news_channel" not in call.data: _LOGGER.error("No rss_news_channel") return if call.data["rss_news_channel"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.rss_news_item", "options": [ais_global.G_EMPTY_OPTION] }) return rss_news_channel = call.data["rss_news_channel"] if "lookup_url" in call.data: _lookup_url = call.data["lookup_url"] _image_url = call.data["image_url"] selected_by_voice_command = True else: # the news was selected from select list in app _lookup_url = None _image_url = None selected_by_voice_command = False for channel in self.news_channels: if channel["NAME"] == rss_news_channel: _lookup_url = channel["LOOKUP_URL"] _image_url = channel["IMAGE_URL"] if _lookup_url is not None: # download the episodes self.hass.services.call('ais_ai_service', 'say_it', {"text": "pobieram"}) try: d = feedparser.parse(_lookup_url) items = [ais_global.G_EMPTY_OPTION] self.news_items = [] for e in d.entries: item = { 'title': e.title, 'link': e.link, 'image_url': _image_url, 'description': e.description } if e.title not in items: items.append(e.title) self.news_items.append(item) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.rss_news_item", "options": items }) if selected_by_voice_command: item = self.news_items[0] self.hass.services.call( 'ais_ai_service', 'say_it', { "text": "mamy " + str(len(d.entries)) + " wiadomości z " + rss_news_channel + ", czytam najnowszy artykuł: " + item["title"] }) self.hass.services.call( 'input_select', 'select_option', { "entity_id": "input_select.rss_news_item", "option": item["title"] }) else: self.hass.services.call( 'ais_ai_service', 'say_it', { "text": "mamy " + str(len(d.entries)) + " wiadomości, wybierz artykuł" }) # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == 'input_select.rss_news_channel' and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(self.hass, 'input_select.rss_news_item') except Exception as e: _LOGGER.error("Error: " + str(e)) self.hass.services.call('ais_ai_service', 'say_it', { "text": "Nie można pobrać wiadomości z: " + rss_news_channel })
def get_podcast_tracks(self, call): import feedparser if "podcast_name" not in call.data: _LOGGER.error("No podcast_name") return if call.data["podcast_name"] == ais_global.G_EMPTY_OPTION: # reset status for item below self.hass.services.call( 'input_select', 'set_options', { "entity_id": "input_select.podcast_track", "options": [ais_global.G_EMPTY_OPTION] }) return podcast_name = call.data["podcast_name"] if "lookup_url" in call.data: _lookup_url = call.data["lookup_url"] _image_url = call.data["image_url"] selected_by_voice_command = True else: # the podcast was selected from select list in app _lookup_url = None _image_url = None selected_by_voice_command = False for podcast in self.podcast_names: if podcast["NAME"] == podcast_name: _lookup_url = podcast["LOOKUP_URL"] _image_url = podcast["IMAGE_URL"] if _lookup_url is not None: # download the episodes self.hass.services.call('ais_ai_service', 'say_it', {"text": "Pobieram"}) try: d = feedparser.parse(check_url(_lookup_url)) tracks = [ais_global.G_EMPTY_OPTION] self.podcast_tracks = [] for e in d.entries: track = {'title': e.title, 'link': e.enclosures[0]} try: track['image_url'] = d.feed.image.href except Exception: track['image_url'] = _image_url tracks.append(e.title) self.podcast_tracks.append(track) self.hass.services.call('input_select', 'set_options', { "entity_id": "input_select.podcast_track", "options": tracks }) if selected_by_voice_command: track = self.podcast_tracks[0] self.hass.services.call( 'ais_ai_service', 'say_it', { "text": "Pobrano " + str(len(d.entries)) + " odcinków" + ", audycji " + podcast_name + ", włączam najnowszy odcinek: " + track["title"] }) self.hass.services.call( 'input_select', 'select_option', { "entity_id": "input_select.podcast_track", "option": track["title"] }) else: # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == 'input_select.podcast_name' and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(self.hass, 'input_select.podcast_track') self.hass.services.call( 'ais_ai_service', 'say_it', { "text": "Pobrano " + str(len(d.entries)) + " odcinków, wybierz odcinek" }) else: self.hass.services.call( 'ais_ai_service', 'say_it', { "text": "Pobrano " + str(len(d.entries)) + " odcinków" + ", audycji " + podcast_name }) except Exception as e: _LOGGER.error("Error: " + str(e)) self.hass.services.call( 'ais_ai_service', 'say_it', {"text": "Nie można pobrać odcinków. " + podcast_name})
async def async_process_search(self, call): """Search in service.""" query = None prev_page_token = None next_page_token = None text = None if ATTR_QUERY in call.data: query = call.data[ATTR_QUERY] if "prevPageToken" in call.data: if call.data["prevPageToken"] is not None: prev_page_token = call.data["prevPageToken"].replace( "prevPageToken_", "") if "nextPageToken" in call.data: if call.data["nextPageToken"] is not None: next_page_token = call.data["nextPageToken"].replace( "nextPageToken_", "") if ((query is None or len(query.strip()) == 0) and prev_page_token is None and next_page_token is None): # get tracks from favorites await self.hass.services.async_call( "ais_bookmarks", "get_favorites", {"audio_source": ais_global.G_AN_MUSIC}, ) return if self.yt_key is None: try: json_ws_resp = await aisCloud.async_key("ytsearch") self.yt_key = json_ws_resp["key"] except Exception as e: ais_global.G_OFFLINE_MODE = True await self.hass.services.async_call( "ais_ai_service", "say_it", { "text": "Brak odpowiedzi, sprawdź połączenie z Intenetem" }, ) _LOGGER.error("process_search_async " + str(e)) return params = dict( order="relevance", part="snippet", key=self.yt_key, maxResults=10, type="video", fields= "items/id/videoId, items/snippet/title, items/snippet/thumbnails/medium/url, " "pageInfo/totalResults, nextPageToken, prevPageToken", ) params.update({"q": query}) if prev_page_token is not None: params.update({"pageToken": prev_page_token}) if next_page_token is not None: params.update({"pageToken": next_page_token}) data = requests.get(URL_BASE, params=params).json() # check if error if "error" in data: for error in data["error"]["errors"]: if error["reason"] == "quotaExceeded": # get the new token and try again await self.async_get_new_key(self.yt_key, query, prev_page_token, next_page_token) return if text is None: text = error["message"] else: text = text + " " + error["message"] await self.hass.services.async_call("ais_ai_service", "say_it", {"text": text}) return list_info = {} list_idx = 0 total_results = 0 if "pageInfo" in data: total_results = data["pageInfo"]["totalResults"] if "prevPageToken" in data: list_info[list_idx] = {} list_info[list_idx][ "title"] = "pobierz poprzednią stronę wyników wyszukiwania" list_info[list_idx][ "name"] = "pobierz poprzednią stronę wyników wyszukiwania" list_info[list_idx][ "thumbnail"] = "/static/icons/favicon-100x100.png" list_info[list_idx][ "uri"] = "prevPageToken_" + data["prevPageToken"] list_info[list_idx]["media_source"] = ais_global.G_AN_MUSIC list_info[list_idx]["audio_type"] = ais_global.G_AN_MUSIC list_info[list_idx]["icon"] = "mdi:page-previous" list_info[list_idx]["query"] = query list_idx = list_idx + 1 if "items" in data: for item in data["items"]: list_info[list_idx] = {} list_info[list_idx]["title"] = item["snippet"]["title"] list_info[list_idx]["name"] = item["snippet"]["title"] # item['snippet']['description'] list_info[list_idx]["thumbnail"] = item["snippet"][ "thumbnails"]["medium"]["url"] list_info[list_idx]["uri"] = item["id"]["videoId"] list_info[list_idx]["media_source"] = ais_global.G_AN_MUSIC list_info[list_idx]["audio_type"] = ais_global.G_AN_MUSIC list_info[list_idx]["icon"] = "mdi:play" list_idx = list_idx + 1 if "nextPageToken" in data: list_info[list_idx] = {} list_info[list_idx][ "title"] = "pobierz następną stronę wyników wyszukiwania" list_info[list_idx][ "name"] = "pobierz następną stronę wyników wyszukiwania" list_info[list_idx][ "thumbnail"] = "/static/icons/favicon-100x100.png" list_info[list_idx][ "uri"] = "nextPageToken_" + data["nextPageToken"] list_info[list_idx]["media_source"] = ais_global.G_AN_MUSIC list_info[list_idx]["audio_type"] = ais_global.G_AN_MUSIC list_info[list_idx]["icon"] = "mdi:page-next" list_info[list_idx]["query"] = query # update list self.hass.states.async_set("sensor.youtubelist", -1, list_info) if len(list_info) > 0: # from remote import homeassistant.components.ais_ai_service as ais_ai if (ais_ai.CURR_ENTITIE == "input_text.ais_music_query" and ais_ai.CURR_BUTTON_CODE == 4): ais_ai.set_curr_entity(self.hass, "sensor.youtubelist") ais_ai.CURR_ENTITIE_ENTERED = True text = "Znaleziono: %s, wybierz pozycję którą mam włączyć" % ( str(total_results)) else: if next_page_token is None and prev_page_token is None: text = "Znaleziono: {}, włączam pierwszy: {}".format( str(total_results), list_info[0]["title"]) await self.hass.services.async_call( "ais_yt_service", "select_track_uri", {"id": 0}) if ais_ai.CURR_ENTITIE == "media_player.wbudowany_glosnik": if next_page_token is not None and ais_ai.CURR_BUTTON_CODE == 22: text = ( "Pobrano następną stronę wyników, włączam pierwszy: %s" % (list_info[1]["title"])) await self.hass.services.async_call( "ais_yt_service", "select_track_uri", {"id": 1}) elif prev_page_token is not None and ais_ai.CURR_BUTTON_CODE == 21: text = ( "Pobrano poprzednią stronę wyników, włączam ostatni: %s" % (list_info[len(list_info) - 2]["title"])) await self.hass.services.async_call( "ais_yt_service", "select_track_uri", {"id": len(list_info) - 2}, ) else: text = "Brak wnyników na YouTube dla zapytania %s" % query # info to user if text is not None: await self.hass.services.async_call("ais_ai_service", "say_it", {"text": text})
def get_favorites_service(call): """Return the list of favorites to app list""" audio_source = None if "audio_source" in call.data: audio_source = call.data["audio_source"] d = hass.data[DOMAIN] list_info = {} list_idx = 0 for item in reversed(d.favorites): if audio_source is None or audio_source == item["source"]: if ("media_stream_image" in item and item["media_stream_image"] is not None): img = item["media_stream_image"] else: img = "/static/icons/tile-win-310x150.png" list_info[list_idx] = {} list_info[list_idx]["title"] = item["name"] if item["name"].startswith(item["source"]): list_info[list_idx]["name"] = item["name"] else: list_info[list_idx]["name"] = ( ais_global.G_NAME_FOR_AUDIO_NATURE.get( item["source"], item["source"]) + " " + item["name"]) list_info[list_idx]["thumbnail"] = img list_info[list_idx]["uri"] = item["media_content_id"] list_info[list_idx]["audio_type"] = item["source"] list_info[list_idx][ "icon_type"] = ais_global.G_ICON_FOR_AUDIO.get( item["source"], "mdi:play") list_info[list_idx]["icon_remove"] = "mdi:delete-forever" list_info[list_idx]["editable"] = True if audio_source == ais_global.G_AN_PODCAST: list_info[list_idx]["icon"] = "mdi:podcast" else: list_info[list_idx]["icon"] = "mdi:play" list_info[list_idx]["id"] = item["id"] list_idx = list_idx + 1 # create lists if audio_source is None: # get all items hass.states.async_set("sensor.aisfavoriteslist", -1, list_info) else: # check if the change was done form remote import homeassistant.components.ais_ai_service as ais_ai if audio_source == ais_global.G_AN_RADIO: hass.states.async_set("sensor.radiolist", -1, list_info) if (ais_ai.CURR_ENTITIE == "input_select.radio_type" and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(hass, "sensor.radiolist") hass.async_add_job( hass.services.async_call("ais_ai_service", "say_it", {"text": "Wybierz stację"})) elif audio_source == ais_global.G_AN_PODCAST: hass.states.async_set("sensor.podcastnamelist", -1, list_info) if (ais_ai.CURR_ENTITIE == "input_select.podcast_type" and ais_ai.CURR_BUTTON_CODE == 23): ais_ai.set_curr_entity(hass, "sensor.podcastnamelist") hass.async_add_job( hass.services.async_call("ais_ai_service", "say_it", {"text": "Wybierz audycję"})) elif audio_source == ais_global.G_AN_MUSIC: hass.states.async_set("sensor.youtubelist", -1, list_info) elif audio_source == ais_global.G_AN_SPOTIFY: hass.states.async_set("sensor.spotifylist", -1, list_info) elif audio_source == ais_global.G_AN_AUDIOBOOK: hass.states.async_set("sensor.audiobookschapterslist", -1, list_info)