Skip to content

Commit

Permalink
Add mempool.space fees sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHolyRoger committed Jan 10, 2024
1 parent fd67a1c commit 92b61ca
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Version
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ Version: 0.2.6 20230425 - Add mempool avg fee per TX child sensor
Version: 0.3.0 20230426 - Rename to Cryptoinfo Advanced
Version: 0.3.1 20230426 - Fixes for HACS/HASS requirements
Version: 0.3.2 20230507 - Add total unknown hash control sensor to chain control, mempool total fee calculated.
Version: 0.3.3 20230521 - Fix a few errors during updates.
Version: 0.3.3 20230521 - Fix a few errors during updates.
Version: 0.3.4 20240110 - Add mempool.space fees sensors.
2 changes: 1 addition & 1 deletion custom_components/cryptoinfo_advanced/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.3"
__version__ = "0.3.4"
6 changes: 6 additions & 0 deletions custom_components/cryptoinfo_advanced/const/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
ATTR_MEMPOOL_TOTAL_FEE_CALC = "mempool_total_fee_calc"
ATTR_MEMPOOL_SIZE_CALC = "mempool_size_calc"
ATTR_MEMPOOL_AVERAGE_FEE_PER_TX = "mempool_average_fee_per_tx"
ATTR_MEMPOOL_FEES_FASTEST = "mempool_fees_fastest"
ATTR_MEMPOOL_FEES_30MIN = "mempool_fees_30min"
ATTR_MEMPOOL_FEES_60MIN = "mempool_fees_60min"
ATTR_MEMPOOL_FEES_ECO = "mempool_fees_eco"
ATTR_MEMPOOL_FEES_MINIMUM = "mempool_fees_minimum"

PROPERTY_POOL_CONTROL_REMAINING = "remaining_percentage"

Expand All @@ -93,6 +98,7 @@
API_ENDPOINT_CHAIN_BLOCK_TIME = "{0}{1}/api.dws?q=getblocktime&height={2}"
API_ENDPOINT_NOMP_POOL_STATS = "https://{0}/api/stats"
API_ENDPOINT_MEMPOOL_STATS = "{0}mempool"
API_ENDPOINT_MEMPOOL_FEES = "{0}v1/fees/recommended"

DAY_SECONDS = 60 * 60 * 24

