'Login to the matrix homeserver and return the client instance.'

def login(self):

    from matrix_client.client import MatrixRequestError
    client = None
    if (self.mx_id in self.auth_tokens):
        try:
            client = self.login_by_token()
            _LOGGER.debug('Logged    in    using    stored    token.')
        except MatrixRequestError as ex:
            _LOGGER.warning('Login    by    token    failed,    falling    back    to    password.    login_by_token    raised:    (%d)    %s', ex.code, ex.content)
    if (not client):
        try:
            client = self.login_by_password()
            _LOGGER.debug('Logged    in    using    password.')
        except MatrixRequestError as ex:
            _LOGGER.error('Login    failed,    both    token    and    username/password    invalid    login_by_password    raised:    (%d)    %s', ex.code, ex.content)
            raise
    return client
