-
-
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
Merged
+101
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
30b7ce0
Create openexchangerates.py
arsaboo 3a6bce5
Create OpenExchangeRates Sensor
arsaboo c37011a
Add openexchangerate sensor
arsaboo 28115ec
Update openexchangerates.py
arsaboo 318ac65
Added params dict
arsaboo e522d7a
Update openexchangerates.py
arsaboo 1cde99f
Update openexchangerates.py
arsaboo 5736672
Update openexchangerates.py
arsaboo ae74f26
Update openexchangerates.py
arsaboo 2b55258
Added API key validation
arsaboo 667247a
Update openexchangerates.py
arsaboo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"""Support for openexchangerates.org exchange rates service.""" | ||
from datetime import timedelta | ||
import logging | ||
import requests | ||
from homeassistant.helpers.entity import Entity | ||
from homeassistant.util import Throttle | ||
from homeassistant.const import CONF_API_KEY | ||
|
||
_RESOURCE = 'https://openexchangerates.org/api/latest.json' | ||
_LOGGER = logging.getLogger(__name__) | ||
# Return cached results if last scan was less then this time ago. | ||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=100) | ||
CONF_BASE = 'base' | ||
CONF_QUOTE = 'quote' | ||
CONF_NAME = 'name' | ||
DEFAULT_NAME = 'Exchange Rate Sensor' | ||
|
||
|
||
def setup_platform(hass, config, add_devices, discovery_info=None): | ||
"""Setup the Openexchangerates sensor.""" | ||
payload = config.get('payload', None) | ||
rest = OpenexchangeratesData( | ||
_RESOURCE, | ||
config.get(CONF_API_KEY), | ||
config.get(CONF_BASE, 'USD'), | ||
config.get(CONF_QUOTE), | ||
payload | ||
) | ||
response = requests.get(_RESOURCE, params={'base': config.get(CONF_BASE, | ||
'USD'), | ||
'app_id': | ||
config.get(CONF_API_KEY)}, | ||
timeout=10) | ||
if response.status_code != 200: | ||
_LOGGER.error("Check your OpenExchangeRates API") | ||
return False | ||
rest.update() | ||
add_devices([OpenexchangeratesSensor(rest, config.get(CONF_NAME, | ||
DEFAULT_NAME), | ||
config.get(CONF_QUOTE))]) | ||
|
||
|
||
class OpenexchangeratesSensor(Entity): | ||
"""Implementing the Openexchangerates sensor.""" | ||
|
||
def __init__(self, rest, name, quote): | ||
"""Initialize the sensor.""" | ||
self.rest = rest | ||
self._name = name | ||
self._quote = quote | ||
self.update() | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the sensor.""" | ||
return self._name | ||
|
||
@property | ||
def state(self): | ||
"""Return the state of the sensor.""" | ||
return self._state | ||
|
||
@property | ||
def device_state_attributes(self): | ||
"""Return other attributes of the sensor.""" | ||
return self.rest.data | ||
|
||
def update(self): | ||
"""Update current conditions.""" | ||
self.rest.update() | ||
value = self.rest.data | ||
self._state = round(value[str(self._quote)], 4) | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class OpenexchangeratesData(object): | ||
"""Get data from Openexchangerates.org.""" | ||
|
||
# pylint: disable=too-many-arguments | ||
def __init__(self, resource, api_key, base, quote, data): | ||
"""Initialize the data object.""" | ||
self._resource = resource | ||
self._api_key = api_key | ||
self._base = base | ||
self._quote = quote | ||
self.data = None | ||
|
||
@Throttle(MIN_TIME_BETWEEN_UPDATES) | ||
def update(self): | ||
"""Get the latest data from openexchangerates.""" | ||
try: | ||
result = requests.get(self._resource, params={'base': self._base, | ||
'app_id': | ||
self._api_key}, | ||
timeout=10) | ||
self.data = result.json()['rates'] | ||
except requests.exceptions.HTTPError: | ||
_LOGGER.error("Check Openexchangerates API Key") | ||
self.data = None | ||
return False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: