2019-04-03 15:40:03 +00:00
|
|
|
"""Support for openexchangerates.org exchange rates service."""
|
2016-06-25 07:02:28 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2016-08-16 20:22:55 +00:00
|
|
|
|
2016-06-25 07:02:28 +00:00
|
|
|
import requests
|
2016-08-16 20:22:55 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2016-10-29 20:27:02 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-09 13:26:53 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_BASE,
|
2019-12-09 13:26:53 +00:00
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_QUOTE,
|
2020-04-08 16:47:38 +00:00
|
|
|
HTTP_OK,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-08-16 20:22:55 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-06-25 07:02:28 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-07-31 19:25:30 +00:00
|
|
|
_RESOURCE = "https://openexchangerates.org/api/latest.json"
|
2016-08-16 20:22:55 +00:00
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by openexchangerates.org"
|
2016-08-16 20:22:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_BASE = "USD"
|
|
|
|
DEFAULT_NAME = "Exchange Rate Sensor"
|
2016-08-20 22:40:16 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(hours=2)
|
2016-08-16 20:22:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_QUOTE): cv.string,
|
|
|
|
vol.Optional(CONF_BASE, default=DEFAULT_BASE): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-16 20:22:55 +00:00
|
|
|
|
2016-06-25 07:02:28 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-10-29 20:27:02 +00:00
|
|
|
"""Set up the Open Exchange Rates sensor."""
|
2016-08-16 20:22:55 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
api_key = config.get(CONF_API_KEY)
|
|
|
|
base = config.get(CONF_BASE)
|
|
|
|
quote = config.get(CONF_QUOTE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
parameters = {"base": base, "app_id": api_key}
|
2016-10-29 20:27:02 +00:00
|
|
|
|
|
|
|
rest = OpenexchangeratesData(_RESOURCE, parameters, quote)
|
|
|
|
response = requests.get(_RESOURCE, params=parameters, timeout=10)
|
|
|
|
|
2020-04-08 16:47:38 +00:00
|
|
|
if response.status_code != HTTP_OK:
|
2016-08-16 20:22:55 +00:00
|
|
|
_LOGGER.error("Check your OpenExchangeRates API key")
|
2016-06-25 07:02:28 +00:00
|
|
|
return False
|
2016-10-29 20:27:02 +00:00
|
|
|
|
2016-06-25 07:02:28 +00:00
|
|
|
rest.update()
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([OpenexchangeratesSensor(rest, name, quote)], True)
|
2016-06-25 07:02:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OpenexchangeratesSensor(Entity):
|
2016-08-16 20:22:55 +00:00
|
|
|
"""Representation of an Open Exchange Rates sensor."""
|
2016-06-25 07:02:28 +00:00
|
|
|
|
|
|
|
def __init__(self, rest, name, quote):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.rest = rest
|
|
|
|
self._name = name
|
|
|
|
self._quote = quote
|
2017-08-06 17:21:55 +00:00
|
|
|
self._state = None
|
2016-06-25 07:02:28 +00:00
|
|
|
|
|
|
|
@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."""
|
2016-10-29 20:27:02 +00:00
|
|
|
attr = self.rest.data
|
2019-02-14 21:09:22 +00:00
|
|
|
attr[ATTR_ATTRIBUTION] = ATTRIBUTION
|
2016-10-29 20:27:02 +00:00
|
|
|
|
|
|
|
return attr
|
2016-06-25 07:02:28 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update current conditions."""
|
|
|
|
self.rest.update()
|
|
|
|
value = self.rest.data
|
|
|
|
self._state = round(value[str(self._quote)], 4)
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class OpenexchangeratesData:
|
2016-06-25 07:02:28 +00:00
|
|
|
"""Get data from Openexchangerates.org."""
|
|
|
|
|
2016-10-29 20:27:02 +00:00
|
|
|
def __init__(self, resource, parameters, quote):
|
2016-06-25 07:02:28 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
self._resource = resource
|
2016-10-29 20:27:02 +00:00
|
|
|
self._parameters = parameters
|
2016-06-25 07:02:28 +00:00
|
|
|
self._quote = quote
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Get the latest data from openexchangerates.org."""
|
2016-06-25 07:02:28 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
result = requests.get(self._resource, params=self._parameters, timeout=10)
|
|
|
|
self.data = result.json()["rates"]
|
2016-06-25 07:02:28 +00:00
|
|
|
except requests.exceptions.HTTPError:
|
2016-10-29 20:27:02 +00:00
|
|
|
_LOGGER.error("Check the Openexchangerates API key")
|
2016-06-25 07:02:28 +00:00
|
|
|
self.data = None
|
|
|
|
return False
|