2016-06-21 14:43:02 +00:00
|
|
|
"""
|
|
|
|
Currency exchange rate support that comes from fixer.io.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.fixer/
|
|
|
|
"""
|
|
|
|
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-01-18 20:48:21 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_BASE, CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-06-21 14:43:02 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
REQUIREMENTS = ['fixerio==0.1.1']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-10-11 07:28:19 +00:00
|
|
|
ATTR_BASE = 'Base currency'
|
|
|
|
ATTR_EXCHANGE_RATE = 'Exchange rate'
|
|
|
|
ATTR_TARGET = 'Target currency'
|
|
|
|
|
|
|
|
CONF_ATTRIBUTION = "Data provided by the European Central Bank (ECB)"
|
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({
|
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_BASE, default=DEFAULT_BASE): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2016-06-21 14:43:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, 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
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
base = config.get(CONF_BASE)
|
2016-06-21 14:43:02 +00:00
|
|
|
target = config.get(CONF_TARGET)
|
|
|
|
|
|
|
|
try:
|
|
|
|
Fixerio(base=base, symbols=[target], secure=True).latest()
|
|
|
|
except exceptions.FixerioException:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("One of the given currencies is not supported")
|
2016-06-21 14:43:02 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
data = ExchangeData(base, target)
|
2017-06-05 15:35:26 +00:00
|
|
|
add_devices([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 {
|
2016-10-11 07:28:19 +00:00
|
|
|
ATTR_BASE: self.data.rate['base'],
|
|
|
|
ATTR_TARGET: self._target,
|
|
|
|
ATTR_EXCHANGE_RATE: self.data.rate['rates'][self._target],
|
|
|
|
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeData(object):
|
|
|
|
"""Get the latest data and update the states."""
|
|
|
|
|
|
|
|
def __init__(self, base_currency, target_currency):
|
|
|
|
"""Initialize the data object."""
|
|
|
|
from fixerio import Fixerio
|
|
|
|
|
|
|
|
self.rate = None
|
|
|
|
self.base_currency = base_currency
|
|
|
|
self.target_currency = target_currency
|
2017-05-02 16:18:47 +00:00
|
|
|
self.exchange = Fixerio(
|
|
|
|
base=self.base_currency, symbols=[self.target_currency],
|
|
|
|
secure=True)
|
2016-06-21 14:43:02 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Fixer.io."""
|
|
|
|
self.rate = self.exchange.latest()
|