|
29 | 29 | from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
30 | 30 | from homeassistant.util.hass_dict import HassKey |
31 | 31 |
|
32 | | -from .common import utcnow_no_timezone, validate_is_float |
| 32 | +from .common import fix_datetime_string, utcnow_no_timezone, validate_is_float |
33 | 33 | from .const import ( |
34 | 34 | ATTR_BATTERY_LAST_REPLACED, |
35 | 35 | ATTR_BATTERY_LEVEL, |
@@ -636,11 +636,15 @@ def last_replaced(self) -> datetime | None: |
636 | 636 | self.device_id |
637 | 637 | ) |
638 | 638 |
|
639 | | - if entry: |
640 | | - if LAST_REPLACED in entry and entry[LAST_REPLACED] is not None: |
641 | | - entry_last_replaced = str(entry[LAST_REPLACED]) |
642 | | - if not entry_last_replaced.endswith("+00:00"): |
643 | | - entry_last_replaced += "+00:00" |
| 639 | + if entry and LAST_REPLACED in entry and entry[LAST_REPLACED] is not None: |
| 640 | + entry_last_replaced = str(entry[LAST_REPLACED]) |
| 641 | + if not entry_last_replaced.endswith("+00:00"): |
| 642 | + entry_last_replaced += "+00:00" |
| 643 | + |
| 644 | + try: |
| 645 | + return datetime.fromisoformat(entry_last_replaced) |
| 646 | + except ValueError: |
| 647 | + entry_last_replaced = fix_datetime_string(entry_last_replaced) |
644 | 648 | return datetime.fromisoformat(entry_last_replaced) |
645 | 649 | return None |
646 | 650 |
|
@@ -677,7 +681,12 @@ def last_reported(self) -> datetime | None: |
677 | 681 | entry_last_reported = str(entry[LAST_REPORTED]) |
678 | 682 | if not entry_last_reported.endswith("+00:00"): |
679 | 683 | entry_last_reported += "+00:00" |
680 | | - return datetime.fromisoformat(entry_last_reported) |
| 684 | + |
| 685 | + try: |
| 686 | + return datetime.fromisoformat(entry_last_reported) |
| 687 | + except ValueError: |
| 688 | + entry_last_reported = fix_datetime_string(entry_last_reported) |
| 689 | + return datetime.fromisoformat(entry_last_reported) |
681 | 690 |
|
682 | 691 | return None |
683 | 692 |
|
|
0 commit comments