mirror of
https://github.com/home-assistant/core.git
synced 2025-09-02 09:01:05 +00:00
.github
config
docs
homeassistant
components
alarm_control_panel
automation
binary_sensor
camera
climate
cover
device_tracker
fan
frontend
garage_door
hvac
light
lock
media_player
mqtt
notify
recorder
rollershutter
scene
sensor
__init__.py
apcupsd.py
arduino.py
arest.py
bitcoin.py
bloomsky.py
coinmarketcap.py
command_line.py
cpuspeed.py
demo.py
deutsche_bahn.py
dht.py
dte_energy_bridge.py
dweet.py
ecobee.py
efergy.py
eliqonline.py
enocean.py
envisalink.py
fastdotcom.py
fitbit.py
fixer.py
forecast.py
fritzbox_callmonitor.py
glances.py
google_travel_time.py
gpsd.py
gtfs.py
homematic.py
hp_ilo.py
imap.py
isy994.py
lastfm.py
linux_battery.py
loopenergy.py
mfi.py
mhz19.py
modbus.py
mold_indicator.py
mqtt.py
mqtt_room.py
mysensors.py
nest.py
netatmo.py
neurio_energy.py
nzbget.py
octoprint.py
ohmconnect.py
onewire.py
openexchangerates.py
openweathermap.py
pi_hole.py
plex.py
rest.py
rfxtrx.py
sabnzbd.py
serial_pm.py
snmp.py
speedtest.py
steam_online.py
supervisord.py
swiss_hydrological_data.py
swiss_public_transport.py
systemmonitor.py
tcp.py
tellduslive.py
tellstick.py
temper.py
template.py
thinkingcleaner.py
time_date.py
torque.py
transmission.py
twitch.py
uber.py
vera.py
verisure.py
wink.py
worldclock.py
wunderground.py
xbox_live.py
yr.py
yweather.py
zigbee.py
zwave.py
switch
thermostat
__init__.py
alexa.py
apcupsd.py
api.py
arduino.py
bloomsky.py
browser.py
configurator.py
conversation.py
demo.py
device_sun_light_trigger.py
discovery.py
downloader.py
dweet.py
ecobee.py
emulated_hue.py
enocean.py
envisalink.py
feedreader.py
foursquare.py
graphite.py
group.py
hdmi_cec.py
history.py
homematic.py
http.py
ifttt.py
influxdb.py
input_boolean.py
input_select.py
input_slider.py
insteon_hub.py
introduction.py
isy994.py
joaoapps_join.py
keyboard.py
knx.py
lirc.py
logbook.py
logentries.py
logger.py
modbus.py
mqtt_eventstream.py
mysensors.py
nest.py
netatmo.py
octoprint.py
panel_custom.py
panel_iframe.py
persistent_notification.py
pilight.py
proximity.py
qwikswitch.py
rfxtrx.py
rpi_gpio.py
script.py
scsgate.py
services.yaml
shell_command.py
splunk.py
statsd.py
sun.py
tellduslive.py
tellstick.py
updater.py
upnp.py
vera.py
verisure.py
weblink.py
wemo.py
wink.py
zeroconf.py
zigbee.py
zone.py
zwave.py
helpers
scripts
util
__init__.py
__main__.py
bootstrap.py
config.py
const.py
core.py
exceptions.py
loader.py
remote.py
script
tests
virtualization
.coveragerc
.dockerignore
.gitignore
.gitmodules
.travis.yml
CONTRIBUTING.md
Dockerfile
LICENSE
MANIFEST.in
README.rst
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
setup.cfg
setup.py
tox.ini
185 lines
6.6 KiB
Python
185 lines
6.6 KiB
Python
"""
|
|
Bitcoin information service that uses blockchain.info.
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
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__)
|
|
|
|
CONF_CURRENCY = 'currency'
|
|
|
|
DEFAULT_CURRENCY = 'USD'
|
|
|
|
ICON = 'mdi:currency-btc'
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
|
|
|
OPTION_TYPES = {
|
|
'exchangerate': ['Exchange rate (1 BTC)', None],
|
|
'trade_volume_btc': ['Trade volume', 'BTC'],
|
|
'miners_revenue_usd': ['Miners revenue', 'USD'],
|
|
'btc_mined': ['Mined', 'BTC'],
|
|
'trade_volume_usd': ['Trade volume', 'USD'],
|
|
'difficulty': ['Difficulty', None],
|
|
'minutes_between_blocks': ['Time between Blocks', 'min'],
|
|
'number_of_transactions': ['No. of Transactions', None],
|
|
'hash_rate': ['Hash rate', 'PH/s'],
|
|
'timestamp': ['Timestamp', None],
|
|
'mined_blocks': ['Minded Blocks', None],
|
|
'blocks_size': ['Block size', None],
|
|
'total_fees_btc': ['Total fees', 'BTC'],
|
|
'total_btc_sent': ['Total sent', 'BTC'],
|
|
'estimated_btc_sent': ['Estimated sent', 'BTC'],
|
|
'total_btc': ['Total', 'BTC'],
|
|
'total_blocks': ['Total Blocks', None],
|
|
'next_retarget': ['Next retarget', None],
|
|
'estimated_transaction_volume_usd': ['Est. Transaction volume', 'USD'],
|
|
'miners_revenue_btc': ['Miners revenue', 'BTC'],
|
|
'market_price_usd': ['Market price', 'USD']
|
|
}
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
vol.Required(CONF_DISPLAY_OPTIONS, default=[]):
|
|
vol.All(cv.ensure_list, [vol.In(OPTION_TYPES)]),
|
|
vol.Optional(CONF_CURRENCY, default=DEFAULT_CURRENCY): cv.string,
|
|
})
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
|
"""Setup the Bitcoin sensors."""
|
|
from blockchain import exchangerates
|
|
|
|
currency = config.get(CONF_CURRENCY)
|
|
|
|
if currency not in exchangerates.get_ticker():
|
|
_LOGGER.warning('Currency "%s" is not available. Using "USD"',
|
|
currency)
|
|
currency = DEFAULT_CURRENCY
|
|
|
|
data = BitcoinData()
|
|
dev = []
|
|
for variable in config[CONF_DISPLAY_OPTIONS]:
|
|
dev.append(BitcoinSensor(data, variable, currency))
|
|
|
|
add_devices(dev)
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
class BitcoinSensor(Entity):
|
|
"""Representation of a Bitcoin sensor."""
|
|
|
|
def __init__(self, data, option_type, currency):
|
|
"""Initialize the sensor."""
|
|
self.data = data
|
|
self._name = OPTION_TYPES[option_type][0]
|
|
self._unit_of_measurement = OPTION_TYPES[option_type][1]
|
|
self._currency = currency
|
|
self.type = option_type
|
|
self._state = None
|
|
self.update()
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the sensor."""
|
|
return self._name
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the state of the sensor."""
|
|
return self._state
|
|
|
|
@property
|
|
def unit_of_measurement(self):
|
|
"""Return the unit the value is expressed in."""
|
|
return self._unit_of_measurement
|
|
|
|
@property
|
|
def icon(self):
|
|
"""Return the icon to use in the frontend, if any."""
|
|
return ICON
|
|
|
|
# pylint: disable=too-many-branches
|
|
def update(self):
|
|
"""Get the latest data and updates the states."""
|
|
self.data.update()
|
|
stats = self.data.stats
|
|
ticker = self.data.ticker
|
|
|
|
# pylint: disable=no-member
|
|
if self.type == 'exchangerate':
|
|
self._state = ticker[self._currency].p15min
|
|
self._unit_of_measurement = self._currency
|
|
elif self.type == 'trade_volume_btc':
|
|
self._state = '{0:.1f}'.format(stats.trade_volume_btc)
|
|
elif self.type == 'miners_revenue_usd':
|
|
self._state = '{0:.0f}'.format(stats.miners_revenue_usd)
|
|
elif self.type == 'btc_mined':
|
|
self._state = '{}'.format(stats.btc_mined * 0.00000001)
|
|
elif self.type == 'trade_volume_usd':
|
|
self._state = '{0:.1f}'.format(stats.trade_volume_usd)
|
|
elif self.type == 'difficulty':
|
|
self._state = '{0:.0f}'.format(stats.difficulty)
|
|
elif self.type == 'minutes_between_blocks':
|
|
self._state = '{0:.2f}'.format(stats.minutes_between_blocks)
|
|
elif self.type == 'number_of_transactions':
|
|
self._state = '{}'.format(stats.number_of_transactions)
|
|
elif self.type == 'hash_rate':
|
|
self._state = '{0:.1f}'.format(stats.hash_rate * 0.000001)
|
|
elif self.type == 'timestamp':
|
|
self._state = stats.timestamp
|
|
elif self.type == 'mined_blocks':
|
|
self._state = '{}'.format(stats.mined_blocks)
|
|
elif self.type == 'blocks_size':
|
|
self._state = '{0:.1f}'.format(stats.blocks_size)
|
|
elif self.type == 'total_fees_btc':
|
|
self._state = '{0:.2f}'.format(stats.total_fees_btc * 0.00000001)
|
|
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)
|
|
elif self.type == 'total_btc':
|
|
self._state = '{0:.2f}'.format(stats.total_btc * 0.00000001)
|
|
elif self.type == 'total_blocks':
|
|
self._state = '{0:.2f}'.format(stats.total_blocks)
|
|
elif self.type == 'next_retarget':
|
|
self._state = '{0:.2f}'.format(stats.next_retarget)
|
|
elif self.type == 'estimated_transaction_volume_usd':
|
|
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)
|
|
elif self.type == 'market_price_usd':
|
|
self._state = '{0:.2f}'.format(stats.market_price_usd)
|
|
|
|
|
|
class BitcoinData(object):
|
|
"""Get the latest data and update the states."""
|
|
|
|
def __init__(self):
|
|
"""Initialize the data object."""
|
|
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
|
|
|
|
self.stats = statistics.get()
|
|
self.ticker = exchangerates.get_ticker()
|