Use voluptuous for exchange sensors (#2801)
* Use voluptuous for exchange sensors * Remove additional checks
This commit is contained in:
1 parent
dab5a78f88
commit
822b7f8770
3 files changed
+54
-31
No files matched your search
@@ -7,11 +7,16 @@ https://home-assistant.io/components/sensor.bitcoin/
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_DISPLAY_OPTIONS
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
REQUIREMENTS = ['blockchain==1.3.3']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
OPTION_TYPES = {
|
||||
'exchangerate': ['Exchange rate (1 BTC)', None],
|
||||
'trade_volume_btc': ['Trade volume', 'BTC'],
|
||||
@@ -35,17 +40,27 @@ OPTION_TYPES = {
|
||||
'miners_revenue_btc': ['Miners revenue', 'BTC'],
|
||||
'market_price_usd': ['Market price', 'USD']
|
||||
}
|
||||
|
||||
ICON = 'mdi:currency-btc'
|
||||
CONF_CURRENCY = 'currency'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_DISPLAY_OPTIONS, default=[]):
|
||||
[vol.In(OPTION_TYPES)],
|
||||
vol.Optional(CONF_CURRENCY, default='USD'): cv.string,
|
||||
})
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Return cached results if last scan was less then this time ago.
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=120)
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Bitcoin sensors."""
|
||||
from blockchain import exchangerates
|
||||
|
||||
currency = config.get('currency', 'USD')
|
||||
currency = config.get(CONF_CURRENCY)
|
||||
|
||||
if currency not in exchangerates.get_ticker():
|
||||
_LOGGER.error('Currency "%s" is not available. Using "USD"', currency)
|
||||
@@ -53,11 +68,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
data = BitcoinData()
|
||||
dev = []
|
||||
for variable in config['display_options']:
|
||||
if variable not in OPTION_TYPES:
|
||||
_LOGGER.error('Option type: "%s" does not exist', variable)
|
||||
else:
|
||||
dev.append(BitcoinSensor(data, variable, currency))
|
||||
for variable in config[CONF_DISPLAY_OPTIONS]:
|
||||
dev.append(BitcoinSensor(data, variable, currency))
|
||||
|
||||
add_devices(dev)
|
||||
|
||||
|
||||
Reference in new issue
Block a user