Skip to content

Commit

Permalink
Add next order product count sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
NLthijs48 committed May 9, 2024
1 parent 3f7bde1 commit c6efd05
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
8 changes: 8 additions & 0 deletions custom_components/crisp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions custom_components/crisp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 7 additions & 1 deletion custom_components/crisp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion custom_components/crisp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit c6efd05

Please sign in to comment.