lucien2k / sonoff-python

Make use of your sonoff smart switches without flashing them via the cloud APIs
MIT License
53 stars 32 forks source link

Exception has occurred: KeyError #22

Open keanuplayz opened 4 years ago

keanuplayz commented 4 years ago

First off, let's start off with displaying my code:

import sonoff

username = "usrname"
password = "pass"
api_region = "region"

s = sonoff.Sonoff(username, password, api_region)

devices = s.get_devices()
print(devices)

if devices:
    # We found a device, lets turn something on
    device_id = devices[0]['device id']
    s.switch('on', device_id, None)

This code was copied straight from the example, and modified to drop config, since it was making trouble on my end.

After running this, I get quite the abnormal error from the code.

Exception has occurred: KeyError
'at'

After delving into the source of this library, I found the exact line from where the exception originates. Line 123: self._bearer_token = resp['at'] at_error


I have absolutely no idea how to fix this, as I am not willing to make modifications to a library I don't understand, unless guided by someone who does.

If anyone can lend a hand with this, you'd have my sincere gratitude.

lucien2k commented 4 years ago

Are you using that exactly as displayed? If so then the region is invalid, which is I guess why its throwing that exception. Change region to 'eu', 'us' or 'cn' and it should stop throwing that error.

keanuplayz commented 4 years ago

Just ran it with exactly the same configuration: image

And the output was: image


Should I use single quotes, perhaps?

leejunweisg commented 4 years ago

Are you using that exactly as displayed? If so then the region is invalid, which is I guess why its throwing that exception. Change region to 'eu', 'us' or 'cn' and it should stop throwing that error.

I tried with my account and it said: "found new region: >>> as <<< (you should change api_region option to this value in configuration.yaml)". Upon changing region to 'as', it's also throwing that exception.

Any chance you'll add support for 'as' region? Or would you point me in the right direction and I'll modify the library myself? thanks!!

alBronko commented 2 years ago

I have the same issue. Was this ever fixed?

leejunweisg commented 2 years ago

I have the same issue. Was this ever fixed?

I believe I got it working for a period of time, but unfortunately I can no longer remember what I did back then.

I ended up flashing Tasmota on all my Sonoff devices and never looked back 👍🏻

alBronko commented 2 years ago

Thanks for the speedy response @leejunweisg. I was hoping not to flash anything if I can avoid it. If anyone has some advice it would be highly appreciated.

alBronko commented 2 years ago

When I look at resp after

 r = requests.post('https://{}-api.coolkit.cc:8080/api/user/login'.format(self._api_region), 
            headers=self._headers, json=app_details)
        resp = r.json()

I have {'error': 403, 'msg': 'Sign:oauthClient enable invalid'}. I just logged in to ewelink using the exact same credentials and had no issue, yet this doesn't work: s = sonoff.Sonoff("XXXX@XXX.com","XXXXX",'as') Thoughts?

limberger commented 2 years ago

I have some issue !

JacobSaugmann commented 2 years ago

Was anybody able to find a solution for this?

IgorWalkowiak commented 1 year ago

Just to save your time guys - error occurs due to usage of obsolete API. You can check how new one is used here https://github.com/AlexxIT/SonoffLAN/blob/master/custom_components/sonoff/core/ewelink/cloud.py

ardu1na commented 1 year ago

> Just to save your time guys - error occurs due to usage of obsolete API. You can check how new one is used here https://github.com/AlexxIT/SonoffLAN/blob/master/custom_components/sonoff/core/ewelink/cloud.py

thanks! that means that this sonoff-python is not working with the new API?

i'm getting the same

` self._bearer_token = resp['at']


KeyError: 'at'
`
IgorWalkowiak commented 1 year ago

Yep,

You still can fix it. xD

I did it 6 months ago, but that was very hardcoded solution and cost me definitely to much time so I would suggest to go and find something what works on that moment. If you want to go with fixing - as far as I remember I had to find new api endpoints and adjust response fields. Those should be in a code I sent in previous comment

ardu1na commented 1 year ago

thanks for the fast answer! yeah i'm just looking for the new api endopints.... but first, as you said, i should look for something that actually works