1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
# VeSync Authentication Module
The VeSync Authentication Module provides a clean separation of authentication logic from the main VeSync class, offering improved maintainability, better error handling, and additional features like token persistence.
## Usage
Username and password must still be provided when instantiating the `VeSync` class, but a token can be loaded instead of logging in. If the loaded token is not valid, the `login()` method will be automatically called.
### Basic Username/Password Authentication
```python
import asyncio
from pyvesync import VeSync
async def main():
with VeSync(username="example@mail.com", password="password") as manager:
# Login
success = await manager.login()
if not success:
print("Login failed!")
return
print("Login successful!")
asyncio.run(main())
```
### Loading authentication data
The authentication data can be provided to arguments of the `set_credentials()` or `load_credentials_from_file()` methods of the instantiated `VeSync` object.
The credentials needed are: `token`, `account_id`, `country_code`, and `region`.
```python
import asyncio
from pyvesync import VeSync
async def main():
with VeSync(username="example@mail.com", password="password") as manager:
# Load credentials from a dictionary
credentials = {
"token": "your_token_here",
"account_id": "your_account_id_here",
"country_code": "US",
"region": "US"
}
success = await manager.set_credentials(**credentials)
# Or load from a file
await manager.load_credentials_from_file("path/to/credentials.json")
asyncio.run(main())
```
### Credential Storage
Credentials can be saved to a file or output as a json string. If no file path is provided the credentials will be saved to the users home directory as `.vesync_auth`.
The credentials file is a json file that has the keys `token`, `account_id`, `country_code`, and `region`.
```python
import asyncio
from pathlib import Path
from pyvesync import VeSync
async def main():
token_file = Path.home() / ".vesync_token"
with VeSync(username="example@mail.com", password="password") as manager:
# Login and save credentials to file
success = await manager.login(token_file_path=token_file)
if success:
# Save credentials to file
manager.save_credentials(token_file)
# Output credentials as json string
print(manager.output_credentials_json())
# Output credentials as dictionary
print(manager.output_credentials_dict())
print("Login successful and credentials saved!")
else:
print("Login failed!")
asyncio.run(main())
```
For a full list of methods and attributes, refer to the [auth](development/auth_api.md) and [vesync](development/vesync_api.md) documentation.
|