titilambert / pyhydroquebec

DEPRECATED - Get your consumption data from your HydroQuebec account (www.hydroquebec.com)
Apache License 2.0
94 stars 44 forks source link

Index out of range #29

Closed b0ne closed 5 years ago

b0ne commented 5 years ago

Describe the bug Error when trying to get the data from HydroQuebec

To Reproduce The command pyhydroquebec -u username -p password (-j and witouth -j) return an error

Task exception was never retrieved future: <Task finished coro=<HydroQuebecClient.fetch_data() done, defined at /usr/local/lib/python3.5/dist-packages/pyhydroquebec/client.py:388> exception=IndexError('list index out of range',)> Traceback (most recent call last): File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step result = coro.send(None) File "/usr/local/lib/python3.5/dist-packages/pyhydroquebec/client.py", line 415, in fetch_data hourly_data = yield from self._get_hourly_data(day_date, p_p_id) File "/usr/local/lib/python3.5/dist-packages/pyhydroquebec/client.py", line 334, in _get_hourly_data hourly_weather_data = json_output['results'][0]['listeTemperaturesHeure'] IndexError: list index out of range

Seem linked to the fact that HydroQuebec dosn't register all the data yesterday

image

b0ne commented 5 years ago

@titilambert

Hi, I looked the code at the line 334 and it seem to try to get the value at position 0 of the array Json_Ouput['result'] and then get an error because the array seem to be empty. I modified the code to add a verification that the length of the arrary is >1 and if not then to do the same thing if the temperature data was missing.

if not json_output.get('results'):

Missing Temperature data from Hydro-Quebec (but don't crash the app for that)

hourly_weather_data = [None]*24 elif len(json_ouput['results'])<1:

Data from Hydro-Quebec isn't good (but don't crash the app for that)

hourly_weather_data = [None]*24 else: hourly_weather_data = json_output['results'][0]['listeTemperaturesHeure'] <

It seem to work for me!

titilambert commented 5 years ago

This issue seems already fixed, see here: https://github.com/titilambert/pyhydroquebec/blob/master/pyhydroquebec/client.py#L341 Could you try with the 2.4.0 ?

b0ne commented 5 years ago

I can confirm the the code you just linked dosn't work (event in 2.4.0). I added en else if if you look at the code I quoted!

You look if you get a result, but not if the result contain any data, for some rease the temperature was n/d today so the program got an array but an array of size 0 so when he try to look for the data at position 0 its throwing an out of bound.

titilambert commented 5 years ago

I really don't understand your patch :/ The issue should be handled by

if not json_output.get('results'):

This is really strange...

Could you add :

print(json_output):

To see the value of the variable ?

Thanks !

b0ne commented 5 years ago

The if not json_output.get('results') work if its returning None, but if it returning an empty array it will pass

titilambert commented 5 years ago

I'm really not sure of that. Could you try this command line ?

python3 -c "print('EMPTY') if not [] else print('NOT EMPTY')"
b0ne commented 5 years ago

python3 -c "print('EMPTY') if not [] else print('NOT EMPTY')"

EMPTY

b0ne commented 5 years ago

Here the print value : {'success': True, 'results': []}

So in this case i think the if not json_output.get('results')

Get the result from sucess : true and not the result []

titilambert commented 5 years ago

I'm closing this issue because of the Hydroquebec update see https://github.com/titilambert/pyhydroquebec/issues/35