Skip to content

Commit 5a9a7b0

Browse files
fix: new update email type and re-adding some debug options. (#22)
* fix: included new email type - update. * re-added some debugging.
1 parent 82298ba commit 5a9a7b0

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

custom_components/ocado/const.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
OCADO_NEW_NEW_TOTAL_SUBJECT = "Your receipt and updates for today’s delivery"
1616
OCADO_SMARTPASS_SUBJECT = "Payment successful: Smart Pass membership"
1717
OCADO_RENEWAL_SUBJECT = "Your Smart Pass will be renewed"
18+
OCADO_UPDATE_SUBJECT = "Confirmation of your order changes"
1819
OCADO_SUBJECT_DICT = {
1920
OCADO_CANCELLATION_SUBJECT: "cancellation",
2021
OCADO_CONFIRMATION_SUBJECT: "confirmation",
22+
OCADO_UPDATE_SUBJECT: "update",
2123
OCADO_NEW_TOTAL_SUBJECT: "new_total",
2224
OCADO_RECEIPT_SUBJECT: "receipt",
2325
OCADO_NEW_NEW_TOTAL_SUBJECT:"new_total"
@@ -55,8 +57,8 @@
5557
REGEX_YEAR = r"(?:19|20)\d{2}"
5658
# If this eventually fails due to other formats being used, python-dateutil should be used
5759
REGEX_DATE_FULL = r"((?:" + REGEX_DATE + r")\/(?:" + REGEX_MONTH + r")\/(?:" + REGEX_YEAR + r"))"
58-
REGEX_TIME = r"([01][0-9]|2[0-3]):([0-5][0-9])([AaPp][Mm])?"
59-
REGEX_NOT_ISO_TIME = r"([0-9]|2[0-3]):([0-5][0-9])([AaPp][Mm])?"
60+
REGEX_TIME = r"([01][0-9]|2[0-3]):([0-5][0-9])\s?([AaPp][Mm])?"
61+
REGEX_NOT_ISO_TIME = r"([0-9]|2[0-3])(?::|.)([0-5][0-9])\s?([AaPp][Mm])?"
6062
REGEX_ORDINALS = r"st|nd|rd|th"
6163

6264
REGEX_AMOUNT = r"(?:\d+x)?\d+k?(?:g|l|ml)"

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.2"
19+
"version": "1.1.3"
2020
}

custom_components/ocado/utils.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,19 @@ def get_edit_datetime(message: str) -> datetime:
122122
"""Parse the edit deadline datetime."""
123123
pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{REGEX_TIME})(?:\son\s)(?P<day>{REGEX_DATE})(?:{REGEX_ORDINALS})\s(?P<month>{REGEX_MONTH_FULL})\s(?P<year>{REGEX_YEAR})"
124124
raw = re.search(pattern, message)
125+
_LOGGER.debug("Trying to get edit datetime")
125126
if raw:
127+
_LOGGER.debug("First attempt found datetime")
126128
edit_datetime_raw = raw.group('year') + '-' + raw.group('month') + '-' + raw.group('day') + ' ' + raw.group('time')
127129
return datetime.strptime(edit_datetime_raw,'%Y-%B-%d %H:%M')
128130
else:
129-
pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{REGEX_NOT_ISO_TIME})(?:,\s)(?P<day>{REGEX_DATE})\s(?P<month>{REGEX_MONTH_FULL})\s(?P<year>{REGEX_YEAR})"
130-
raw = re.search(pattern, message, re.MULTILINE)
131+
_LOGGER.debug("Trying backup pattern")
132+
pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{REGEX_NOT_ISO_TIME})(?:\son\s|,\s)?(?P<day>{REGEX_DATE})(?:{REGEX_ORDINALS})?\s?(?P<month>{REGEX_MONTH_FULL})\s(?P<year>{REGEX_YEAR})"
133+
raw = re.search(pattern, message, re.MULTILINE)
131134
if raw:
132-
edit_datetime_raw = raw.group('year') + '-' + raw.group('month') + '-' + raw.group('day') + ' 0' + raw.group('time')
135+
_LOGGER.debug("Second attempt found datetime")
136+
edit_datetime_raw = raw.group('year') + '-' + raw.group('month') + '-' + raw.group('day') + ' 0' + raw.group('time').replace(" ","").replace(".",":")
133137
edit_datetime_raw = re.sub(r"pm",r"PM",re.sub(r"am",r"AM",edit_datetime_raw))
134-
_LOGGER.debug(edit_datetime_raw)
135138
return datetime.strptime(edit_datetime_raw,'%Y-%B-%d %I:%M%p')
136139
_LOGGER.error("No edit date found in message.")
137140
raise ValueError("No edit date found in message.")
@@ -177,14 +180,20 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
177180
server.close()
178181
server.logout()
179182
return message_ids, None
183+
total = len(message_ids[0].split())
184+
_LOGGER.debug("Beginning triaging of %s emails retrieved.", str(total))
185+
i=0
180186
for message_id in reversed(message_ids[0].split()):
187+
i+=1
188+
_LOGGER.debug("Starting on message %s/%s", str(i), str(total))
181189
result, message_data = server.fetch(message_id,"(RFC822)")
182190
if message_data is None:
183191
continue
184192
message_data = message_data[0][1] # type: ignore
185193
ocado_email = _parse_email(message_id, message_data) # type: ignore
186194
# If the type of email is a cancellation, add the order number to check for later
187195
if ocado_email.type == "cancellation":
196+
_LOGGER.debug("Cancellation email found and added to cancelled orders.")
188197
ocado_cancelled.append(ocado_email.order_number)
189198
# If the order number isn't in the list of cancelled order numbers
190199
if ocado_email.order_number not in ocado_cancelled:
@@ -193,6 +202,7 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
193202
# We only care about the most recent receipt
194203
# This is currently broken due to Ocado changes with the PDF no longer included in emails
195204
if ocado_receipt is None:
205+
_LOGGER.debug("Ocado order (%s) added to receipts.", ocado_email.order_number)
196206
ocado_receipt = OcadoReceipt(ocado_email.date, ocado_email.order_number)
197207
# email_message = BytesParser(policy=policy.default).parsebytes(message_data) # type: ignore
198208
# for part in email_message.iter_attachments():
@@ -246,20 +256,22 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
246256
# day_list = getattr(fridge, day) + getattr(cupboard, day)
247257
# setattr(ocado_receipt, day, day_list)
248258
# setattr(ocado_receipt, "date_dict", fridge.date_dict)
249-
elif ocado_email.type == "confirmation":
259+
elif ocado_email.type == "confirmation" or ocado_email.type == "update":
250260
# Make sure we're not adding an older version of an order we already have
261+
_LOGGER.debug("Confirmed order is not in the list of confirmed orders? %s", ocado_email.order_number not in ocado_confirmed_orders)
251262
if ocado_email.order_number not in ocado_confirmed_orders:
252263
ocado_confirmed_orders.append(ocado_email.order_number)
253264
ocado_confirmations.append(ocado_email)
265+
_LOGGER.debug("Ocado order (%s) added to confirmations.", ocado_email.order_number)
254266
elif ocado_email.type == "new_total":
255267
# We only care about the most recent new total
256268
if ocado_total is None:
257269
ocado_confirmed_orders.append(ocado_email.order_number)
258270
ocado_total = ocado_email
259-
260-
271+
_LOGGER.debug("Ocado order (%s) added to totals.", ocado_email.order_number)
261272
server.close()
262273
server.logout()
274+
_LOGGER.debug("Finished with IMAP and closed the connection")
263275
# It's possible the total order number is repeated, so remove it
264276
ocado_orders = list(set(ocado_confirmed_orders))
265277
triaged_emails = OcadoEmails(

0 commit comments

Comments
 (0)