2019-04-03 15:40:03 +00:00
|
|
|
"""Support for openexchangerates.org exchange rates service."""
|
2022-01-04 10:30:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-10-09 12:50:21 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_NAME, CONF_QUOTE
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-07 21:45:32 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-04 10:30:13 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-08-02 12:49:46 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2016-06-25 07:02:28 +00:00
|
|
|
|
2022-10-09 12:50:21 +00:00
|
|
|
from .const import DOMAIN
|
2022-08-02 12:49:46 +00:00
|
|
|
from .coordinator import OpenexchangeratesCoordinator
|
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
|
|
|
|
2022-08-02 12:49:46 +00:00
|
|
|
|
2022-08-07 21:45:32 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Open Exchange Rates sensor."""
|
|
|
|
# Only YAML imported configs have name and quote in config entry data.
|
|
|
|
name: str | None = config_entry.data.get(CONF_NAME)
|
|
|
|
quote: str = config_entry.data.get(CONF_QUOTE, "EUR")
|
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
OpenexchangeratesSensor(
|
|
|
|
config_entry, coordinator, name, rate_quote, rate_quote == quote
|
|
|
|
)
|
|
|
|
for rate_quote in coordinator.data.rates
|
|
|
|
)
|
2022-08-02 12:49:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OpenexchangeratesSensor(
|
|
|
|
CoordinatorEntity[OpenexchangeratesCoordinator], SensorEntity
|
|
|
|
):
|
2016-08-16 20:22:55 +00:00
|
|
|
"""Representation of an Open Exchange Rates sensor."""
|
2016-06-25 07:02:28 +00:00
|
|
|
|
2022-08-01 09:35:31 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
|
2022-08-02 12:49:46 +00:00
|
|
|
def __init__(
|
2022-08-07 21:45:32 +00:00
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
coordinator: OpenexchangeratesCoordinator,
|
|
|
|
name: str | None,
|
|
|
|
quote: str,
|
|
|
|
enabled: bool,
|
2022-08-02 12:49:46 +00:00
|
|
|
) -> None:
|
2016-06-25 07:02:28 +00:00
|
|
|
"""Initialize the sensor."""
|
2022-08-02 12:49:46 +00:00
|
|
|
super().__init__(coordinator)
|
2022-08-07 21:45:32 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
identifiers={(DOMAIN, config_entry.entry_id)},
|
|
|
|
manufacturer="Open Exchange Rates",
|
|
|
|
name=f"Open Exchange Rates {coordinator.base}",
|
|
|
|
)
|
|
|
|
self._attr_entity_registry_enabled_default = enabled
|
|
|
|
if name and enabled:
|
|
|
|
# name is legacy imported from YAML config
|
|
|
|
# this block can be removed when removing import from YAML
|
|
|
|
self._attr_name = name
|
|
|
|
self._attr_has_entity_name = False
|
|
|
|
else:
|
|
|
|
self._attr_name = quote
|
|
|
|
self._attr_has_entity_name = True
|
2022-08-02 12:49:46 +00:00
|
|
|
self._attr_native_unit_of_measurement = quote
|
2022-08-07 21:45:32 +00:00
|
|
|
self._attr_unique_id = f"{config_entry.entry_id}_{quote}"
|
|
|
|
self._quote = quote
|
2016-06-25 07:02:28 +00:00
|
|
|
|
|
|
|
@property
|
2022-08-02 12:49:46 +00:00
|
|
|
def native_value(self) -> float:
|
2016-06-25 07:02:28 +00:00
|
|
|
"""Return the state of the sensor."""
|
2022-08-02 12:49:46 +00:00
|
|
|
return round(self.coordinator.data.rates[self._quote], 4)
|