How to use the aioesphomeapi.core.APIConnectionError function in aioesphomeapi

To help you get started, we’ve selected a few aioesphomeapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github esphome / aioesphomeapi / aioesphomeapi / client.py View on Github external
async def connect(self, on_stop=None, login=False):
        if self._connection is not None:
            raise APIConnectionError("Already connected!")

        connected = False
        stopped = False

        async def _on_stop():
            nonlocal stopped

            if stopped:
                return
            stopped = True
            self._connection = None
            if connected and on_stop is not None:
                await on_stop()

        self._connection = APIConnection(self._params, _on_stop)
github esphome / aioesphomeapi / aioesphomeapi / connection.py View on Github external
def _check_connected(self) -> None:
        if not self._connected:
            raise APIConnectionError("Must be connected!")
github esphome / aioesphomeapi / aioesphomeapi / client.py View on Github external
self._connection = None
            if connected and on_stop is not None:
                await on_stop()

        self._connection = APIConnection(self._params, _on_stop)

        try:
            await self._connection.connect()
            if login:
                await self._connection.login()
        except APIConnectionError:
            await _on_stop()
            raise
        except Exception as e:
            await _on_stop()
            raise APIConnectionError(
                "Unexpected error while connecting: {}".format(e))

        connected = True
github esphome / aioesphomeapi / aioesphomeapi / connection.py View on Github external
async def login(self) -> None:
        self._check_connected()
        if self._authenticated:
            raise APIConnectionError("Already logged in!")

        connect = pb.ConnectRequest()
        if self._params.password is not None:
            connect.password = self._params.password
        resp = await self.send_message_await_response(connect, pb.ConnectResponse)
        if resp.invalid_password:
            raise APIConnectionError("Invalid password!")

        self._authenticated = True
github esphome / aioesphomeapi / aioesphomeapi / connection.py View on Github external
def _check_authenticated(self) -> None:
        if not self._authenticated:
            raise APIConnectionError("Must login first!")
github esphome / aioesphomeapi / aioesphomeapi / connection.py View on Github external
return
            if do_append(resp):
                responses.append(resp)
            if do_stop(resp):
                fut.set_result(responses)

        self._message_handlers.append(on_message)
        await self.send_message(send_msg)

        try:
            await asyncio.wait_for(fut, timeout)
        except asyncio.TimeoutError:
            if self._stopped:
                raise APIConnectionError(
                    "Disconnected while waiting for API response!")
            raise APIConnectionError("Timeout while waiting for API response!")

        try:
            self._message_handlers.remove(on_message)
        except ValueError:
            pass

        return responses
github esphome / aioesphomeapi / aioesphomeapi / connection.py View on Github external
async def connect(self) -> None:
        if self._stopped:
            raise APIConnectionError("Connection is closed!")
        if self._connected:
            raise APIConnectionError("Already connected!")

        try:
            coro = resolve_ip_address(self._params.eventloop, self._params.address,
                                      self._params.port)
            sockaddr = await asyncio.wait_for(coro, 30.0)
        except APIConnectionError as err:
            await self._on_error()
            raise err
        except asyncio.TimeoutError:
            await self._on_error()
            raise APIConnectionError("Timeout while resolving IP address")

        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.setblocking(False)
        self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

        _LOGGER.debug("%s: Connecting to %s:%s (%s)", self._params.address,
                      self._params.address, self._params.port, sockaddr)
        try:
            coro = self._params.eventloop.sock_connect(self._socket, sockaddr)
            await asyncio.wait_for(coro, 30.0)
        except OSError as err:
            await self._on_error()
            raise APIConnectionError(
                "Error connecting to {}: {}".format(sockaddr, err))
        except asyncio.TimeoutError:
            await self._on_error()
github esphome / aioesphomeapi / aioesphomeapi / client.py View on Github external
def _check_connected(self):
        if self._connection is None:
            raise APIConnectionError("Not connected!")
        if not self._connection.is_connected:
            raise APIConnectionError("Connection not done!")
github esphome / aioesphomeapi / aioesphomeapi / client.py View on Github external
def _check_authenticated(self):
        self._check_connected()
        if not self._connection.is_authenticated:
            raise APIConnectionError("Not authenticated!")
github esphome / aioesphomeapi / aioesphomeapi / util.py View on Github external
async def resolve_ip_address_getaddrinfo(eventloop: asyncio.events.AbstractEventLoop,
                                         host: str, port: int) -> Tuple[Any, ...]:
    try:
        res = await eventloop.getaddrinfo(host, port, family=socket.AF_INET,
                                          proto=socket.IPPROTO_TCP)
    except OSError as err:
        raise APIConnectionError("Error resolving IP address: {}".format(err))

    if not res:
        raise APIConnectionError("Error resolving IP address: No matches!")

    _, _, _, _, sockaddr = res[0]

    return sockaddr