Skip to content

Commit 036257e

Browse files
committed
Add days since ATH sensors, fix image_url attribute.
1 parent be997dc commit 036257e

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

Version

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ Version: 0.3.1 20230426 - Fixes for HACS/HASS requirements
3131
Version: 0.3.2 20230507 - Add total unknown hash control sensor to chain control, mempool total fee calculated.
3232
Version: 0.3.3 20230521 - Fix a few errors during updates.
3333
Version: 0.3.4 20240110 - Add mempool.space fees sensors.
34-
Version: 0.3.5 20240110 - Add mempool.space Next Block sensors.
34+
Version: 0.3.5 20240110 - Add mempool.space Next Block sensors.
35+
Version: 0.3.6 20240119 - Add days since ATH sensors, fix image_url attribute.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.5"
1+
__version__ = "0.3.6"

custom_components/cryptoinfo_advanced/const/const.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
ATTR_24H_LOW = "24h_low"
4040
ATTR_24H_HIGH = "24h_high"
4141
ATTR_IMAGE_URL = "image_url"
42+
ATTR_ALL_TIME_HIGH_DATE = "all_time_high_date"
43+
ATTR_ALL_TIME_HIGH_DAYS = "all_time_high_days"
44+
ATTR_ALL_TIME_LOW_DATE = "all_time_low_date"
45+
ATTR_ALL_TIME_LOW_DAYS = "all_time_low_days"
4246

4347
ATTR_DIFFICULTY = "difficulty"
4448
ATTR_DIFFICULTY_CALC = "difficulty_calc"

custom_components/cryptoinfo_advanced/crypto_sensor.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
import json
1111
import time
1212
import traceback
13-
from datetime import datetime, timedelta
13+
from datetime import datetime, timedelta, timezone
14+
from dateutil import parser as dtparser
1415

