Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions homeassistant/components/sensor/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_DISPLAY_OPTIONS, ATTR_ATTRIBUTION)
from homeassistant.const import (
CONF_DISPLAY_OPTIONS, ATTR_ATTRIBUTION, CONF_CURRENCY)
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__)

CONF_ATTRIBUTION = "Data provided by blockchain.info"
CONF_CURRENCY = 'currency'

DEFAULT_CURRENCY = 'USD'

ICON = 'mdi:currency-btc'

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
SCAN_INTERVAL = timedelta(minutes=5)

OPTION_TYPES = {
'exchangerate': ['Exchange rate (1 BTC)', None],
Expand Down Expand Up @@ -74,7 +73,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for variable in config[CONF_DISPLAY_OPTIONS]:
dev.append(BitcoinSensor(data, variable, currency))

add_devices(dev)
add_devices(dev, True)


class BitcoinSensor(Entity):
Expand All @@ -88,7 +87,6 @@ def __init__(self, data, option_type, currency):
self._currency = currency
self.type = option_type
self._state = None
self.update()

@property
def name(self):
Expand Down Expand Up @@ -154,8 +152,8 @@ def update(self):
elif self.type == 'total_btc_sent':
self._state = '{0:.2f}'.format(stats.total_btc_sent * 0.00000001)
elif self.type == 'estimated_btc_sent':
self._state = '{0:.2f}'.format(stats.estimated_btc_sent *
0.00000001)
self._state = '{0:.2f}'.format(
stats.estimated_btc_sent * 0.00000001)
elif self.type == 'total_btc':
self._state = '{0:.2f}'.format(stats.total_btc * 0.00000001)
elif self.type == 'total_blocks':
Expand All @@ -166,8 +164,8 @@ def update(self):
self._state = '{0:.2f}'.format(
stats.estimated_transaction_volume_usd)
elif self.type == 'miners_revenue_btc':
self._state = '{0:.1f}'.format(stats.miners_revenue_btc *
0.00000001)
self._state = '{0:.1f}'.format(
stats.miners_revenue_btc * 0.00000001)
elif self.type == 'market_price_usd':
self._state = '{0:.2f}'.format(stats.market_price_usd)

Expand All @@ -180,7 +178,6 @@ def __init__(self):
self.stats = None
self.ticker = None

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from blockchain.info."""
from blockchain import statistics, exchangerates
Expand Down
45 changes: 37 additions & 8 deletions homeassistant/components/sensor/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,58 @@
https://home-assistant.io/components/sensor.blockchain/
"""
import logging
from datetime import timedelta

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
import voluptuous as vol

REQUIREMENTS = ['python-blockchain-api==0.0.2']

_LOGGER = logging.getLogger(__name__)

CONF_ADDRESSES = 'addresses'
CONF_ATTRIBUTION = "Data provided by blockchain.info"

DEFAULT_NAME = 'Bitcoin Balance'

ICON = 'mdi:currency-btc'

SCAN_INTERVAL = timedelta(minutes=5)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ADDRESSES): [cv.string]
vol.Required(CONF_ADDRESSES): [cv.string],
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the blockchain sensors."""
"""Set up the Blockchain.info sensors."""
from pyblockchain import validate_address

addresses = config.get(CONF_ADDRESSES)
name = config.get(CONF_NAME)

for address in addresses:
if not validate_address(address):
_LOGGER.error("Bitcoin address is not valid: " + address)
_LOGGER.error("Bitcoin address is not valid: %s", address)
return False
add_devices([BlockchainSensor('Bitcoin Balance', addresses)])

add_devices([BlockchainSensor(name, addresses)], True)


class BlockchainSensor(Entity):
"""Representation of a blockchain.info sensor."""
"""Representation of a Blockchain.info sensor."""

def __init__(self, name, addresses):
"""Initialize the sensor."""
self._name = name
self.addresses = addresses
self._state = None
self._unit_of_measurement = 'BTC'
self.update()

@property
def name(self):
Expand All @@ -56,6 +73,18 @@ def unit_of_measurement(self):
"""Return the unit of measurement this sensor expresses itself in."""
return self._unit_of_measurement

@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return ICON

@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
}

def update(self):
"""Get the latest state of the sensor."""
from pyblockchain import get_balance
Expand Down
12 changes: 4 additions & 8 deletions homeassistant/components/sensor/coinmarketcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.const import ATTR_ATTRIBUTION, CONF_CURRENCY
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['coinmarketcap==2.0.1']

Expand All @@ -32,13 +31,12 @@
ATTR_TOTAL_SUPPLY = 'total_supply'

CONF_ATTRIBUTION = "Data provided by CoinMarketCap"
CONF_CURRENCY = 'currency'

DEFAULT_CURRENCY = 'bitcoin'

ICON = 'mdi:currency-usd'

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
SCAN_INTERVAL = timedelta(minutes=15)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_CURRENCY, default=DEFAULT_CURRENCY): cv.string,
Expand All @@ -56,7 +54,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
currency)
currency = DEFAULT_CURRENCY

add_devices([CoinMarketCapSensor(CoinMarketCapData(currency))])
add_devices([CoinMarketCapSensor(CoinMarketCapData(currency))], True)


class CoinMarketCapSensor(Entity):
Expand All @@ -67,7 +65,6 @@ def __init__(self, data):
self.data = data
self._ticker = None
self._unit_of_measurement = 'USD'
self.update()

@property
def name(self):
Expand Down Expand Up @@ -118,7 +115,6 @@ def __init__(self, currency):
self.currency = currency
self.ticker = None

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Get the latest data from blockchain.info."""
from coinmarketcap import Market
Expand Down
33 changes: 26 additions & 7 deletions homeassistant/components/sensor/etherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.etherscan/
"""
from datetime import timedelta

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, ATTR_ATTRIBUTION)
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv
import voluptuous as vol

REQUIREMENTS = ['python-etherscan-api==0.0.1']

CONF_ADDRESS = 'address'
CONF_ATTRIBUTION = "Data provided by etherscan.io"

DEFAULT_NAME = 'Ethereum Balance'

SCAN_INTERVAL = timedelta(minutes=5)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ADDRESS): cv.string
vol.Required(CONF_ADDRESS): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the etherscan sensors."""
add_devices([EtherscanSensor('Ethereum Balance',
config.get(CONF_ADDRESS))])
"""Set up the Etherscan.io sensors."""
address = config.get(CONF_ADDRESS)
name = config.get(CONF_NAME)

add_devices([EtherscanSensor(name, address)], True)


class EtherscanSensor(Entity):
Expand All @@ -32,7 +45,6 @@ def __init__(self, name, address):
self.address = address
self._state = None
self._unit_of_measurement = 'ETH'
self.update()

@property
def name(self):
Expand All @@ -49,6 +61,13 @@ def unit_of_measurement(self):
"""Return the unit of measurement this sensor expresses itself in."""
return self._unit_of_measurement

@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
}

def update(self):
"""Get the latest state of the sensor."""
from pyetherscan import get_balance
Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
CONF_COMMAND_STOP = 'command_stop'
CONF_CONDITION = 'condition'
CONF_COVERS = 'covers'
CONF_CURRENCY = 'currency'
CONF_CUSTOMIZE = 'customize'
CONF_CUSTOMIZE_DOMAIN = 'customize_domain'
CONF_CUSTOMIZE_GLOB = 'customize_glob'
Expand Down