From 81b1446aadb004918af6a2b8646bd880363cac78 Mon Sep 17 00:00:00 2001 From: Nolan Gilley Date: Mon, 5 Jun 2017 01:48:38 -0400 Subject: [PATCH] blockchain.info sensor (#7856) * blockchain sensor * Update blockchain.py * Update blockchain.py * add validation of btc addresses --- .coveragerc | 1 + homeassistant/components/sensor/blockchain.py | 62 +++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 66 insertions(+) create mode 100644 homeassistant/components/sensor/blockchain.py diff --git a/.coveragerc b/.coveragerc index 93dee127076..ee095f77d1f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -372,6 +372,7 @@ omit = homeassistant/components/sensor/arwn.py homeassistant/components/sensor/bbox.py homeassistant/components/sensor/bitcoin.py + homeassistant/components/sensor/blockchain.py homeassistant/components/sensor/bom.py homeassistant/components/sensor/broadlink.py homeassistant/components/sensor/dublin_bus_transport.py diff --git a/homeassistant/components/sensor/blockchain.py b/homeassistant/components/sensor/blockchain.py new file mode 100644 index 00000000000..a8d197ed837 --- /dev/null +++ b/homeassistant/components/sensor/blockchain.py @@ -0,0 +1,62 @@ +""" +Support for Blockchain.info sensors. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/sensor.blockchain/ +""" +import logging +from homeassistant.components.sensor import PLATFORM_SCHEMA +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' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_ADDRESSES): [cv.string] +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the blockchain sensors.""" + from pyblockchain import validate_address + addresses = config.get(CONF_ADDRESSES) + for address in addresses: + if not validate_address(address): + _LOGGER.error("Bitcoin address is not valid: " + address) + return False + add_devices([BlockchainSensor('Bitcoin Balance', addresses)]) + + +class BlockchainSensor(Entity): + """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): + """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 of measurement this sensor expresses itself in.""" + return self._unit_of_measurement + + def update(self): + """Get the latest state of the sensor.""" + from pyblockchain import get_balance + self._state = get_balance(self.addresses) diff --git a/requirements_all.txt b/requirements_all.txt index 95500006a37..a29c56d31f2 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -645,6 +645,9 @@ pysnmp==4.3.7 # homeassistant.components.switch.thinkingcleaner pythinkingcleaner==0.0.3 +# homeassistant.components.sensor.blockchain +python-blockchain-api==0.0.2 + # homeassistant.components.media_player.clementine python-clementine-remote==1.0.1