1516
from .const.const import (
1617
_LOGGER,
@@ -27,8 +28,12 @@
2728
ATTR_CIRCULATING_SUPPLY,
2829
ATTR_TOTAL_SUPPLY,
2930
ATTR_ALL_TIME_HIGH,
31+
ATTR_ALL_TIME_HIGH_DATE,
32+
ATTR_ALL_TIME_HIGH_DAYS,
3033
ATTR_ALL_TIME_HIGH_DISTANCE,
3134
ATTR_ALL_TIME_LOW,
35+
ATTR_ALL_TIME_LOW_DATE,
36+
ATTR_ALL_TIME_LOW_DAYS,
3237
ATTR_24H_LOW,
3338
ATTR_24H_HIGH,
3439
ATTR_IMAGE_URL,
@@ -193,6 +198,8 @@ def __init__(
193198
self._24h_low = None
194199
self._24h_high = None
195200
self._image_url = None
201+
self._ath_date = None
202+
self._atl_date = None
196203
self._difficulty = None
197204
self._hashrate = None
198205
self._pool_control_1000b = None
@@ -413,6 +420,24 @@ def all_time_high_distance(self):
413420

414421
return round(float(self._all_time_high) - self.state, 2)
415422

423+
@property
424+
def all_time_high_days(self):
425+
if self._ath_date is None:
426+
return None
427+
428+
date_diff = datetime.now(timezone.utc) - self._ath_date
429+
430+
return date_diff.days
431+
432+
@property
433+
def all_time_low_days(self):
434+
if self._atl_date is None:
435+
return None
436+
437+
date_diff = datetime.now(timezone.utc) - self._atl_date
438+
439+
return date_diff.days
440+
416441
@property
417442
def pool_control_1000b_perc(self):
418443
if self._pool_control_1000b is None:
@@ -441,6 +466,8 @@ def get_extra_state_attrs(self, full_attr_force=False):
441466
output_attrs[ATTR_24H_LOW] = self._24h_low
442467
output_attrs[ATTR_24H_HIGH] = self._24h_high
443468
output_attrs[ATTR_IMAGE_URL] = self._image_url
469+
output_attrs[ATTR_ALL_TIME_HIGH_DATE] = self._ath_date
470+
output_attrs[ATTR_ALL_TIME_LOW_DATE] = self._atl_date
444471

445472
if full_attr_force or self._fetch_type in CryptoInfoAdvEntityManager.instance().fetch_supply_types:
446473
output_attrs[ATTR_CIRCULATING_SUPPLY] = self._circulating_supply
@@ -578,6 +605,12 @@ def get_extra_sensor_attrs(self, full_attr_force=False, child_sensor=None):
578605
if child_sensor is None or child_sensor.attribute_key == ATTR_ALL_TIME_HIGH_DISTANCE:
579606
output_attrs[ATTR_ALL_TIME_HIGH_DISTANCE] = self.all_time_high_distance
580607

608+
if child_sensor is None or child_sensor.attribute_key == ATTR_ALL_TIME_HIGH_DAYS:
609+
output_attrs[ATTR_ALL_TIME_HIGH_DAYS] = self.all_time_high_days
610+
611+
if child_sensor is None or child_sensor.attribute_key == ATTR_ALL_TIME_LOW_DAYS:
612+
output_attrs[ATTR_ALL_TIME_LOW_DAYS] = self.all_time_low_days
613+
581614
if full_attr_force or self._fetch_type == CryptoInfoAdvDataFetchType.CHAIN_CONTROL:
582615

583616
if child_sensor is None or child_sensor.attribute_key == ATTR_POOL_CONTROL_1000B_PERC:
@@ -855,6 +888,8 @@ async def _fetch_price_data_main(self, api_data=None):
855888
low_24h=api_data["low_24h"],
856889
high_24h=api_data["high_24h"],
857890
image_url=api_data["image"],
891+
ath_date=api_data.get("ath_date"),
892+
atl_date=api_data.get("atl_date"),
858893
)
859894

860895
else:
@@ -1192,6 +1227,8 @@ def _update_all_properties(
11921227
low_24h=None,
11931228
high_24h=None,
11941229
image_url=None,
1230+
ath_date=None,
1231+
atl_date=None,
11951232
difficulty=None,
11961233
hashrate=None,
11971234
pool_control_1000b=None,
@@ -1231,6 +1268,7 @@ def _update_all_properties(
12311268
self._all_time_low = all_time_low
12321269
self._24h_low = low_24h
12331270
self._24h_high = high_24h
1271+
self._image_url = image_url
12341272
self._difficulty = difficulty
12351273
self._hashrate = hashrate
12361274
self._pool_control_1000b = pool_control_1000b
@@ -1254,6 +1292,16 @@ def _update_all_properties(
12541292
self._mempool_next_block_fee_range_min = mempool_next_block_fee_range_min
12551293
self._mempool_next_block_fee_range_max = mempool_next_block_fee_range_max
12561294
self._attr_available = available
1295+
if ath_date and len(ath_date) > 0:
1296+
try:
1297+
self._ath_date = dtparser.parse(ath_date)
1298+
except Exception:
1299+
self._ath_date = None
1300+
if atl_date and len(atl_date) > 0:
1301+
try:
1302+
self._atl_date = dtparser.parse(atl_date)
1303+
except Exception:
1304+
self._atl_date = None
12571305

12581306
self._update_child_sensors()
12591307

custom_components/cryptoinfo_advanced/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"iot_class": "cloud_polling",
1111
"issue_tracker": "https://github.com/TheHolyRoger/hass-cryptoinfo/issues",
1212
"requirements": [],
13-
"version": "0.3.5"
13+
"version": "0.3.6"
1414
}

example/configuration.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ sensor:
3939
unit_of_measurement: "$"
4040
- property: "all_time_high_distance"
4141
unit_of_measurement: "$"
42+
- property: "all_time_high_days"
43+
unit_of_measurement: "days"
4244

4345

4446
# BTC Price GBP - API: CoinGecko

0 commit comments

Comments
 (0)