2019-04-03 15:40:03 +00:00
|
|
|
"""Currency exchange rate support that comes from fixer.io."""
|
2016-06-21 14:43:02 +00:00
|
|
|
from datetime import timedelta
|
2018-01-18 20:48:21 +00:00
|
|
|
import logging
|
2016-06-21 14:43:02 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2018-07-03 10:30:56 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, CONF_NAME
|
2018-01-18 20:48:21 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-06-21 14:43:02 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-10-11 07:28:19 +00:00
|
|
|
ATTR_EXCHANGE_RATE = 'Exchange rate'
|
|
|
|
ATTR_TARGET = 'Target currency'
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by the European Central Bank (ECB)"
|
2016-10-11 07:28:19 +00:00
|
|
|
|
2016-06-21 14:43:02 +00:00
|
|
|
CONF_TARGET = 'target'
|
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
DEFAULT_BASE = 'USD'
|
|
|
|
DEFAULT_NAME = 'Exchange rate'
|
|
|
|
|
2018-01-18 20:48:21 +00:00
|
|
|
ICON = 'mdi:currency-usd'
|
2016-08-20 22:40:16 +00:00
|
|
|
|
2017-06-05 15:35:26 +00:00
|
|
|
SCAN_INTERVAL = timedelta(days=1)
|
2016-08-20 22:40:16 +00:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2018-07-03 10:30:56 +00:00
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
2016-06-21 14:43:02 +00:00
|
|
|
vol.Required(CONF_TARGET): cv.string,
|
2016-08-20 22:40:16 +00:00
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-06-21 14:43:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Fixer.io sensor."""
|
2018-01-18 20:48:21 +00:00
|
|
|
from fixerio import Fixerio, exceptions
|
2016-06-21 14:43:02 +00:00
|
|
|
|
2018-07-03 10:30:56 +00:00
|
|
|
api_key = config.get(CONF_API_KEY)
|
2016-08-20 22:40:16 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-06-21 14:43:02 +00:00
|
|
|
target = config.get(CONF_TARGET)
|
|
|
|
|
|
|
|
try:
|
2018-07-03 10:30:56 +00:00
|
|
|
Fixerio(symbols=[target], access_key=api_key).latest()
|
2016-06-21 14:43:02 +00:00
|
|
|
except exceptions.FixerioException:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("One of the given currencies is not supported")
|
2018-07-03 10:30:56 +00:00
|
|
|
return
|
2016-06-21 14:43:02 +00:00
|
|
|
|
2018-07-03 10:30:56 +00:00
|
|
|
data = ExchangeData(target, api_key)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([ExchangeRateSensor(data, name, target)], True)
|
2016-06-21 14:43:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExchangeRateSensor(Entity):
|
|
|
|
"""Representation of a Exchange sensor."""
|
|
|
|
|
|
|
|
def __init__(self, data, name, target):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.data = data
|
|
|
|
self._target = target
|
|
|
|
self._name = name
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._target
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self.data.rate is not None:
|
|
|
|
return {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2018-07-03 10:30:56 +00:00
|
|
|
ATTR_EXCHANGE_RATE: self.data.rate['rates'][self._target],
|
|
|
|
ATTR_TARGET: self._target,
|
2016-06-21 14:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return ICON
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
|
|
|
self.data.update()
|
|
|
|
self._state = round(self.data.rate['rates'][self._target], 3)
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class ExchangeData:
|
2016-06-21 14:43:02 +00:00
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
2018-07-03 10:30:56 +00:00
|
|
|
def __init__(self, target_currency, api_key):
|
2016-06-21 14:43:02 +00:00
|
|
|
"""Initialize the data object."""
|
|
|
|
from fixerio import Fixerio
|
|
|
|
|
2018-07-03 10:30:56 +00:00
|
|
|
self.api_key = api_key
|
2016-06-21 14:43:02 +00:00
|
|
|
self.rate = None
|
|
|
|
self.target_currency = target_currency
|
2017-05-02 16:18:47 +00:00
|
|
|
self.exchange = Fixerio(
|
2018-07-03 10:30:56 +00:00
|
|
|
symbols=[self.target_currency], access_key=self.api_key)
|
2016-06-21 14:43:02 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Fixer.io."""
|
|
|
|
self.rate = self.exchange.latest()
|