Skip to content

Commit fa10776

Browse files
committed
refactor: add yearly enriched order count method and update sensor responses
1 parent 85aa74e commit fa10776

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

custom_components/rohlikcz/hub.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def yearly_count(self, year: str) -> int:
177177
"""Count of orders for a given year."""
178178
return sum(1 for o in self._data["orders"].values() if o["date"].startswith(year))
179179

180+
def yearly_enriched_count(self, year: str) -> int:
181+
"""Count of enriched orders (with item details) for a given year."""
182+
return sum(1 for o in self._data["orders"].values() if o["date"].startswith(year) and "items" in o)
183+
180184
def alltime_total(self) -> float:
181185
"""Sum of all order amounts."""
182186
return sum(o["amount"] for o in self._data["orders"].values())

custom_components/rohlikcz/sensor.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,12 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
758758
year = datetime.now(ZoneInfo("Europe/Prague")).strftime("%Y")
759759
categories = store.category_totals(year=year, level=1, hide_discontinued=self._rohlik_account.hide_discontinued)
760760
if not categories:
761-
return {"year": year, "enriched_orders": store.enriched_count, "total_orders": store.yearly_count(year)}
761+
return {"year": year, "enriched_orders": store.yearly_enriched_count(year), "total_orders": store.yearly_count(year)}
762762
return {
763763
"year": year,
764764
"total_count": len(categories),
765765
"categories": categories[:self._rohlik_account.top_n],
766-
"enriched_orders": store.enriched_count,
766+
"enriched_orders": store.yearly_enriched_count(year),
767767
"total_orders": store.yearly_count(year),
768768
}
769769

@@ -844,12 +844,12 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
844844
year = datetime.now(ZoneInfo("Europe/Prague")).strftime("%Y")
845845
categories = store.category_totals(year=year, level=0, hide_discontinued=self._rohlik_account.hide_discontinued)
846846
if not categories:
847-
return {"year": year, "enriched_orders": store.enriched_count, "total_orders": store.yearly_count(year)}
847+
return {"year": year, "enriched_orders": store.yearly_enriched_count(year), "total_orders": store.yearly_count(year)}
848848
return {
849849
"year": year,
850850
"total_count": len(categories),
851851
"categories": categories[:self._rohlik_account.top_n],
852-
"enriched_orders": store.enriched_count,
852+
"enriched_orders": store.yearly_enriched_count(year),
853853
"total_orders": store.yearly_count(year),
854854
}
855855

@@ -930,12 +930,12 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
930930
year = datetime.now(ZoneInfo("Europe/Prague")).strftime("%Y")
931931
categories = store.category_totals(year=year, level=2, hide_discontinued=self._rohlik_account.hide_discontinued)
932932
if not categories:
933-
return {"year": year, "enriched_orders": store.enriched_count, "total_orders": store.yearly_count(year)}
933+
return {"year": year, "enriched_orders": store.yearly_enriched_count(year), "total_orders": store.yearly_count(year)}
934934
return {
935935
"year": year,
936936
"total_count": len(categories),
937937
"categories": categories[:self._rohlik_account.top_n],
938-
"enriched_orders": store.enriched_count,
938+
"enriched_orders": store.yearly_enriched_count(year),
939939
"total_orders": store.yearly_count(year),
940940
}
941941

@@ -1016,12 +1016,12 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
10161016
year = datetime.now(ZoneInfo("Europe/Prague")).strftime("%Y")
10171017
categories = store.category_totals(year=year, level=3, hide_discontinued=self._rohlik_account.hide_discontinued)
10181018
if not categories:
1019-
return {"year": year, "enriched_orders": store.enriched_count, "total_orders": store.yearly_count(year)}
1019+
return {"year": year, "enriched_orders": store.yearly_enriched_count(year), "total_orders": store.yearly_count(year)}
10201020
return {
10211021
"year": year,
10221022
"total_count": len(categories),
10231023
"categories": categories[:self._rohlik_account.top_n],
1024-
"enriched_orders": store.enriched_count,
1024+
"enriched_orders": store.yearly_enriched_count(year),
10251025
"total_orders": store.yearly_count(year),
10261026
}
10271027

0 commit comments

Comments
 (0)