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

Prevent crash where createdAt is not returned #9

Merged
merged 1 commit into from Jul 27, 2020

Conversation

boc-the-git
Copy link
Contributor

I will admit this isn't extensively tested because I don't know the overall solution too well, but I can confirm this fixes the issue as reported here: https://community.home-assistant.io/t/amber-electric-australia-custom-component/201231/18

@stuarts0011
Copy link

Hi boc - This did not resolve the problem for me? I'm still getting exactly the same error log.

@boc-the-git
Copy link
Contributor Author

boc-the-git commented Jul 20, 2020 via email

@stuarts0011
Copy link

stuarts0011 commented Jul 25, 2020 via email

@boc-the-git
Copy link
Contributor Author

boc-the-git commented Jul 25, 2020

2069 Sometimes it will load the platform, sometimes not.

I've tested with 2069 and no issue. As you say it sometimes works and sometimes doesn't, can you look to supply more info next time it crashes? Without more info, I can only assume it's unrelated.

It would also be useful to get the output from this.. please create a python script with this and run it:

import requests
import json

URL = "https://api.amberelectric.com.au/prices/listprices"

response = requests.post(URL, '{"postcode":"2069"}')

print(response.content)

@stuarts0011
Copy link

If the error exists on reboot, it won't load the platform as here:

"Log Details (ERROR)
Logger: homeassistant.components.sensor
Source: custom_components/amberelectric/ambermodel.py:41
Integration: Sensor (documentation, issues)
First occurred: 17:54:18 (1 occurrences)
Last logged: 17:54:18

Error while setting up amberelectric platform for sensor
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
return fut.result()
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "/config/custom_components/amberelectric/sensor.py", line 45, in setup_platform
AmberPricingSensor(amber_data, postcode, CONST_SOLARFIT, "Amber solar feed in tariff", "mdi:solar-power"),
File "/config/custom_components/amberelectric/sensor.py", line 62, in init
self.update()
File "/usr/src/homeassistant/homeassistant/util/init.py", line 239, in wrapper
result = method(*args, **kwargs)
File "/config/custom_components/amberelectric/sensor.py", line 157, in update
self.amber_data = AmberData.from_dict(json.loads(response.text))
File "/config/custom_components/amberelectric/ambermodel.py", line 437, in from_dict
data = Data.from_dict(obj.get("data"))
File "/config/custom_components/amberelectric/ambermodel.py", line 400, in from_dict
VariablePricesAndRenewable.from_dict, obj.get("variablePricesAndRenewables")
File "/config/custom_components/amberelectric/ambermodel.py", line 51, in from_list
return [f(y) for y in x]
File "/config/custom_components/amberelectric/ambermodel.py", line 51, in
return [f(y) for y in x]
File "/config/custom_components/amberelectric/ambermodel.py", line 315, in from_dict
created_at = from_datetime(obj.get("createdAt"))
File "/config/custom_components/amberelectric/ambermodel.py", line 41, in from_datetime
return dateutil.parser.parse(x)
File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 1374, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 646, in parse
res, skipped_tokens = self._parse(timestr, **kwargs)
File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 725, in _parse
l = _timelex.split(timestr) # Splits the timestr into tokens
File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 207, in split
return list(cls(s))
File "/usr/local/lib/python3.7/site-packages/dateutil/parser/_parser.py", line 76, in init
'{itype}'.format(itype=instream.class.name))
TypeError: Parser must be a string or character stream, not NoneType
Connection lost. Reconnecting…"

If it happens after successfully loading the platform, it just doesn't return data on that request.

@stuarts0011
Copy link

The output from your python above is as expected.

@boc-the-git
Copy link
Contributor Author

Thanks for the extra information @stuarts0011

What I can infer from that is you are not running my code. I guess this is why you are getting the exact same (to the line of code) error message as my original error - because it was before my code change.

Your error message referencing line 315 of ambermodel.py is referencing the current/original code, not my modified code. If you update your ambermodel.py as per my change (see the 'Files changed' tab), you should no longer get the error.

@stuarts0011
Copy link

Thanks for the extra information @stuarts0011

What I can infer from that is you are not running my code. I guess this is why you are getting the exact same (to the line of code) error message as my original error - because it was before my code change.

Your error message referencing line 315 of ambermodel.py is referencing the current/original code, not my modified code. If you update your ambermodel.py as per my change (see the 'Files changed' tab), you should no longer get the error.

Hahaa. I left the original line 315 in and added your extra lines, but I realise I was meant to delete the original line 315. Sorry and thanks.

@boc-the-git
Copy link
Contributor Author

No worries @stuarts0011 ! I'll assume that my change does fix your issue then unless we hear otherwise :)

@lewisbenge lewisbenge merged commit 9e07b4b into lewisbenge:master Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants