1010import json
1111import time
1212import traceback
13- from datetime import datetime , timedelta
13+ from datetime import datetime , timedelta , timezone
14+ from dateutil import parser as dtparser
1415
1516from .const .const import (
1617 _LOGGER ,
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
0 commit comments