2020-02-06 14:41:48 +00:00
|
|
|
"""Support for Blockchain.com sensors."""
|
2022-01-04 10:08:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-06-05 11:36:39 +00:00
|
|
|
from datetime import timedelta
|
2019-10-18 13:12:00 +00:00
|
|
|
import logging
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2019-10-18 13:12:00 +00:00
|
|
|
from pyblockchain import get_balance, validate_address
|
2017-06-05 11:36:39 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-10-18 13:12:00 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
|
2022-01-04 10:08:28 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-10-18 13:12:00 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-04 10:08:28 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-06-05 05:48:38 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2020-02-06 14:41:48 +00:00
|
|
|
ATTRIBUTION = "Data provided by blockchain.com"
|
2019-02-14 21:09:22 +00:00
|
|
|
|
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)
|
2017-06-05 05:48:38 +00:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
2017-06-05 05:48:38 +00:00
|
|
|
|
|
|
|
|
2022-01-04 10:08:28 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-02-06 14:41:48 +00:00
|
|
|
"""Set up the Blockchain.com sensors."""
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2020-04-09 07:26:06 +00:00
|
|
|
addresses = config[CONF_ADDRESSES]
|
|
|
|
name = config[CONF_NAME]
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2017-06-05 05:48:38 +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)
|
2022-01-04 10:08:28 +00:00
|
|
|
return
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([BlockchainSensor(name, addresses)], True)
|
2017-06-05 05:48:38 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
class BlockchainSensor(SensorEntity):
|
2020-02-06 14:41:48 +00:00
|
|
|
"""Representation of a Blockchain.com sensor."""
|
2017-06-05 05:48:38 +00:00
|
|
|
|
2021-07-12 20:49:38 +00:00
|
|
|
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
|
|
_attr_icon = ICON
|
2021-08-11 08:45:05 +00:00
|
|
|
_attr_native_unit_of_measurement = "BTC"
|
2021-07-12 20:49:38 +00:00
|
|
|
|
2017-06-05 05:48:38 +00:00
|
|
|
def __init__(self, name, addresses):
|
|
|
|
"""Initialize the sensor."""
|
2021-07-12 20:49:38 +00:00
|
|
|
self._attr_name = name
|
2017-06-05 05:48:38 +00:00
|
|
|
self.addresses = addresses
|
2017-06-05 11:36:39 +00:00
|
|
|
|
2017-06-05 05:48:38 +00:00
|
|
|
def update(self):
|
|
|
|
"""Get the latest state of the sensor."""
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_value = get_balance(self.addresses)
|