Skip to content

Commit 00fc47d

Browse files
fix: if next/upcoming are none (#27)
* fix: updated time parsing * fix: improper handling of non-iso time - added zfill * chore: update version * fix: Split out the handling of no future order with past emails within the IMAP search range so it no longer results in comparing a None to a datetime. * fix: updated sensors to deal properly with no next/upcoming orders. * chore: update version
1 parent ae58d17 commit 00fc47d

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

custom_components/ocado/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
"BeautifulSoup4>=4.10.0"
1717
],
1818
"single_config_entry": true,
19-
"version": "1.1.6"
19+
"version": "1.1.7"
2020
}

custom_components/ocado/sensor.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,21 @@ def _handle_coordinator_update(self) -> None:
153153
return
154154

155155
now = datetime.now()
156-
order = ocado_data.get("next") or ocado_data.get("upcoming")
157-
156+
# order = ocado_data.get("next") or ocado_data.get("upcoming")
157+
# Switch between orders depending on delivery datetime or output None
158+
order = ocado_data.get("next")
159+
if (order is None):
160+
order = ocado_data.get("upcoming")
161+
if order is not None:
162+
if order.delivery_window_end < now:
163+
order = None
164+
if order is not None:
165+
# If the delivery datetime is in the past check upcoming
166+
if (order.delivery_window_end < now):
167+
order = ocado_data.get("upcoming")
168+
if order is not None:
169+
if (order.delivery_window_end < now):
170+
order = None
158171
if order is not None:
159172
result = set_order(self, order, now) # type: ignore
160173
_LOGGER.debug("Set_order returned %s", result)
@@ -260,16 +273,37 @@ def _handle_coordinator_update(self) -> None:
260273
if order is not None:
261274
if order.edit_datetime < now:
262275
order = None
276+
else:
277+
order = None
263278
if order is not None:
279+
# If the edit datetime is in the past check upcoming
264280
if (order.edit_datetime < now):
265281
order = ocado_data.get("upcoming")
266282
if order is not None:
283+
# If the edit datetime is in the past return empty
267284
if order.edit_datetime < now:
268-
order = None
285+
self._attr_state = None
286+
self._attr_icon = "mdi:help-circle"
287+
self._hass_custom_attributes = {
288+
"updated": datetime.now(),
289+
"order_number": None,
290+
}
291+
else:
292+
result = set_edit_order(self, order, now) # type: ignore
293+
_LOGGER.debug("Set_order returned %s", result)
294+
else:
295+
self._attr_state = None
296+
self._attr_icon = "mdi:help-circle"
297+
self._hass_custom_attributes = {
298+
"updated": datetime.now(),
299+
"order_number": None,
300+
}
269301
else:
302+
# Set the edit order with the selected order
270303
result = set_edit_order(self, order, now) # type: ignore
271304
_LOGGER.debug("Set_order returned %s", result)
272305
else:
306+
# If no orders are returned, return an empty order
273307
self._attr_state = None
274308
self._attr_icon = "mdi:help-circle"
275309
self._hass_custom_attributes = {
@@ -289,7 +323,7 @@ def _handle_coordinator_update(self) -> None:
289323
if detect_attr_changes(new, old):
290324
_LOGGER.debug("Updating due to new attributes")
291325
self.async_write_ha_state()
292-
# Now check if the edit deadline has passed
326+
# Now check if the edit deadline has passed -> what if there is no next? Display
293327
elif "next" in current.attributes:
294328
if hasattr(current.attributes.get("next"),"edit_deadline"):
295329
if current.attributes.get("next").edit_deadline < now: # type: ignore

0 commit comments

Comments
 (0)