10
10
11
11
12
12
class Event :
13
+ """This class is used to populate the data of a Hilo Challenge Event, contains datetime info and consumption data"""
13
14
setting_deadline : datetime
14
15
pre_cold_start : datetime
15
16
pre_cold_end : datetime
@@ -62,13 +63,28 @@ def __init__(self, **event: dict[str, Any]):
62
63
"last_update" ,
63
64
]
64
65
66
+ def update_wh (self , used_wH : float ) -> None :
67
+ """This function is used to update the used_kWh attribute during a Hilo Challenge Event"""
68
+ LOG .debug (f"Updating Wh: { used_wH } " )
69
+ self .used_kWh = round (used_wH / 1000 , 2 )
70
+ self .last_update = datetime .now (timezone .utc ).astimezone ()
71
+
72
+ def should_check_for_allowed_wh (self ) -> bool :
73
+ """This function is used to authorize subscribing to a specific event in Hilo to receive the allowed_kWh
74
+ that is made available in the pre_heat phase"""
75
+ now = datetime .now (self .preheat_start .tzinfo )
76
+ time_since_preheat_start = (self .preheat_start - now ).total_seconds ()
77
+ already_has_allowed_wh = self .allowed_kWh > 0
78
+ return 1800 <= time_since_preheat_start <= 2700 and not already_has_allowed_wh
79
+
65
80
def as_dict (self ) -> dict [str , Any ]:
66
81
rep = {k : getattr (self , k ) for k in self .dict_items }
67
82
rep ["phases" ] = {k : getattr (self , k ) for k in self .phases_list }
68
83
rep ["state" ] = self .state
69
84
return rep
70
85
71
86
def _convert_phases (self , phases : dict [str , Any ]) -> None :
87
+ """Formats phase times for later use"""
72
88
self .phases_list = []
73
89
for key , value in phases .items ():
74
90
phase_match = re .match (r"(.*)(DateUTC|Utc)" , key )
@@ -89,6 +105,7 @@ def _convert_phases(self, phases: dict[str, Any]) -> None:
89
105
def _create_phases (
90
106
self , hours : int , phase_name : str , parent_phase : str
91
107
) -> datetime :
108
+ """Creates optional "appreciation" and "pre_cold" phases according to Hilo phases datetimes"""
92
109
parent_start = getattr (self , f"{ parent_phase } _start" )
93
110
phase_start = f"{ phase_name } _start"
94
111
phase_end = f"{ phase_name } _end"
@@ -128,6 +145,7 @@ def current_phase_times(self) -> dict[str, datetime]:
128
145
129
146
@property
130
147
def state (self ) -> str :
148
+ """Defines state in the next_event attribute"""
131
149
now = datetime .now (self .preheat_start .tzinfo )
132
150
if self .pre_cold_start and self .pre_cold_start <= now < self .pre_cold_end :
133
151
return "pre_cold"
0 commit comments