From c6efd05712cca2255f4f123f2e87753b3f2e9ff7 Mon Sep 17 00:00:00 2001 From: Thijs Wiefferink Date: Thu, 9 May 2024 21:36:19 +0000 Subject: [PATCH] Add next order product count sensor --- README.md | 8 +++++++- custom_components/crisp/api.py | 8 ++++++++ custom_components/crisp/const.py | 1 + custom_components/crisp/coordinator.py | 8 +++++++- custom_components/crisp/sensor.py | 8 +++++++- 5 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8038276..f0539b9 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,19 @@ An instance of this integration provides information about a single Crisp accoun The integration can be set up multiple times if you want to track multiple accounts. An account is represented as a device. -## Sensors per account +## Account sensors Name | Description -- | -- `Order count total` | Total number of orders in your Crisp account (includes completed, cancelled and pending orders). `Order count open` | Count of open orders in your Crisp account (out for delivery, or planned in the future). +## Next order sensors + +Name | Description +-- | -- +`Next order product count` | Number of products in the next order up for delivery (if there is any) + ## Installation 1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`). diff --git a/custom_components/crisp/api.py b/custom_components/crisp/api.py index 681753c..7416e1f 100644 --- a/custom_components/crisp/api.py +++ b/custom_components/crisp/api.py @@ -86,8 +86,16 @@ async def login(self, email: str, country: str, login_code: str) -> any: async def get_order_count(self) -> any: """Get the total number of orders of this user.""" + # Data: + # - count: int (all orders of the user, including cancelled, open, delivered) + # - openOrderIds: int[] (list of order ids that are open, paid but not delivered yet) return await self._api_wrapper(method="get", path="/order/count") + async def get_order_details(self, order_id: int) -> any: + """Get the detail of a particular order.""" + + return await self._api_wrapper(method="get", path=f"/order/{order_id}") + async def _api_wrapper( self, method: str, diff --git a/custom_components/crisp/const.py b/custom_components/crisp/const.py index 55bcb16..9a7f2a4 100644 --- a/custom_components/crisp/const.py +++ b/custom_components/crisp/const.py @@ -12,3 +12,4 @@ # Sensor entity id keys SENSOR_ORDER_COUNT_TOTAL = "order_count_total" SENSOR_ORDER_COUNT_OPEN = "order_count_open" +SENSOR_NEXT_ORDER_PRODUCT_COUNT = "next_order_delivery_on" diff --git a/custom_components/crisp/coordinator.py b/custom_components/crisp/coordinator.py index 40e60ec..df09e72 100644 --- a/custom_components/crisp/coordinator.py +++ b/custom_components/crisp/coordinator.py @@ -25,6 +25,7 @@ class CrispData(TypedDict): order_count_total: int order_count_open: int + next_order_product_count: None | int # https://developers.home-assistant.io/docs/integration_fetching_data#coordinated-single-api-poll-for-data-for-all-entities class CrispDataUpdateCoordinator(DataUpdateCoordinator[CrispData]): @@ -55,13 +56,18 @@ async def _async_update_data(self): order_count_total = order_count_data['count'] order_count_open = len(open_order_ids) + next_order_product_count = None if (len(open_order_ids) >= 1): next_order_id = open_order_ids[0] - LOGGER.debug("next order id: %s", next_order_id) + # LOGGER.debug("next order id: %s", next_order_id) + next_open_order = await self.client.get_order_details(next_order_id) + # LOGGER.debug(json.dumps(next_open_order.keys(), indent=4)) + next_order_product_count = len(next_open_order.get('data', {}).get('products')) result: CrispData = { 'order_count_total': order_count_total, 'order_count_open': order_count_open, + 'next_order_product_count': next_order_product_count } return result except CrispApiClientAuthenticationError as exception: diff --git a/custom_components/crisp/sensor.py b/custom_components/crisp/sensor.py index c9eba78..b566ed9 100644 --- a/custom_components/crisp/sensor.py +++ b/custom_components/crisp/sensor.py @@ -6,7 +6,7 @@ from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.helpers.typing import StateType -from .const import DOMAIN, SENSOR_ORDER_COUNT_OPEN, SENSOR_ORDER_COUNT_TOTAL +from .const import DOMAIN, SENSOR_NEXT_ORDER_PRODUCT_COUNT, SENSOR_ORDER_COUNT_OPEN, SENSOR_ORDER_COUNT_TOTAL from .coordinator import CrispData, CrispDataUpdateCoordinator from .entity import CrispEntity from dataclasses import dataclass @@ -31,6 +31,12 @@ class CrispSensorEntityDescription(SensorEntityDescription): icon="mdi:receipt-clock-outline", get_value=lambda data: data['order_count_open'], ), + CrispSensorEntityDescription( + key=SENSOR_NEXT_ORDER_PRODUCT_COUNT, + name="Next order product count", + icon="mdi:food-croissant", + get_value=lambda data: data['next_order_product_count'], + ), ) async def async_setup_entry(hass, entry, async_add_devices):