Expand Down
64 changes: 63 additions & 1 deletion custom_components/cryptoinfo_advanced/crypto_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
ATTR_BLOCKS_CONFIRMED,
ATTR_BLOCKS_ORPHANED,
ATTR_BLOCK_TIME_IN_SECONDS,
ATTR_MEMPOOL_FEES_FASTEST,
ATTR_MEMPOOL_FEES_30MIN,
ATTR_MEMPOOL_FEES_60MIN,
ATTR_MEMPOOL_FEES_ECO,
ATTR_MEMPOOL_FEES_MINIMUM,
ATTR_MEMPOOL_TX_COUNT,
ATTR_MEMPOOL_TOTAL_FEE,
ATTR_MEMPOOL_TOTAL_FEE_CALC,
Expand All @@ -70,6 +75,7 @@
API_ENDPOINT_CHAIN_ORPHANS,
API_ENDPOINT_CHAIN_BLOCK_TIME,
API_ENDPOINT_NOMP_POOL_STATS,
API_ENDPOINT_MEMPOOL_FEES,
API_ENDPOINT_MEMPOOL_STATS,
CONF_DIFF_MULTIPLIER,
CONF_BLOCK_TIME_MINUTES,
Expand Down Expand Up @@ -188,6 +194,11 @@ def __init__(
self._blocks_orphaned = None
self._mempool_tx_count = None
self._mempool_total_fee = None
self._mempool_fees_fastest = None
self._mempool_fees_30min = None
self._mempool_fees_60min = None
self._mempool_fees_eco = None
self._mempool_fees_minimum = None

@property
def is_child_sensor(self):
Expand Down Expand Up @@ -428,6 +439,13 @@ def get_extra_state_attrs(self, full_attr_force=False):
output_attrs[ATTR_MEMPOOL_TX_COUNT] = self._mempool_tx_count
output_attrs[ATTR_MEMPOOL_TOTAL_FEE] = self._mempool_total_fee

if full_attr_force or self._fetch_type == CryptoInfoAdvDataFetchType.MEMPOOL_FEES:
output_attrs[ATTR_MEMPOOL_FEES_FASTEST] = self._mempool_fees_fastest
output_attrs[ATTR_MEMPOOL_FEES_30MIN] = self._mempool_fees_30min
output_attrs[ATTR_MEMPOOL_FEES_60MIN] = self._mempool_fees_60min
output_attrs[ATTR_MEMPOOL_FEES_ECO] = self._mempool_fees_eco
output_attrs[ATTR_MEMPOOL_FEES_MINIMUM] = self._mempool_fees_minimum

return output_attrs

@property
Expand Down Expand Up @@ -595,7 +613,7 @@ def check_valid_config(self, raise_error=True):

return False

if self._fetch_type == CryptoInfoAdvDataFetchType.MEMPOOL_STATS:
if self._fetch_type in CryptoInfoAdvEntityManager.instance().fetch_mempool_types:

if self.cryptocurrency_name.lower() not in ['btc', 'bitcoin']:
_LOGGER.error(f"Sensor {self.name} is not BTC, mempool is only supported for BTC.")
Expand Down Expand Up @@ -733,6 +751,12 @@ def _extract_data_mempool_stats_full(self, json_data):
def _extract_data_mempool_stats_primary(self, api_data):
return int(api_data["vsize"])

def _extract_data_mempool_fees_full(self, json_data):
return json_data

def _extract_data_mempool_fees_primary(self, api_data):
return int(api_data["fastestFee"])

async def _fetch_price_data_main(self, api_data=None):
if not self._fetch_type == CryptoInfoAdvDataFetchType.PRICE_MAIN:
raise ValueError()
Expand Down Expand Up @@ -974,6 +998,31 @@ async def _fetch_mempool_stats(self, api_data=None):

return self.data

async def _fetch_mempool_fees(self, api_data=None):
self.check_valid_config()

mempool_data, api_data = await self._async_api_fetch(
api_data,
API_ENDPOINT_MEMPOOL_FEES.format(API_BASE_URL_MEMPOOLSPACE),
self._extract_data_mempool_fees_full,
self._extract_data_mempool_fees_primary
)

if mempool_data is not None:
self._update_all_properties(
state=int(mempool_data),
mempool_fees_fastest=int(api_data["fastestFee"]),
mempool_fees_30min=int(api_data["halfHourFee"]),
mempool_fees_60min=int(api_data["hourFee"]),
mempool_fees_eco=int(api_data["economyFee"]),
mempool_fees_minimum=int(api_data["minimumFee"]),
)

else:
raise ValueError()

return self.data

def _render_fetch_args(self):
if self._fetch_args is None:
return None
Expand Down Expand Up @@ -1052,6 +1101,11 @@ def _update_all_properties(
blocks_orphaned=None,
mempool_tx_count=None,
mempool_total_fee=None,
mempool_fees_fastest=None,
mempool_fees_30min=None,
mempool_fees_60min=None,
mempool_fees_eco=None,
mempool_fees_minimum=None,
available=True,
):
self._state = state
Expand Down Expand Up @@ -1080,6 +1134,11 @@ def _update_all_properties(
self._blocks_orphaned = blocks_orphaned
self._mempool_tx_count = mempool_tx_count
self._mempool_total_fee = mempool_total_fee
self._mempool_fees_fastest = mempool_fees_fastest
self._mempool_fees_30min = mempool_fees_30min
self._mempool_fees_60min = mempool_fees_60min
self._mempool_fees_eco = mempool_fees_eco
self._mempool_fees_minimum = mempool_fees_minimum
self._attr_available = available

self._update_child_sensors()
Expand Down Expand Up @@ -1158,6 +1217,9 @@ async def _async_update(self):
elif self._fetch_type == CryptoInfoAdvDataFetchType.MEMPOOL_STATS:
api_data = await self._fetch_mempool_stats(api_data)

elif self._fetch_type == CryptoInfoAdvDataFetchType.MEMPOOL_FEES:
api_data = await self._fetch_mempool_fees(api_data)

else:
api_data = await self._fetch_price_data_main(api_data)

Expand Down
9 changes: 9 additions & 0 deletions custom_components/cryptoinfo_advanced/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class CryptoInfoAdvDataFetchType:
CHAIN_BLOCK_TIME = CryptoInfoAdvFetchProp("chain_block_time")
NOMP_POOL_STATS = CryptoInfoAdvFetchProp("nomp_pool_stats")
MEMPOOL_STATS = CryptoInfoAdvFetchProp("mempool_stats")
MEMPOOL_FEES = CryptoInfoAdvFetchProp("mempool_fees")


class CryptoInfoAdvEntityManager:
Expand Down Expand Up @@ -106,6 +107,7 @@ def fetch_types(self):
CryptoInfoAdvDataFetchType.CHAIN_BLOCK_TIME,
CryptoInfoAdvDataFetchType.NOMP_POOL_STATS,
CryptoInfoAdvDataFetchType.MEMPOOL_STATS,
CryptoInfoAdvDataFetchType.MEMPOOL_FEES,
]

@property
Expand Down Expand Up @@ -158,6 +160,13 @@ def fetch_shared_types(self):
CryptoInfoAdvDataFetchType.CHAIN_CONTROL,
]

@property
def fetch_mempool_types(self):
return [
CryptoInfoAdvDataFetchType.MEMPOOL_STATS,
CryptoInfoAdvDataFetchType.MEMPOOL_FEES,
]

def get_extra_sensor_fetch_type_from_str(self, parent_sensor, attribute_key):
for t in self._extra_sensor_types:
if t == attribute_key:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/cryptoinfo_advanced/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/TheHolyRoger/hass-cryptoinfo/issues",
"requirements": [],
"version": "0.3.3"
"version": "0.3.4"
}
20 changes: 20 additions & 0 deletions example/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ sensor:
id: "BTC Mempool Size MB"
unit_of_measurement: "MB"

- platform: cryptoinfo_advanced
id: "BTC Mempool Fees - Fastest"
cryptocurrency_name: "btc"
unit_of_measurement: "sats"
update_frequency: 1
api_mode: "mempool_fees"
extra_sensors:
- property: mempool_fees_30min
id: "BTC Mempool Fees - 30min"
unit_of_measurement: "sats"
- property: mempool_fees_60min
id: "BTC Mempool Fees - 60min"
unit_of_measurement: "sats"
- property: mempool_fees_eco
id: "BTC Mempool Fees - Economy"
unit_of_measurement: "sats"
- property: mempool_fees_minimum
id: "BTC Mempool Fees - Minimum"
unit_of_measurement: "sats"



# ROGER NOMP Pool stats - API: NOMP (Any NOMP based pool)
Expand Down

0 comments on commit 92b61ca

Please sign in to comment.