From a67c035017c0d5c0c6b6af371e2e8c6775ec3b52 Mon Sep 17 00:00:00 2001 From: TheHolyRoger Date: Thu, 11 Apr 2024 15:03:22 +0100 Subject: [PATCH] Add max fetch failures with config option --- Version | 3 ++- .../cryptoinfo_advanced/__init__.py | 2 +- .../cryptoinfo_advanced/const/const.py | 3 +++ .../cryptoinfo_advanced/crypto_sensor.py | 17 +++++++++++++++-- .../cryptoinfo_advanced/manifest.json | 2 +- custom_components/cryptoinfo_advanced/sensor.py | 5 +++++ example/configuration.yaml | 1 + 7 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Version b/Version index e773990..171de98 100644 --- a/Version +++ b/Version @@ -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. \ No newline at end of file +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 \ No newline at end of file diff --git a/custom_components/cryptoinfo_advanced/__init__.py b/custom_components/cryptoinfo_advanced/__init__.py index d7b30e1..8879c6c 100644 --- a/custom_components/cryptoinfo_advanced/__init__.py +++ b/custom_components/cryptoinfo_advanced/__init__.py @@ -1 +1 @@ -__version__ = "0.3.6" +__version__ = "0.3.7" diff --git a/custom_components/cryptoinfo_advanced/const/const.py b/custom_components/cryptoinfo_advanced/const/const.py index bcfe346..42cdc3c 100644 --- a/custom_components/cryptoinfo_advanced/const/const.py +++ b/custom_components/cryptoinfo_advanced/const/const.py @@ -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" @@ -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 diff --git a/custom_components/cryptoinfo_advanced/crypto_sensor.py b/custom_components/cryptoinfo_advanced/crypto_sensor.py index 611e7a3..2e2d274 100644 --- a/custom_components/cryptoinfo_advanced/crypto_sensor.py +++ b/custom_components/cryptoinfo_advanced/crypto_sensor.py @@ -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, ) @@ -146,6 +147,7 @@ def __init__( block_time_minutes="", difficulty_window="", halving_window="", + max_fetch_failures=None, is_child_sensor=False, ): # Internal Properties @@ -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 @@ -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) @@ -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 @@ -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 @@ -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) @@ -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, ) @@ -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() diff --git a/custom_components/cryptoinfo_advanced/manifest.json b/custom_components/cryptoinfo_advanced/manifest.json index 60c1110..11dce49 100644 --- a/custom_components/cryptoinfo_advanced/manifest.json +++ b/custom_components/cryptoinfo_advanced/manifest.json @@ -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" } \ No newline at end of file diff --git a/custom_components/cryptoinfo_advanced/sensor.py b/custom_components/cryptoinfo_advanced/sensor.py index f414e18..69280fa 100644 --- a/custom_components/cryptoinfo_advanced/sensor.py +++ b/custom_components/cryptoinfo_advanced/sensor.py @@ -9,6 +9,7 @@ from .const.const import ( _LOGGER, + DEFAULT_MAX_FETCH_FAILURES, DOMAIN, PLATFORMS, CONF_CRYPTOCURRENCY_NAME, @@ -26,6 +27,7 @@ CONF_BLOCK_TIME_MINUTES, CONF_DIFFICULTY_WINDOW, CONF_HALVING_WINDOW, + CONF_MAX_FETCH_FAILURES, ) from .manager import CryptoInfoAdvEntityManager, CryptoInfoAdvDataFetchType @@ -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 = [] @@ -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) @@ -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, diff --git a/example/configuration.yaml b/example/configuration.yaml index 407904b..0cf882c 100644 --- a/example/configuration.yaml +++ b/example/configuration.yaml @@ -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: "$"