|
16 | 16 | from homeassistant.helpers.entity_platform import AddEntitiesCallback |
17 | 17 | from .const import DOMAIN, ICON_UPDATE, ICON_CREDIT, ICON_NO_LIMIT, ICON_FREE_EXPRESS, ICON_DELIVERY, ICON_BAGS, \ |
18 | 18 | ICON_CART, ICON_ACCOUNT, ICON_EMAIL, ICON_PHONE, ICON_PREMIUM_DAYS, ICON_LAST_ORDER, ICON_NEXT_ORDER_SINCE, \ |
19 | | - ICON_NEXT_ORDER_TILL, ICON_INFO, ICON_DELIVERY_TIME |
| 19 | + ICON_NEXT_ORDER_TILL, ICON_INFO, ICON_DELIVERY_TIME, ICON_MONTHLY_SPENT |
20 | 20 | from .entity import BaseEntity |
21 | 21 | from .hub import RohlikAccount |
22 | | -from .utils import extract_delivery_datetime |
| 22 | +from .utils import extract_delivery_datetime, calculate_current_month_orders_total |
23 | 23 |
|
24 | 24 | SCAN_INTERVAL = timedelta(seconds=600) |
25 | 25 |
|
@@ -48,7 +48,8 @@ async def async_setup_entry( |
48 | 48 | NextOrderTill(rohlik_hub), |
49 | 49 | NextOrderSince(rohlik_hub), |
50 | 50 | DeliveryInfo(rohlik_hub), |
51 | | - DeliveryTime(rohlik_hub) |
| 51 | + DeliveryTime(rohlik_hub), |
| 52 | + MonthlySpent(rohlik_hub) |
52 | 53 | ] |
53 | 54 |
|
54 | 55 | if rohlik_hub.has_address: |
@@ -423,6 +424,29 @@ async def async_will_remove_from_hass(self) -> None: |
423 | 424 | self._rohlik_account.remove_callback(self.async_write_ha_state) |
424 | 425 |
|
425 | 426 |
|
| 427 | +class MonthlySpent(BaseEntity, SensorEntity): |
| 428 | + """Sensor for amount spent in current month.""" |
| 429 | + |
| 430 | + _attr_translation_key = "monthly_spent" |
| 431 | + _attr_should_poll = False |
| 432 | + |
| 433 | + @property |
| 434 | + def native_value(self) -> float | None: |
| 435 | + """Returns amount spend within last month.""" |
| 436 | + _LOGGER.error(self._rohlik_account.data.get('delivered_orders', "[]")) |
| 437 | + return calculate_current_month_orders_total(self._rohlik_account.data.get('delivered_orders', [])) |
| 438 | + |
| 439 | + @property |
| 440 | + def icon(self) -> str: |
| 441 | + return ICON_MONTHLY_SPENT |
| 442 | + |
| 443 | + async def async_added_to_hass(self) -> None: |
| 444 | + self._rohlik_account.register_callback(self.async_write_ha_state) |
| 445 | + |
| 446 | + async def async_will_remove_from_hass(self) -> None: |
| 447 | + self._rohlik_account.remove_callback(self.async_write_ha_state) |
| 448 | + |
| 449 | + |
426 | 450 | class NoLimitOrders(BaseEntity, SensorEntity): |
427 | 451 | """Sensor for remaining no limit orders.""" |
428 | 452 |
|
|
0 commit comments