core/homeassistant/components/blockchain/sensor.py

61 lines
1.6 KiB
Python
Raw Normal View History

"""Support for Blockchain.com sensors."""
2017-06-05 11:36:39 +00:00
from datetime import timedelta
import logging
2017-06-05 11:36:39 +00:00
from pyblockchain import get_balance, validate_address
2017-06-05 11:36:39 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
2017-06-05 11:36:39 +00:00
ATTRIBUTION = "Data provided by blockchain.com"
2019-07-31 19:25:30 +00:00
CONF_ADDRESSES = "addresses"
2017-06-05 11:36:39 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Bitcoin Balance"
2017-06-05 11:36:39 +00:00
2019-07-31 19:25:30 +00:00
ICON = "mdi:currency-btc"
2017-06-05 11:36:39 +00:00
SCAN_INTERVAL = timedelta(minutes=5)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_ADDRESSES): [cv.string],
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Blockchain.com sensors."""
2017-06-05 11:36:39 +00:00
addresses = config[CONF_ADDRESSES]
name = config[CONF_NAME]
2017-06-05 11:36:39 +00:00
for address in addresses:
if not validate_address(address):
2017-06-05 11:36:39 +00:00
_LOGGER.error("Bitcoin address is not valid: %s", address)
return False
2017-06-05 11:36:39 +00:00
add_entities([BlockchainSensor(name, addresses)], True)
class BlockchainSensor(SensorEntity):
"""Representation of a Blockchain.com sensor."""
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
_attr_icon = ICON
_attr_native_unit_of_measurement = "BTC"
def __init__(self, name, addresses):
"""Initialize the sensor."""
self._attr_name = name
self.addresses = addresses
2017-06-05 11:36:39 +00:00
def update(self):
"""Get the latest state of the sensor."""
self._attr_native_value = get_balance(self.addresses)