Skip to content

Commit

Permalink
Add 'next order delivery on' sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
NLthijs48 committed May 15, 2024
1 parent c6efd05 commit 341170e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion custom_components/crisp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# 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"
SENSOR_NEXT_ORDER_PRODUCT_COUNT = "next_order_product_count"
SENSOR_NEXT_ORDER_DELIVERY_ON = "next_order_delivery_on"
15 changes: 12 additions & 3 deletions custom_components/crisp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from datetime import timedelta
from datetime import timedelta, date
from typing import TypedDict

from homeassistant.config_entries import ConfigEntry
Expand All @@ -25,7 +25,9 @@ class CrispData(TypedDict):

order_count_total: int
order_count_open: int
# TODO: 'next_order' dict property, nest other fields inside that?
next_order_product_count: None | int
next_order_delivery_on: None | date

# 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 @@ -57,17 +59,24 @@ async def _async_update_data(self):
order_count_open = len(open_order_ids)

next_order_product_count = None
next_order_delivery_on = None
if (len(open_order_ids) >= 1):
next_order_id = open_order_ids[0]
# 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'))
next_open_order_data = next_open_order.get('data', {})

next_order_product_count = len(next_open_order_data.get('products'))

next_open_order_delivery_slot = next_open_order_data.get('deliverySlot', {})
next_order_delivery_on = date.fromisoformat(next_open_order_delivery_slot.get('date'))

result: CrispData = {
'order_count_total': order_count_total,
'order_count_open': order_count_open,
'next_order_product_count': next_order_product_count
'next_order_product_count': next_order_product_count,
'next_order_delivery_on': next_order_delivery_on,
}
return result
except CrispApiClientAuthenticationError as exception:
Expand Down
14 changes: 11 additions & 3 deletions custom_components/crisp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from __future__ import annotations
from collections.abc import Callable

from datetime import date

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.helpers.typing import StateType

from .const import DOMAIN, SENSOR_NEXT_ORDER_PRODUCT_COUNT, 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, SENSOR_NEXT_ORDER_DELIVERY_ON
from .coordinator import CrispData, CrispDataUpdateCoordinator
from .entity import CrispEntity
from dataclasses import dataclass
Expand All @@ -15,7 +17,7 @@
class CrispSensorEntityDescription(SensorEntityDescription):
"""Describes Crisp sensor entity."""

get_value: Callable[[CrispData], StateType]
get_value: Callable[[CrispData], StateType | date]


SENSOR_TYPES: tuple[CrispSensorEntityDescription, ...] = (
Expand All @@ -37,6 +39,12 @@ class CrispSensorEntityDescription(SensorEntityDescription):
icon="mdi:food-croissant",
get_value=lambda data: data['next_order_product_count'],
),
CrispSensorEntityDescription(
key=SENSOR_NEXT_ORDER_DELIVERY_ON,
name="Next order delivery on",
icon="mdi:calendar",
get_value=lambda data: data['next_order_delivery_on'],
),
)

async def async_setup_entry(hass, entry, async_add_devices):
Expand Down Expand Up @@ -64,6 +72,6 @@ def __init__(
self.entity_description = entity_description

@property
def native_value(self) -> StateType:
def native_value(self):
"""Return the value reported by the sensor."""
return self.entity_description.get_value(self.coordinator.data)

0 comments on commit 341170e

Please sign in to comment.