2019-04-03 17:40:03 +02:00
|
|
|
"""Stock market information from Alpha Vantage."""
|
2017-12-03 21:34:59 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2018-01-08 16:58:34 +01:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION, CONF_API_KEY, CONF_CURRENCY, CONF_NAME)
|
2017-12-03 21:34:59 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
ATTR_CLOSE = 'close'
|
|
|
|
ATTR_HIGH = 'high'
|
|
|
|
ATTR_LOW = 'low'
|
|
|
|
|
2019-02-14 22:09:22 +01:00
|
|
|
ATTRIBUTION = "Stock market information provided by Alpha Vantage"
|
|
|
|
|
2018-01-08 16:58:34 +01:00
|
|
|
CONF_FOREIGN_EXCHANGE = 'foreign_exchange'
|
|
|
|
CONF_FROM = 'from'
|
|
|
|
CONF_SYMBOL = 'symbol'
|
2017-12-03 21:34:59 +01:00
|
|
|
CONF_SYMBOLS = 'symbols'
|
2018-01-08 16:58:34 +01:00
|
|
|
CONF_TO = 'to'
|
|
|
|
|
|
|
|
ICONS = {
|
|
|
|
'BTC': 'mdi:currency-btc',
|
|
|
|
'EUR': 'mdi:currency-eur',
|
|
|
|
'GBP': 'mdi:currency-gbp',
|
|
|
|
'INR': 'mdi:currency-inr',
|
|
|
|
'RUB': 'mdi:currency-rub',
|
2018-02-11 23:51:10 +01:00
|
|
|
'TRY': 'mdi:currency-try',
|
2018-01-08 16:58:34 +01:00
|
|
|
'USD': 'mdi:currency-usd',
|
|
|
|
}
|
2017-12-03 21:34:59 +01:00
|
|
|
|
2018-01-08 16:58:34 +01:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=5)
|
2017-12-03 21:34:59 +01:00
|
|
|
|
2018-01-08 16:58:34 +01:00
|
|
|
SYMBOL_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_SYMBOL): cv.string,
|
|
|
|
vol.Optional(CONF_CURRENCY): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
})
|
2017-12-03 21:34:59 +01:00
|
|
|
|
2018-01-08 16:58:34 +01:00
|
|
|
CURRENCY_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_FROM): cv.string,
|
|
|
|
vol.Required(CONF_TO): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
})
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
2018-02-11 23:51:10 +01:00
|
|
|
vol.Optional(CONF_FOREIGN_EXCHANGE):
|
2018-01-08 16:58:34 +01:00
|
|
|
vol.All(cv.ensure_list, [CURRENCY_SCHEMA]),
|
2018-02-11 23:51:10 +01:00
|
|
|
vol.Optional(CONF_SYMBOLS):
|
2018-01-08 16:58:34 +01:00
|
|
|
vol.All(cv.ensure_list, [SYMBOL_SCHEMA]),
|
2017-12-03 21:34:59 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-12-03 21:34:59 +01:00
|
|
|
"""Set up the Alpha Vantage sensor."""
|
|
|
|
from alpha_vantage.timeseries import TimeSeries
|
2018-01-08 16:58:34 +01:00
|
|
|
from alpha_vantage.foreignexchange import ForeignExchange
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
api_key = config.get(CONF_API_KEY)
|
2018-02-27 07:42:32 +01:00
|
|
|
symbols = config.get(CONF_SYMBOLS, [])
|
|
|
|
conversions = config.get(CONF_FOREIGN_EXCHANGE, [])
|
2018-02-11 23:51:10 +01:00
|
|
|
|
|
|
|
if not symbols and not conversions:
|
|
|
|
msg = 'Warning: No symbols or currencies configured.'
|
|
|
|
hass.components.persistent_notification.create(
|
|
|
|
msg, 'Sensor alpha_vantage')
|
|
|
|
_LOGGER.warning(msg)
|
|
|
|
return
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
timeseries = TimeSeries(key=api_key)
|
|
|
|
|
|
|
|
dev = []
|
|
|
|
for symbol in symbols:
|
|
|
|
try:
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Configuring timeseries for symbols: %s",
|
|
|
|
symbol[CONF_SYMBOL])
|
2018-01-08 16:58:34 +01:00
|
|
|
timeseries.get_intraday(symbol[CONF_SYMBOL])
|
2017-12-03 21:34:59 +01:00
|
|
|
except ValueError:
|
|
|
|
_LOGGER.error(
|
|
|
|
"API Key is not valid or symbol '%s' not known", symbol)
|
|
|
|
dev.append(AlphaVantageSensor(timeseries, symbol))
|
|
|
|
|
2018-01-08 16:58:34 +01:00
|
|
|
forex = ForeignExchange(key=api_key)
|
2018-02-11 23:51:10 +01:00
|
|
|
for conversion in conversions:
|
2018-01-08 16:58:34 +01:00
|
|
|
from_cur = conversion.get(CONF_FROM)
|
|
|
|
to_cur = conversion.get(CONF_TO)
|
|
|
|
try:
|
2018-02-13 06:06:03 +01:00
|
|
|
_LOGGER.debug("Configuring forex %s - %s", from_cur, to_cur)
|
2018-01-08 16:58:34 +01:00
|
|
|
forex.get_currency_exchange_rate(
|
|
|
|
from_currency=from_cur, to_currency=to_cur)
|
|
|
|
except ValueError as error:
|
|
|
|
_LOGGER.error(
|
|
|
|
"API Key is not valid or currencies '%s'/'%s' not known",
|
|
|
|
from_cur, to_cur)
|
|
|
|
_LOGGER.debug(str(error))
|
|
|
|
dev.append(AlphaVantageForeignExchange(forex, conversion))
|
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
add_entities(dev, True)
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Setup completed")
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AlphaVantageSensor(Entity):
|
|
|
|
"""Representation of a Alpha Vantage sensor."""
|
|
|
|
|
|
|
|
def __init__(self, timeseries, symbol):
|
|
|
|
"""Initialize the sensor."""
|
2018-01-08 16:58:34 +01:00
|
|
|
self._symbol = symbol[CONF_SYMBOL]
|
|
|
|
self._name = symbol.get(CONF_NAME, self._symbol)
|
2017-12-03 21:34:59 +01:00
|
|
|
self._timeseries = timeseries
|
|
|
|
self.values = None
|
2018-01-08 16:58:34 +01:00
|
|
|
self._unit_of_measurement = symbol.get(CONF_CURRENCY, self._symbol)
|
|
|
|
self._icon = ICONS.get(symbol.get(CONF_CURRENCY, 'USD'))
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
@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."""
|
2018-01-08 16:58:34 +01:00
|
|
|
return self._unit_of_measurement
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.values['1. open']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self.values is not None:
|
|
|
|
return {
|
2019-02-14 22:09:22 +01:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2017-12-03 21:34:59 +01:00
|
|
|
ATTR_CLOSE: self.values['4. close'],
|
|
|
|
ATTR_HIGH: self.values['2. high'],
|
|
|
|
ATTR_LOW: self.values['3. low'],
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2018-01-08 16:58:34 +01:00
|
|
|
return self._icon
|
2017-12-03 21:34:59 +01:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Requesting new data for symbol %s", self._symbol)
|
2017-12-03 21:34:59 +01:00
|
|
|
all_values, _ = self._timeseries.get_intraday(self._symbol)
|
|
|
|
self.values = next(iter(all_values.values()))
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Received new values for symbol %s", self._symbol)
|
2018-01-08 16:58:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AlphaVantageForeignExchange(Entity):
|
|
|
|
"""Sensor for foreign exchange rates."""
|
|
|
|
|
|
|
|
def __init__(self, foreign_exchange, config):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._foreign_exchange = foreign_exchange
|
|
|
|
self._from_currency = config.get(CONF_FROM)
|
|
|
|
self._to_currency = config.get(CONF_TO)
|
|
|
|
if CONF_NAME in config:
|
|
|
|
self._name = config.get(CONF_NAME)
|
|
|
|
else:
|
|
|
|
self._name = '{}/{}'.format(self._to_currency, self._from_currency)
|
|
|
|
self._unit_of_measurement = self._to_currency
|
|
|
|
self._icon = ICONS.get(self._from_currency, 'USD')
|
|
|
|
self.values = 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._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2018-01-18 21:47:46 +01:00
|
|
|
return round(float(self.values['5. Exchange Rate']), 4)
|
2018-01-08 16:58:34 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self.values is not None:
|
|
|
|
return {
|
2019-02-14 22:09:22 +01:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2018-01-08 16:58:34 +01:00
|
|
|
CONF_FROM: self._from_currency,
|
|
|
|
CONF_TO: self._to_currency,
|
|
|
|
}
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the states."""
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Requesting new data for forex %s - %s",
|
2018-02-13 06:06:03 +01:00
|
|
|
self._from_currency, self._to_currency)
|
2018-01-08 16:58:34 +01:00
|
|
|
self.values, _ = self._foreign_exchange.get_currency_exchange_rate(
|
|
|
|
from_currency=self._from_currency, to_currency=self._to_currency)
|
2018-02-09 00:14:49 +01:00
|
|
|
_LOGGER.debug("Received new data for forex %s - %s",
|
2018-02-13 06:06:03 +01:00
|
|
|
self._from_currency, self._to_currency)
|