1 """Bitcoin information service that uses blockchain.com."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from blockchain
import exchangerates, statistics
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
14 SensorEntityDescription,
22 _LOGGER = logging.getLogger(__name__)
24 DEFAULT_CURRENCY =
"USD"
28 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
31 name=
"Exchange rate (1 BTC)",
34 key=
"trade_volume_btc",
36 native_unit_of_measurement=
"BTC",
39 key=
"miners_revenue_usd",
40 name=
"Miners revenue",
41 native_unit_of_measurement=
"USD",
46 native_unit_of_measurement=
"BTC",
49 key=
"trade_volume_usd",
51 native_unit_of_measurement=
"USD",
58 key=
"minutes_between_blocks",
59 name=
"Time between Blocks",
60 native_unit_of_measurement=UnitOfTime.MINUTES,
63 key=
"number_of_transactions",
64 name=
"No. of Transactions",
69 native_unit_of_measurement=f
"PH/{UnitOfTime.SECONDS}",
86 native_unit_of_measurement=
"BTC",
91 native_unit_of_measurement=
"BTC",
94 key=
"estimated_btc_sent",
95 name=
"Estimated sent",
96 native_unit_of_measurement=
"BTC",
101 native_unit_of_measurement=
"BTC",
109 name=
"Next retarget",
112 key=
"estimated_transaction_volume_usd",
113 name=
"Est. Transaction volume",
114 native_unit_of_measurement=
"USD",
117 key=
"miners_revenue_btc",
118 name=
"Miners revenue",
119 native_unit_of_measurement=
"BTC",
122 key=
"market_price_usd",
124 native_unit_of_measurement=
"USD",
128 OPTION_KEYS = [desc.key
for desc
in SENSOR_TYPES]
130 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
132 vol.Required(CONF_DISPLAY_OPTIONS, default=[]): vol.All(
133 cv.ensure_list, [vol.In(OPTION_KEYS)]
135 vol.Optional(CONF_CURRENCY, default=DEFAULT_CURRENCY): cv.string,
143 add_entities: AddEntitiesCallback,
144 discovery_info: DiscoveryInfoType |
None =
None,
146 """Set up the Bitcoin sensors."""
148 currency = config[CONF_CURRENCY]
150 if currency
not in exchangerates.get_ticker():
151 _LOGGER.warning(
"Currency %s is not available. Using USD", currency)
152 currency = DEFAULT_CURRENCY
157 for description
in SENSOR_TYPES
158 if description.key
in config[CONF_DISPLAY_OPTIONS]
165 """Representation of a Bitcoin sensor."""
167 _attr_attribution =
"Data provided by blockchain.com"
168 _attr_icon =
"mdi:currency-btc"
171 self, data: BitcoinData, currency: str, description: SensorEntityDescription
173 """Initialize the sensor."""
179 """Get the latest data and updates the states."""
181 stats = self.
datadata.stats
182 ticker = self.
datadata.ticker
185 if sensor_type ==
"exchangerate":
188 elif sensor_type ==
"trade_volume_btc":
190 elif sensor_type ==
"miners_revenue_usd":
192 elif sensor_type ==
"btc_mined":
194 elif sensor_type ==
"trade_volume_usd":
196 elif sensor_type ==
"difficulty":
198 elif sensor_type ==
"minutes_between_blocks":
200 elif sensor_type ==
"number_of_transactions":
202 elif sensor_type ==
"hash_rate":
204 elif sensor_type ==
"timestamp":
206 elif sensor_type ==
"mined_blocks":
208 elif sensor_type ==
"blocks_size":
210 elif sensor_type ==
"total_fees_btc":
211 self.
_attr_native_value_attr_native_value = f
"{stats.total_fees_btc * 0.00000001:.2f}"
212 elif sensor_type ==
"total_btc_sent":
213 self.
_attr_native_value_attr_native_value = f
"{stats.total_btc_sent * 0.00000001:.2f}"
214 elif sensor_type ==
"estimated_btc_sent":
215 self.
_attr_native_value_attr_native_value = f
"{stats.estimated_btc_sent * 0.00000001:.2f}"
216 elif sensor_type ==
"total_btc":
218 elif sensor_type ==
"total_blocks":
220 elif sensor_type ==
"next_retarget":
222 elif sensor_type ==
"estimated_transaction_volume_usd":
223 self.
_attr_native_value_attr_native_value = f
"{stats.estimated_transaction_volume_usd:.2f}"
224 elif sensor_type ==
"miners_revenue_btc":
225 self.
_attr_native_value_attr_native_value = f
"{stats.miners_revenue_btc * 0.00000001:.1f}"
226 elif sensor_type ==
"market_price_usd":
231 """Get the latest data and update the states."""
233 stats: statistics.Stats
234 ticker: dict[str, exchangerates.Currency]
237 """Get the latest data from blockchain.com."""
240 self.
tickerticker = exchangerates.get_ticker()
None __init__(self, BitcoinData data, str currency, SensorEntityDescription description)
_attr_native_unit_of_measurement
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)