2020 ICON_NEXT_ORDER_TILL , ICON_INFO , ICON_DELIVERY_TIME , ICON_MONTHLY_SPENT
2121from .entity import BaseEntity
2222from .hub import RohlikAccount
23- from .utils import extract_delivery_datetime , calculate_current_month_orders_total , get_earliest_order
23+ from .utils import extract_delivery_datetime , get_earliest_order , parse_delivery_datetime_string
2424
2525SCAN_INTERVAL = timedelta (seconds = 600 )
2626
@@ -441,7 +441,7 @@ def __init__(self, rohlik_account: RohlikAccount) -> None:
441441 super ().__init__ (rohlik_account )
442442 self ._monthly_total : float = 0.0
443443 self ._processed_orders : set [str ] = set () # Store order IDs
444- self ._current_month : str = datetime .now ().strftime ("%Y-%m" )
444+ self ._current_month : str = datetime .now (ZoneInfo ( "Europe/Prague" ) ).strftime ("%Y-%m" )
445445 self ._last_reset : datetime | None = None
446446
447447 def _is_order_final (self , order : dict ) -> bool :
@@ -480,18 +480,18 @@ async def async_added_to_hass(self) -> None:
480480 if (last_state := await self .async_get_last_state ()) is not None :
481481 self ._monthly_total = last_state .attributes .get ("monthly_total" , 0.0 )
482482 self ._processed_orders = set (last_state .attributes .get ("processed_orders" , []))
483- self ._current_month = last_state .attributes .get ("current_month" , datetime .now ().strftime ("%Y-%m" ))
483+ self ._current_month = last_state .attributes .get ("current_month" , datetime .now (ZoneInfo ( "Europe/Prague" ) ).strftime ("%Y-%m" ))
484484 if last_reset_str := last_state .attributes .get ("last_reset" ):
485485 self ._last_reset = datetime .fromisoformat (last_reset_str )
486486
487487 self ._check_and_reset_month ()
488488 self ._process_new_orders ()
489489
490- self ._rohlik_account .register_callback (self .async_write_ha_state )
490+ self ._rohlik_account .register_callback (self ._handle_coordinator_update )
491491
492492 def _check_and_reset_month (self ) -> None :
493493 """Reset total if month changed."""
494- current_month = datetime .now ().strftime ("%Y-%m" )
494+ current_month = datetime .now (ZoneInfo ( "Europe/Prague" ) ).strftime ("%Y-%m" )
495495 if current_month != self ._current_month :
496496 _LOGGER .info (f"Month changed from { self ._current_month } to { current_month } , resetting monthly total" )
497497 self ._monthly_total = 0.0
@@ -509,7 +509,7 @@ def _process_new_orders(self) -> None:
509509 if not orders :
510510 return
511511
512- current_month_pattern = datetime .now ().strftime ("%Y-%m-" )
512+ current_month_pattern = datetime .now (ZoneInfo ( "Europe/Prague" ) ).strftime ("%Y-%m-" )
513513 new_orders_count = 0
514514
515515 for order in orders :
@@ -554,11 +554,15 @@ def _process_new_orders(self) -> None:
554554 if new_orders_count > 0 :
555555 _LOGGER .info (f"Processed { new_orders_count } new order(s). Monthly total: { self ._monthly_total } CZK" )
556556
557+ async def _handle_coordinator_update (self ) -> None :
558+ """Handle coordinator updates by processing new orders and updating state."""
559+ self ._check_and_reset_month ()
560+ self ._process_new_orders ()
561+ await self .async_write_ha_state ()
562+
557563 @property
558564 def native_value (self ) -> float | None :
559565 """Returns amount spent in current month."""
560- self ._check_and_reset_month ()
561- self ._process_new_orders ()
562566 return self ._monthly_total if self ._monthly_total > 0 else 0.0
563567
564568 @property
@@ -577,7 +581,7 @@ def icon(self) -> str:
577581 return ICON_MONTHLY_SPENT
578582
579583 async def async_will_remove_from_hass (self ) -> None :
580- self ._rohlik_account .remove_callback (self .async_write_ha_state )
584+ self ._rohlik_account .remove_callback (self ._handle_coordinator_update )
581585
582586
583587class NoLimitOrders (BaseEntity , SensorEntity ):
@@ -735,18 +739,8 @@ def native_value(self) -> datetime | None:
735739 """Returns start of delivery window for the earliest order."""
736740 earliest_order = get_earliest_order (self ._rohlik_account .data .get ('next_order' , []))
737741 if earliest_order :
738- try :
739- slot_start = datetime .strptime (earliest_order .get ("deliverySlot" , {}).get ("since" , None ),
740- "%Y-%m-%dT%H:%M:%S.%f%z" )
741- return slot_start
742- except (ValueError , TypeError ):
743- # Try without microseconds if the format doesn't match
744- try :
745- slot_start = datetime .strptime (earliest_order .get ("deliverySlot" , {}).get ("since" , None ),
746- "%Y-%m-%dT%H:%M:%S%z" )
747- return slot_start
748- except (ValueError , TypeError ):
749- return None
742+ since_str = earliest_order .get ("deliverySlot" , {}).get ("since" , None )
743+ return parse_delivery_datetime_string (since_str )
750744 return None
751745
752746 @property
@@ -771,18 +765,8 @@ def native_value(self) -> datetime | None:
771765 """Returns end of delivery window for the earliest order."""
772766 earliest_order = get_earliest_order (self ._rohlik_account .data .get ('next_order' , []))
773767 if earliest_order :
774- try :
775- slot_end = datetime .strptime (earliest_order .get ("deliverySlot" , {}).get ("till" , None ),
776- "%Y-%m-%dT%H:%M:%S.%f%z" )
777- return slot_end
778- except (ValueError , TypeError ):
779- # Try without microseconds if the format doesn't match
780- try :
781- slot_end = datetime .strptime (earliest_order .get ("deliverySlot" , {}).get ("till" , None ),
782- "%Y-%m-%dT%H:%M:%S%z" )
783- return slot_end
784- except (ValueError , TypeError ):
785- return None
768+ till_str = earliest_order .get ("deliverySlot" , {}).get ("till" , None )
769+ return parse_delivery_datetime_string (till_str )
786770 return None
787771
788772 @property
0 commit comments