-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Add OpenExchangeRates sensor #2356
Conversation
_LOGGER.error("Check OpenExchangeRates API") | ||
self.data = None | ||
return False | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 1: W391 blank line at end of file
def update(self): | ||
"""Get the latest data from openexchangerates.""" | ||
try: | ||
result = requests.get(self._resource + '?base=' + self._base + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can pass a params dict instead of encoding the query string yourself: http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks....done :)
payload | ||
) | ||
rest.update() | ||
add_devices([OpenexchangeratesSensor(rest, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like I wrote before, please verify that the API key is working before adding it to the system.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the status code check on lines 88 through 90 and the sensor is not added when the API key is not valid, but it does not exit gracefully.
16-06-22 20:46:34 custom_components.sensor.openexchangerates: Check OpenExchangeRates API
16-06-22 20:46:34 homeassistant.components.sensor: Error while setting up platform openexchangerates
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 98, in _setup_platform
discovery_info)
File "/home/pi/.homeassistant/custom_components/sensor/openexchangerates.py", line 32, in setup_platform
config.get(CONF_QUOTE))])
File "/home/pi/.homeassistant/custom_components/sensor/openexchangerates.py", line 43, in __init__
self.update()
File "/home/pi/.homeassistant/custom_components/sensor/openexchangerates.py", line 64, in update
self._state = round(value[str(self._quote)], 4)
TypeError: 'NoneType' object is not subscriptable
I should probably do some check around 29, but the try
and except
did not work. I added the following before rest.update()
on line 29, but that did not help.
try:
response = requests.get(_RESOURCE, params={'base': config.get(CONF_BASE,
'USD'),
'app_id':
config.get(CONF_API_KEY)},
timeout=10)
except response.exceptions.HTTPError:
_LOGGER.error("Check OpenExchangeRates API1")
return False
Any suggestions? I could not find other sensors that use rest.update()
for ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, check the return value at line 29, if false, log error and return. As the http error could happen anytime, you also need to check if value is None, then return, at line 64 before doing anything else with value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With an in correct API, it should have triggered the response.exceptions.HTTPError:
, but it did not.
If I put a condition not None
on 64, other lines give an error as self
is None
. Is there a way to check if the rest.update()
on line 29 or self.update()
on line 43 return valid values?
I tried if rest is not None:
but that does not work :(
I was able to resolve the issue :). Now, the sensor checks the API key for any errors and adds the sensor if there are no errors. I have updated the documentation as well, but just wanted to inform you that I am still not able to get the |
Looks good! 🐬 |
"""Update current conditions.""" | ||
self.rest.update() | ||
value = self.rest.data | ||
self._state = round(value[str(self._quote)], 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will still error after an HTTP exception below. You can do:
round(value[str(self._quote)], 4) if value is not None else None
Super....thanks all!! |
Description: Initial version to obtain current exchange rate between any two currency pair from OpenExchangeRates.
Obtain API key here. Free accounts allow 1000 requests per month and is limited to
'USD'
base currency.Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#584
Example entry for
configuration.yaml
(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If code communicates with devices:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
..coveragerc
.If the code does not interact with devices:
tox
run successfully. Your PR cannot be merged unless tests passGet the current exchange rate between any two supported currency pair.