1 """Currency exchange rate support that comes from fixer.io."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from fixerio
import Fixerio
10 from fixerio.exceptions
import FixerioException
11 import voluptuous
as vol
14 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
23 _LOGGER = logging.getLogger(__name__)
25 ATTR_EXCHANGE_RATE =
"Exchange rate"
26 ATTR_TARGET =
"Target currency"
29 DEFAULT_NAME =
"Exchange rate"
34 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
36 vol.Required(CONF_API_KEY): cv.string,
37 vol.Required(CONF_TARGET): cv.string,
38 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
46 add_entities: AddEntitiesCallback,
47 discovery_info: DiscoveryInfoType |
None =
None,
49 """Set up the Fixer.io sensor."""
50 api_key = config.get(CONF_API_KEY)
51 name = config.get(CONF_NAME)
52 target = config.get(CONF_TARGET)
55 Fixerio(symbols=[target], access_key=api_key).latest()
56 except FixerioException:
57 _LOGGER.error(
"One of the given currencies is not supported")
65 """Representation of a Exchange sensor."""
67 _attr_attribution =
"Data provided by the European Central Bank (ECB)"
68 _attr_icon =
"mdi:currency-usd"
71 """Initialize the sensor."""
79 """Return the name of the sensor."""
80 return self.
_name_name
84 """Return the unit of measurement of this entity, if any."""
89 """Return the state of the sensor."""
94 """Return the state attributes."""
95 if self.
datadata.rate
is not None:
97 ATTR_EXCHANGE_RATE: self.
datadata.rate[
"rates"][self.
_target_target],
98 ATTR_TARGET: self.
_target_target,
103 """Get the latest data and updates the states."""
109 """Get the latest data and update the states."""
112 """Initialize the data object."""
119 """Get the latest data from Fixer.io."""
def __init__(self, target_currency, api_key)
dict[str, Any]|None extra_state_attributes(self)
def native_unit_of_measurement(self)
def __init__(self, data, name, target)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)