1- """Event object """
1+ """Event object"""
2+
23from datetime import datetime , timedelta , timezone
34import logging
45import re
@@ -26,15 +27,15 @@ class Event:
2627
2728 def __init__ (self , ** event : dict [str , Any ]):
2829 """Initialize."""
29- self ._convert_phases (cast (dict [str , Any ], event .get ("phases" )))
30+ self ._convert_phases (cast (dict [str , Any ], event .get ("phases" , {} )))
3031 params : dict [str , Any ] = event .get ("parameters" ) or {}
3132 devices : list [dict [str , Any ]] = params .get ("devices" , [])
3233 consumption : dict [str , Any ] = event .get ("consumption" , {})
3334 allowed_wH : int = consumption .get ("baselineWh" , 0 ) or 0
3435 used_wH : int = consumption .get ("currentWh" , 0 ) or 0
3536 self .participating : bool = cast (bool , event .get ("isParticipating" , False ))
3637 self .configurable : bool = cast (bool , event .get ("isConfigurable" , False ))
37- self .period : str = cast (str , event .get ("period" , "" ))
38+ self .period : str = ( cast (str , event .get ("period" , "" )) or "" ). lower ( )
3839 self .event_id : int = cast (int , event ["id" ])
3940 self .total_devices : int = len (devices )
4041 self .opt_out_devices : int = len ([x for x in devices if x ["optOut" ]])
@@ -44,6 +45,7 @@ def __init__(self, **event: dict[str, Any]):
4445 self .allowed_kWh : float = round (allowed_wH / 1000 , 2 )
4546 self .used_kWh : float = round (used_wH / 1000 , 2 )
4647 self .used_percentage : float = 0
48+ self .reward = cast (float , event .get ("reward" , 0.0 ))
4749 self .last_update = datetime .now (timezone .utc ).astimezone ()
4850 if allowed_wH > 0 :
4951 self .used_percentage = round (used_wH / allowed_wH * 100 , 2 )
@@ -63,19 +65,29 @@ def __init__(self, **event: dict[str, Any]):
6365 "used_kWh" ,
6466 "used_percentage" ,
6567 "last_update" ,
68+ "reward" ,
6669 ]
6770
6871 def update_wh (self , used_wH : float ) -> None :
6972 """This function is used to update the used_kWh attribute during a Hilo Challenge Event"""
7073 LOG .debug ("Updating Wh: %s" , used_wH )
7174 self .used_kWh = round (used_wH / 1000 , 2 )
7275 self .last_update = datetime .now (timezone .utc ).astimezone ()
76+ self ._recalculate_percentage ()
7377
7478 def update_allowed_wh (self , allowed_wH : float ) -> None :
7579 """This function is used to update the allowed_kWh attribute during a Hilo Challenge Event"""
7680 LOG .debug ("Updating allowed Wh: %s" , allowed_wH )
7781 self .allowed_kWh = round (allowed_wH / 1000 , 2 )
7882 self .last_update = datetime .now (timezone .utc ).astimezone ()
83+ self ._recalculate_percentage ()
84+
85+ def _recalculate_percentage (self ) -> None :
86+ """Recalculate used percentage based on current values"""
87+ if self .allowed_kWh > 0 :
88+ self .used_percentage = round (self .used_kWh / self .allowed_kWh * 100 , 2 )
89+ else :
90+ self .used_percentage = 0
7991
8092 def should_check_for_allowed_wh (self ) -> bool :
8193 """This function is used to authorize subscribing to a specific event in Hilo to receive the allowed_kWh
0 commit comments