def request_call(self, url): """Call web API request.""" uri = 'http://' + self._host + ":" + str(self._port) + url _LOGGER.debug("Enigma: [request_call] - Call request %s ", uri) try: return self._opener.open(uri, timeout=10).read().decode('UTF8') except (HTTPError, URLError, ConnectionRefusedError): _LOGGER.exception("Enigma: [request_call] - Error connecting to \ remote enigma %s: %s ", self._host, HTTPError.code) return False
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up platform.""" """Initialize the Enigma device.""" devices = [] enigma_list = hass.data[ENIGMA_DOMAIN] for device in enigma_list: _LOGGER.debug("Configured a new EnigmaMediaPlayer %s", device.get_host) devices.append(EnigmaMediaPlayer(device)) async_add_entities(devices, update_before_add=True)
async def request_call(self, url): """Call web API request.""" uri = 'http://' + self._host + ":" + str(self._port) + url _LOGGER.debug("Enigma: [request_call] - Call request %s ", uri) # Check if is password enabled if self._password is not None: # Handle HTTP Auth async with self._opener.get(uri, auth=aiohttp.BasicAuth(self._username, self._password)) as resp: text = await resp.read() return text else: async with self._opener.get(uri) as resp: text = await resp.read() return text
def async_send_message(self, message="", **kwargs): """Send message.""" try: displaytime = DEFAULT_DISPLAY_TIME messagetype = DEFAULT_MESSAGE_TYPE data = kwargs.get(ATTR_DATA) or {} if data: if 'displaytime' in data: displaytime = data['displaytime'] if 'messagetype' in data: messagetype = data['messagetype'] _LOGGER.debug("Enigma notify service: [async_send_message] - Sending Message %s \ (timeout=%s and type=%s", message, displaytime, messagetype) self.request_call('/web/message?text=' + message.replace(" ", "%20") + '&type=' + messagetype + '&timeout=' + displaytime) except ImportError: _LOGGER.error("Enigma notify service: [Exception raised]")
async def load_sources(self): """Initialize the Enigma device loading the sources.""" from bs4 import BeautifulSoup if self._bouquet: # Load user set bouquet. _LOGGER.debug("Enigma: [load_sources] - Request user bouquet %s ", self._bouquet) epgbouquet_xml = await self.request_call( '/web/epgnow?bRef=' + urllib.parse.quote_plus(self._bouquet)) # Channels name soup = BeautifulSoup(epgbouquet_xml, 'html.parser') src_names = soup.find_all('e2eventservicename') self._source_names = [src_name.string for src_name in src_names] # Channels reference src_references = soup.find_all('e2eventservicereference') sources = [ src_reference.string for src_reference in src_references ] self._sources = dict(zip(self._source_names, sources)) else: # Load sources from first bouquet. reference = urllib.parse.quote_plus(await self.get_bouquet_reference()) _LOGGER.debug("Enigma: [load_sources] - Request reference %s ", reference) epgbouquet_xml = await self.request_call('/web/epgnow?bRef=' + reference) # Channels name soup = BeautifulSoup(epgbouquet_xml, 'html.parser') src_names = soup.find_all('e2eventservicename') self._source_names = [src_name.string for src_name in src_names] # Channels reference src_references = soup.find_all('e2eventservicereference') sources = [ src_reference.string for src_reference in src_references ] self._sources = dict(zip(self._source_names, sources))
def async_select_source(self, source): """Select input source.""" _LOGGER.debug("Enigma: [async_select_source] - Change source channel") self.request_call('/web/zap?sRef=' + self._sources[source])
def media_image_url(self): """Title of current playing media.""" _LOGGER.debug("Enigma: [media_image_url] - %s", self._picon_url) return self._picon_url
def update(self): """Import BeautifulSoup.""" from bs4 import BeautifulSoup # Get the latest details from the device. _LOGGER.info("Enigma: [update] - request for host %s (%s)", self._host, self._name) powerstate_xml = self.request_call('/web/powerstate') powerstate_soup = BeautifulSoup(powerstate_xml, 'html.parser') pwstate = powerstate_soup.e2instandby.renderContents().decode('UTF8') self._pwstate = '' _LOGGER.debug("Enigma: [update] - Powerstate for host %s = %s", self._host, pwstate) if pwstate.find('false') >= 0: self._pwstate = 'false' if pwstate.find('true') >= 0: self._pwstate = 'true' # If name was not defined, get the name from the box if self._name == 'Enigma2 Satelite': about_xml = self.request_call('/web/about') soup = BeautifulSoup(about_xml, 'html.parser') name = soup.e2model.renderContents().decode('UTF8') _LOGGER.debug("Enigma: [update] - Name for host %s = %s", self._host, name) if name: self._name = name # If powered on if self._pwstate == 'false': subservices_xml = self.request_call('/web/subservices') soup = BeautifulSoup(subservices_xml, 'html.parser') servicename = soup.e2servicename.renderContents().decode('UTF8') reference = soup.e2servicereference.renderContents().decode('UTF8') eventtitle = 'N/A' # If we got a valid reference, check the title of the event and # the picon url if reference != '' and reference != 'N/A' and \ not reference.startswith('1:0:0:0:0:0:0:0:0:0:'): xml = self.request_call('/web/epgservicenow?sRef=' + reference) soup = BeautifulSoup(xml, 'html.parser') eventtitle = soup.e2eventtitle.renderContents().decode('UTF8') if self._password != DEFAULT_PASSWORD: # if icon = screenhost then get screenshot if self._picon == 'screenshot': self._picon_url = 'http://' + \ self._username + ':' + \ self._password + \ '@' + self._host + ':' + \ str(self._port) + '/grab?format=png\ &r=720&mode=all&reference=' + \ reference.replace(":", "_")[:-1] # otherwise try to get picon else: self._picon_url = 'http://' + \ self._username + ':' + \ self._password + \ '@' + self._host + ':' + \ str(self._port) + '/picon/' + \ reference.replace(":", "_")[:-1] \ + '.png' else: # if icon = screenhost then get screenshot if self._picon == 'screenshot': self._picon_url = 'http://' + \ self._username + ':' + \ self._password + \ '@' + self._host + ':' + \ str(self._port) + '/grab?format=png\ &r=720&mode=all&reference=' + \ reference.replace(":", "_")[:-1] # otherwise try to get picon else: self._picon_url = 'http://' + self._host + ':' + \ str(self._port) + '/picon/' + \ reference.replace(":", "_")[:-1] \ + '.png' _LOGGER.debug("Enigma: [update] - Eventtitle for host %s = %s", self._host, eventtitle) # Check volume and if is muted and update self variables volume_xml = self.request_call('/web/vol') soup = BeautifulSoup(volume_xml, 'html.parser') volcurrent = soup.e2current.renderContents().decode('UTF8') volmuted = soup.e2ismuted.renderContents().decode('UTF8') self._volume = int(volcurrent) / MAX_VOLUME if volcurrent else None self._muted = (volmuted == 'True') if volmuted else None _LOGGER.debug("Enigma: [update] - Volume for host %s = %s", self._host, volcurrent) _LOGGER.debug("Enigma: [update] - Is host %s muted = %s", self._host, volmuted) # Concatenate Channel and Title name to display self._selected_source = (servicename + ' - ' + eventtitle) return True
async def async_media_pause(self): """Send media pause command to media player.""" _LOGGER.debug("Enigma: [pause] - Does nothing")
async def async_media_play(self): """Send play command.""" _LOGGER.debug("Enigma: [play] - Does nothing")
async def async_media_play_pause(self): """Simulate play pause media player.""" _LOGGER.debug("Enigma: [play_pause_toogle] - Does nothing")