Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of simple usage in Python #388

Closed
arthurlapertosa opened this issue Oct 5, 2022 · 2 comments
Closed

Example of simple usage in Python #388

arthurlapertosa opened this issue Oct 5, 2022 · 2 comments

Comments

@arthurlapertosa
Copy link

arthurlapertosa commented Oct 5, 2022

Hello! I'd like to use this library to integrate with my program built on top of the original https://github.com/sampsyo/wideq. I have devices from api v2 and that's why I'm trying to migrate to this library.
I saw you don't have an example.py like the wideq library has, so I tried to create a snippet to make it get information from my AC, but I couldn't make it work.
The snippet is:

from wideq.core_async import ClientAsync, CoreAsync, lg_client_session
from wideq.ac import AirConditionerDevice
import asyncio

async def get_login():
    core = CoreAsync(country='BR', language='pt-BR')
    gateway = await core.gateway_info()
    auth = await ClientAsync.oauth_info_from_url('(I pasted the callback url here)')
    user_number = await core.get_user_number(auth['oauth_url'], auth['access_token'])
    auth['user_number'] = user_number
    client = ClientAsync.load({'auth': auth, 'gateway': gateway, 'session': lg_client_session(), 'country': 'BR', 'language': 'pt-BR'})
    await client.refresh_devices()

    device = client.get_device('844e329b-d43b-564a5-bsa5-1c39291e6106')
    ac = AirConditionerDevice(client, device)

    resp = await ac.poll()
    print(resp)

asyncio.run(get_login())

When I run this code, it returns a 'Not logged in error' (in the client.refresh_devices() call). Could you please help me to get this working, or maybe point me out some python script with a basic usage of the wideq library, please?

@arthurlapertosa
Copy link
Author

I could solve the problem by logging in and getting the client with client = await ClientAsync.from_login(EMAIL, PASSWORD).
The working snippet is:

async def get_login():
    client = await ClientAsync.from_login(EMAIL, PASSWORD, country='BR', language='pt-BR')

    device = client.get_device('844e329b-d43b-564a5-bsa5-1c39291e6106')
    ac = AirConditionerDevice(client, device)

    while True:
        time.sleep(1)
        resp = await ac.poll()
        print(f'On: {resp.is_on}; Cur temp: {resp.current_temp}; Targ temp: {resp.target_temp}; Power: {resp.energy_current}')
        
asyncio.run(get_ac_info())

@pifou25
Copy link

pifou25 commented Apr 2, 2023

Hello @arthurlapertosa
I also tried to build a simple example.py like yours but there is no function from_login did you use some like the get_oauth_info_from_login but seems specific to HAss... what else ? Did you make some fork with a public example ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants