1 """Support for currencylayer.com exchange rates service."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
21 _LOGGER = logging.getLogger(__name__)
22 _RESOURCE =
"http://apilayer.net/api/live"
25 DEFAULT_NAME =
"CurrencyLayer Sensor"
30 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
32 vol.Required(CONF_API_KEY): cv.string,
33 vol.Required(CONF_QUOTE): vol.All(cv.ensure_list, [cv.string]),
34 vol.Optional(CONF_BASE, default=DEFAULT_BASE): cv.string,
35 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
43 add_entities: AddEntitiesCallback,
44 discovery_info: DiscoveryInfoType |
None =
None,
46 """Set up the Currencylayer sensor."""
47 base = config[CONF_BASE]
48 api_key = config[CONF_API_KEY]
49 parameters = {
"source": base,
"access_key": api_key,
"format": 1}
53 response = requests.get(_RESOURCE, params=parameters, timeout=10)
54 if "error" in response.json():
63 """Implementing the Currencylayer sensor."""
65 _attr_attribution =
"Data provided by currencylayer.com"
66 _attr_icon =
"mdi:currency"
69 """Initialize the sensor."""
77 """Return the unit of measurement of this entity, if any."""
82 """Return the name of the sensor."""
83 return self.
_base_base
87 """Return the state of the sensor."""
91 """Update current date."""
93 if (value := self.
restrest.data)
is not None:
94 self.
_state_state = round(value[f
"{self._base}{self._quote}"], 4)
98 """Get data from Currencylayer.org."""
101 """Initialize the data object."""
107 """Get the latest data from Currencylayer."""
110 if "error" in result.json():
111 raise ValueError(result.json()[
"error"][
"info"])
112 self.
datadata = result.json()[
"quotes"]
113 _LOGGER.debug(
"Currencylayer data updated: %s", result.json()[
"timestamp"])
114 except ValueError
as err:
115 _LOGGER.error(
"Check Currencylayer API %s", err.args)
def __init__(self, resource, parameters)
def __init__(self, rest, base, quote)
def native_unit_of_measurement(self)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)