2021-05-14 15:02:11 +00:00
|
|
|
"""The kraken integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2021-09-23 14:42:55 +00:00
|
|
|
from typing import Optional
|
2021-05-14 15:02:11 +00:00
|
|
|
|
2021-05-15 20:55:50 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2021-05-14 15:02:11 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-05-15 20:55:50 +00:00
|
|
|
from homeassistant.helpers import device_registry
|
2021-05-14 15:02:11 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-05-17 07:12:04 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-05-14 15:02:11 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
from . import KrakenData
|
|
|
|
from .const import (
|
|
|
|
CONF_TRACKED_ASSET_PAIRS,
|
|
|
|
DISPATCH_CONFIG_UPDATED,
|
|
|
|
DOMAIN,
|
|
|
|
SENSOR_TYPES,
|
2021-09-23 14:42:55 +00:00
|
|
|
KrakenResponse,
|
|
|
|
KrakenSensorEntityDescription,
|
2021-05-14 15:02:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-05-17 07:12:04 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2021-05-14 15:02:11 +00:00
|
|
|
"""Add kraken entities from a config_entry."""
|
|
|
|
|
|
|
|
@callback
|
2021-05-15 20:55:50 +00:00
|
|
|
def async_update_sensors(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
|
|
dev_reg = device_registry.async_get(hass)
|
2021-05-14 15:02:11 +00:00
|
|
|
|
|
|
|
existing_devices = {
|
|
|
|
device.name: device.id
|
2021-05-15 20:55:50 +00:00
|
|
|
for device in device_registry.async_entries_for_config_entry(
|
|
|
|
dev_reg, config_entry.entry_id
|
2021-05-14 15:02:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-23 14:42:55 +00:00
|
|
|
entities = []
|
2021-05-14 15:02:11 +00:00
|
|
|
for tracked_asset_pair in config_entry.options[CONF_TRACKED_ASSET_PAIRS]:
|
|
|
|
# Only create new devices
|
2021-05-15 20:55:50 +00:00
|
|
|
if (
|
|
|
|
device_name := create_device_name(tracked_asset_pair)
|
|
|
|
) in existing_devices:
|
|
|
|
existing_devices.pop(device_name)
|
2021-05-14 15:02:11 +00:00
|
|
|
else:
|
2021-09-23 14:42:55 +00:00
|
|
|
entities.extend(
|
|
|
|
[
|
2021-05-14 15:02:11 +00:00
|
|
|
KrakenSensor(
|
|
|
|
hass.data[DOMAIN],
|
|
|
|
tracked_asset_pair,
|
2021-09-23 14:42:55 +00:00
|
|
|
description,
|
2021-05-14 15:02:11 +00:00
|
|
|
)
|
2021-09-23 14:42:55 +00:00
|
|
|
for description in SENSOR_TYPES
|
|
|
|
]
|
|
|
|
)
|
|
|
|
async_add_entities(entities, True)
|
2021-05-14 15:02:11 +00:00
|
|
|
|
|
|
|
# Remove devices for asset pairs which are no longer tracked
|
|
|
|
for device_id in existing_devices.values():
|
2021-05-15 20:55:50 +00:00
|
|
|
dev_reg.async_remove_device(device_id)
|
2021-05-14 15:02:11 +00:00
|
|
|
|
2021-05-15 20:55:50 +00:00
|
|
|
async_update_sensors(hass, config_entry)
|
2021-05-14 15:02:11 +00:00
|
|
|
|
2021-05-17 07:12:04 +00:00
|
|
|
config_entry.async_on_unload(
|
2021-05-14 15:02:11 +00:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
DISPATCH_CONFIG_UPDATED,
|
|
|
|
async_update_sensors,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-23 14:42:55 +00:00
|
|
|
class KrakenSensor(CoordinatorEntity[Optional[KrakenResponse]], SensorEntity):
|
2021-05-14 15:02:11 +00:00
|
|
|
"""Define a Kraken sensor."""
|
|
|
|
|
2021-09-23 14:42:55 +00:00
|
|
|
entity_description: KrakenSensorEntityDescription
|
|
|
|
|
2021-05-14 15:02:11 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
kraken_data: KrakenData,
|
|
|
|
tracked_asset_pair: str,
|
2021-09-23 14:42:55 +00:00
|
|
|
description: KrakenSensorEntityDescription,
|
2021-05-14 15:02:11 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize."""
|
2021-05-17 07:12:04 +00:00
|
|
|
assert kraken_data.coordinator is not None
|
2021-05-14 15:02:11 +00:00
|
|
|
super().__init__(kraken_data.coordinator)
|
2021-09-23 14:42:55 +00:00
|
|
|
self.entity_description = description
|
2021-05-14 15:02:11 +00:00
|
|
|
self.tracked_asset_pair_wsname = kraken_data.tradable_asset_pairs[
|
|
|
|
tracked_asset_pair
|
|
|
|
]
|
2021-09-23 14:42:55 +00:00
|
|
|
source_asset = tracked_asset_pair.split("/")[0]
|
2021-05-14 15:02:11 +00:00
|
|
|
self._target_asset = tracked_asset_pair.split("/")[1]
|
2021-09-23 14:42:55 +00:00
|
|
|
if "number_of" not in description.key:
|
|
|
|
self._attr_native_unit_of_measurement = self._target_asset
|
|
|
|
self._device_name = f"{source_asset} {self._target_asset}"
|
|
|
|
self._attr_name = "_".join(
|
2021-05-14 15:02:11 +00:00
|
|
|
[
|
|
|
|
tracked_asset_pair.split("/")[0],
|
|
|
|
tracked_asset_pair.split("/")[1],
|
2021-09-23 14:42:55 +00:00
|
|
|
description.key,
|
2021-05-14 15:02:11 +00:00
|
|
|
]
|
|
|
|
)
|
2021-09-23 14:42:55 +00:00
|
|
|
self._attr_unique_id = self._attr_name.lower()
|
2021-05-14 15:02:11 +00:00
|
|
|
self._received_data_at_least_once = False
|
|
|
|
self._available = True
|
|
|
|
|
2021-09-23 14:42:55 +00:00
|
|
|
self._attr_device_info = {
|
|
|
|
"identifiers": {(DOMAIN, f"{source_asset}_{self._target_asset}")},
|
|
|
|
"name": self._device_name,
|
|
|
|
"manufacturer": "Kraken.com",
|
|
|
|
"entry_type": "service",
|
|
|
|
}
|
2021-05-14 15:02:11 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self._update_internal_state()
|
|
|
|
|
2021-05-17 07:12:04 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
2021-05-14 15:02:11 +00:00
|
|
|
self._update_internal_state()
|
2021-05-15 20:55:50 +00:00
|
|
|
super()._handle_coordinator_update()
|
2021-05-14 15:02:11 +00:00
|
|
|
|
2021-05-17 07:12:04 +00:00
|
|
|
def _update_internal_state(self) -> None:
|
2021-05-14 15:02:11 +00:00
|
|
|
try:
|
2021-09-23 14:42:55 +00:00
|
|
|
self._attr_native_value = self.entity_description.value_fn(
|
|
|
|
self.coordinator, self.tracked_asset_pair_wsname # type: ignore[arg-type]
|
|
|
|
)
|
2021-05-14 15:02:11 +00:00
|
|
|
self._received_data_at_least_once = True # Received data at least one time.
|
|
|
|
except TypeError:
|
|
|
|
if self._received_data_at_least_once:
|
|
|
|
if self._available:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Asset Pair %s is no longer available",
|
|
|
|
self._device_name,
|
|
|
|
)
|
|
|
|
self._available = False
|
|
|
|
|
|
|
|
@property
|
2021-05-17 07:12:04 +00:00
|
|
|
def icon(self) -> str:
|
2021-05-14 15:02:11 +00:00
|
|
|
"""Return the icon."""
|
|
|
|
if self._target_asset == "EUR":
|
|
|
|
return "mdi:currency-eur"
|
|
|
|
if self._target_asset == "GBP":
|
|
|
|
return "mdi:currency-gbp"
|
|
|
|
if self._target_asset == "USD":
|
|
|
|
return "mdi:currency-usd"
|
|
|
|
if self._target_asset == "JPY":
|
|
|
|
return "mdi:currency-jpy"
|
|
|
|
if self._target_asset == "XBT":
|
|
|
|
return "mdi:currency-btc"
|
|
|
|
return "mdi:cash"
|
|
|
|
|
|
|
|
@property
|
2021-05-17 07:12:04 +00:00
|
|
|
def available(self) -> bool:
|
2021-05-14 15:02:11 +00:00
|
|
|
"""Could the api be accessed during the last update call."""
|
|
|
|
return self._available and self.coordinator.last_update_success
|
|
|
|
|
|
|
|
|
|
|
|
def create_device_name(tracked_asset_pair: str) -> str:
|
|
|
|
"""Create the device name for a given tracked asset pair."""
|
|
|
|
return f"{tracked_asset_pair.split('/')[0]} {tracked_asset_pair.split('/')[1]}"
|