Skip to content

Commit

Permalink
Add max fetch failures with config option
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHolyRoger committed Apr 11, 2024
1 parent 15c7dad commit a67c035
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Version
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ Version: 0.3.2 20230507 - Add total unknown hash control sensor to chain control
Version: 0.3.3 20230521 - Fix a few errors during updates.
Version: 0.3.4 20240110 - Add mempool.space fees sensors.
Version: 0.3.5 20240110 - Add mempool.space Next Block sensors.
Version: 0.3.6 20240119 - Add days since ATH sensors, fix image_url attribute.
Version: 0.3.6 20240119 - Add days since ATH sensors, fix image_url attribute.
Version: 0.3.7 20240411 - Add max fetch failures with config option
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.6"
__version__ = "0.3.7"
3 changes: 3 additions & 0 deletions custom_components/cryptoinfo_advanced/const/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
CONF_BLOCK_TIME_MINUTES = "block_time_minutes"
CONF_DIFFICULTY_WINDOW = "difficulty_window"
CONF_HALVING_WINDOW = "halving_window"
CONF_MAX_FETCH_FAILURES = "max_fetch_failures"

SENSOR_PREFIX = "Cryptoinfo "
ATTR_LAST_UPDATE = "last_update"
Expand Down Expand Up @@ -116,6 +117,8 @@

DAY_SECONDS = 60 * 60 * 24

DEFAULT_MAX_FETCH_FAILURES = 3

DEFAULT_CHAIN_DIFFICULTY_WINDOW = 2016
DEFAULT_CHAIN_DIFF_MULTIPLIER = 4294967296
DEFAULT_CHAIN_BLOCK_TIME_MINS = 10.0
Expand Down
17 changes: 15 additions & 2 deletions custom_components/cryptoinfo_advanced/crypto_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
DEFAULT_CHAIN_DIFF_MULTIPLIER,
DEFAULT_CHAIN_BLOCK_TIME_MINS,
DEFAULT_CHAIN_HALVING_WINDOW,
DEFAULT_MAX_FETCH_FAILURES,
DAY_SECONDS,
PROPERTY_POOL_CONTROL_REMAINING,
)
Expand Down Expand Up @@ -146,6 +147,7 @@ def __init__(
block_time_minutes="",
difficulty_window="",
halving_window="",
max_fetch_failures=None,
is_child_sensor=False,
):
# Internal Properties
Expand All @@ -161,6 +163,7 @@ def __init__(
".", "", 1).isdigit() else DEFAULT_CHAIN_BLOCK_TIME_MINS
self._difficulty_window = int(difficulty_window) if difficulty_window.isdigit() else DEFAULT_CHAIN_DIFFICULTY_WINDOW
self._halving_window = int(halving_window) if halving_window.isdigit() else DEFAULT_CHAIN_HALVING_WINDOW
self._max_fetch_failures = int(max_fetch_failures) if max_fetch_failures is not None else DEFAULT_MAX_FETCH_FAILURES
self._internal_id_name = id_name if id_name is not None else ""
self._fetch_type = CryptoInfoAdvEntityManager.instance().get_fetch_type_from_str(api_mode)
self._fetch_args = fetch_args if fetch_args and len(fetch_args) else None
Expand All @@ -170,6 +173,7 @@ def __init__(
self._is_child_sensor = is_child_sensor
self._child_sensors = list()
self._child_sensor_config = extra_sensors
self._fetch_failure_count = 0

# HASS Attributes
self.async_update = Throttle(update_frequency)(self._async_update)
Expand Down Expand Up @@ -1253,6 +1257,8 @@ def _update_all_properties(
mempool_next_block_fee_range_max=None,
available=True,
):
if available:
self._fetch_failure_count = 0
self._state = state
self._last_update = datetime.today().strftime("%d-%m-%Y %H:%M")
self._base_price = base_price
Expand Down Expand Up @@ -1351,6 +1357,12 @@ def init_child_sensors(self):

return child_sensors

def _process_failed_fetch(self):
self._fetch_failure_count = self._fetch_failure_count + 1

if self._fetch_failure_count >= self._max_fetch_failures:
self._update_all_properties(available=False)

async def _async_update(self):
api_data = None

Expand Down Expand Up @@ -1392,7 +1404,7 @@ async def _async_update(self):
try:
api_data = await self._fetch_price_data_alternate(api_data)
except ValueError:
self._update_all_properties(available=False)
self._process_failed_fetch()
return

CryptoInfoAdvEntityManager.instance().set_cached_entity_data(self, api_data)
Expand Down Expand Up @@ -1426,6 +1438,7 @@ def __init__(
extra_sensors="",
api_domain_name="",
pool_name=parent_sensor._pool_name,
max_fetch_failures=parent_sensor._max_fetch_failures,
is_child_sensor=True,
)

Expand All @@ -1446,4 +1459,4 @@ def _update(self):
self._update_all_properties(state=new_state)

elif new_state is None:
self._update_all_properties(available=False)
self._process_failed_fetch()
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.6"
"version": "0.3.7"
}
5 changes: 5 additions & 0 deletions custom_components/cryptoinfo_advanced/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .const.const import (
_LOGGER,
DEFAULT_MAX_FETCH_FAILURES,
DOMAIN,
PLATFORMS,
CONF_CRYPTOCURRENCY_NAME,
Expand All @@ -26,6 +27,7 @@
CONF_BLOCK_TIME_MINUTES,
CONF_DIFFICULTY_WINDOW,
CONF_HALVING_WINDOW,
CONF_MAX_FETCH_FAILURES,
)

from .manager import CryptoInfoAdvEntityManager, CryptoInfoAdvDataFetchType
Expand Down Expand Up @@ -69,6 +71,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
block_time_minutes = config.get(CONF_BLOCK_TIME_MINUTES)
difficulty_window = config.get(CONF_DIFFICULTY_WINDOW)
halving_window = config.get(CONF_HALVING_WINDOW)
max_fetch_failures = config.get(CONF_MAX_FETCH_FAILURES)

entities = []

Expand All @@ -93,6 +96,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
block_time_minutes,
difficulty_window,
halving_window,
max_fetch_failures,
)
if new_sensor.check_valid_config(False):
entities.append(new_sensor)
Expand Down Expand Up @@ -123,6 +127,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
cv.ensure_list,
[cv.string],
),
vol.Optional(CONF_MAX_FETCH_FAILURES, default=DEFAULT_MAX_FETCH_FAILURES): cv.positive_int,
vol.Optional(CONF_FETCH_ARGS, default=""): cv.string,
vol.Optional(CONF_EXTRA_SENSORS): vol.All(
cv.ensure_list,
Expand Down
1 change: 1 addition & 0 deletions example/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sensor:
currency_name: "usd"
unit_of_measurement: "$"
update_frequency: 1
max_fetch_failures: 3
extra_sensors:
- property: "all_time_high"
unit_of_measurement: "$"
Expand Down

0 comments on commit a67c035

Please sign in to comment.