Exemple #1
0
    async def get_host(self):
        """Get the hostname/IP of the WiFi unit."""
        if self.host is not None:
            self._wifi_host = self.host
        else:
            import async_timeout

            for host in WIFIHOSTS:
                try:
                    if self.loop is None:
                        self.loop = gdh_loop()
                    if self.session is None:
                        self.session = gdh_session()
                    url = API.format(schema="http",
                                     host=host,
                                     port="",
                                     endpoint=self.endpoint)
                    async with async_timeout.timeout(5, loop=self.loop):
                        await self.session.get(url)
                        self._wifi_host = host
                        return self._wifi_host
                # pylint: disable=W0703
                except Exception:
                    self._wifi_host = self._wifi_host
        return self._wifi_host
Exemple #2
0
 async def get_alarm_volume(self):
     """Get the alarm volume for the device."""
     endpoint = '/setup/assistant/alarms/volume'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             response = await self._session.post(url, headers=HEADERS)
             self._alarmvolume = await response.json()
     except (asyncio.TimeoutError,
             aiohttp.ClientError, socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
Exemple #3
0
 async def get_scan_result(self):
     """Scan for bluetooth devices."""
     endpoint = '/setup/bluetooth/scan_results'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             response = await self._session.get(url)
             self._devices = await response.json()
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
Exemple #4
0
 async def set_discovery_enabled(self):
     """Enable bluetooth discoverablility."""
     endpoint = '/setup/bluetooth/discovery'
     data = {"enable_discovery": True}
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             response = await self._session.post(url,
                                                 headers=HEADERS,
                                                 data=json.dumps(data))
             _LOGGER.debug(response.status)
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
Exemple #5
0
 async def set_eureka_info(self, data):
     """Set eureka info."""
     endpoint = '/setup/set_eureka_info'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     returnvalue = False
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             result = await self._session.post(url,
                                               json=data,
                                               headers=HEADERS)
             if result.status == 200:
                 returnvalue = True
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
     return returnvalue
Exemple #6
0
 async def scan_for_devices(self, sleep=5):
     """Scan for bluetooth devices."""
     endpoint = '/setup/bluetooth/scan'
     data = {"enable": True, "clear_results": True, "timeout": 5}
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     await self.set_discovery_enabled()
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             response = await self._session.post(url,
                                                 headers=HEADERS,
                                                 data=json.dumps(data))
             _LOGGER.debug(response.status)
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
     time.sleep(sleep)
Exemple #7
0
 async def reboot(self):
     """Reboot the device."""
     endpoint = '/setup/reboot'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     data = {'params': 'now'}
     returnvalue = False
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             result = await self._session.post(url,
                                               json=data,
                                               headers=HEADERS)
             if result.status == 200:
                 returnvalue = True
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
     return returnvalue
Exemple #8
0
 async def set_alarm_volume(self, volume):
     """Set the alarm volume for the device."""
     data = {'volume': volume}
     endpoint = '/setup/assistant/alarms/volume'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     returnvalue = False
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             response = await self._session.post(url,
                                                 json=data,
                                                 headers=HEADERS)
             if response.status == 200:
                 returnvalue = True
     except (asyncio.TimeoutError,
             aiohttp.ClientError, socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
     return returnvalue
Exemple #9
0
 async def control_notifications(self, active):
     """Set control_notifications option."""
     endpoint = '/setup/set_eureka_info'
     url = API.format(ip=self._ipaddress, endpoint=endpoint)
     value = 1 if active else 2
     data = {'settings': {'control_notifications': value}}
     returnvalue = False
     try:
         async with async_timeout.timeout(5, loop=self._loop):
             result = await self._session.post(url,
                                               json=data,
                                               headers=HEADERS)
             if result.status == 200:
                 returnvalue = True
     except (asyncio.TimeoutError, aiohttp.ClientError,
             socket.gaierror) as error:
         _LOGGER.error('Error connecting to googledevices, %s', error)
     return returnvalue
Exemple #10
0
    async def get_clients(self):
        """Return clients form the network."""
        import requests
        import asyncio
        import aiohttp
        from socket import gaierror

        self._clients = []
        if self.info.wifi_host is None:
            async with self.session:
                await self.info.get_host()
        if self.info.wifi_host is None:
            await log.error("Host is 'None', host can not be 'None'")
            return self._clients
        endpoint = WIFIAPIPREFIX + "diagnostic-report"
        url = API.format(schema="http",
                         host=self.info.wifi_host,
                         port=":80",
                         endpoint=endpoint)
        try:
            response = requests.request("GET", url)
            all_clients = response.text
            all_clients = all_clients.split("/proc/net/arp")[1]
            all_clients = all_clients.split("/proc/slabinfo")[0]
            all_clients = all_clients.splitlines()
            for device in all_clients[1:-2]:
                host = device.split()[0]
                mac = device.split()[3]
                info = {"ip": host, "mac": mac}
                self._clients.append(info)
        except (TypeError, KeyError, IndexError) as error:
            msg = "Error parsing information - {}".format(error)
            log.error(msg)
        except (
                asyncio.TimeoutError,
                aiohttp.ClientError,
                gaierror,
                asyncio.CancelledError,
        ) as error:
            msg = "{} - {}".format(url, error)
            log.error(msg)
        except Exception as error:  # pylint: disable=W0703
            log.error(error)
        return self._clients
Exemple #11
0
async def gdh_request(
    host,
    schema=None,
    port=None,
    token=None,
    endpoint=None,
    json=True,
    session=None,
    loop=None,
    headers=None,
    data=None,
    json_data=None,
    params=None,
    method="get",
):
    """Web request."""
    import asyncio
    import aiohttp
    import async_timeout
    from socket import gaierror
    from googledevices.utils.const import API
    import googledevices.utils.log as log

    if schema is None:
        schema = "http"
    if port is not None:
        port = ":{port}".format(port=port)
    else:
        port = ""
    url = API.format(schema=schema, host=host, port=port, endpoint=endpoint)
    result = None
    if token is not None:
        if headers is None:
            headers = {}
        headers["cast-local-authorization-token"] = token

    if session is None:
        session = gdh_session()
    if loop is None:
        loop = gdh_loop()
    try:
        async with async_timeout.timeout(8, loop=loop):
            if method == "post":
                webrequest = await session.post(
                    url, json=json_data, data=data, params=params, headers=headers, ssl=False
                )
            else:
                webrequest = await session.get(
                    url, json=json_data, data=data, params=params, headers=headers, ssl=False
                )
            if json:
                result = await webrequest.json()
            else:
                result = webrequest
    except (TypeError, KeyError, IndexError) as error:
        log.error("Error parsing information - {}".format(error))
    except asyncio.TimeoutError:
        log.error("Timeout contacting {}".format(url))
    except asyncio.CancelledError:
        log.error("Cancellation error contacting {}".format(url))
    except aiohttp.ClientError as error:
        log.error("ClientError contacting {} - {}".format(url, error))
    except gaierror as error:
        log.error("I/O error contacting {} - {}".format(url, error))
    except Exception as error:  # pylint: disable=W0703
        log.error("Unexpected error contacting {} - {}".format(url, error))
    return result