2727)
2828from homeassistant .helpers .entity_registry import RegistryEntry
2929from homeassistant .helpers .update_coordinator import DataUpdateCoordinator
30+ from homeassistant .util import dt as dt_util
3031from homeassistant .util .hass_dict import HassKey
3132
32- from .common import fix_datetime_string , utcnow_no_timezone , validate_is_float
33+ from .common import validate_is_float
3334from .const import (
3435 ATTR_BATTERY_LAST_REPLACED ,
3536 ATTR_BATTERY_LEVEL ,
@@ -116,9 +117,7 @@ class BatteryNotesSubentryCoordinator(DataUpdateCoordinator[None]):
116117 wrapped_battery : RegistryEntry | None = None
117118 wrapped_battery_low : RegistryEntry | None = None
118119 is_orphaned : bool = False
119- last_wrapped_battery_state_write : datetime = utcnow_no_timezone () - timedelta (
120- hours = 2
121- )
120+ last_wrapped_battery_state_write : datetime = dt_util .utcnow () - timedelta (hours = 2 )
122121 _current_battery_level : str | None = None
123122 _previous_battery_low : bool | None = None
124123 _previous_battery_level : str | None = None
@@ -195,13 +194,11 @@ def __init__( # noqa: PLR0912
195194 device_entry = device_registry .async_get (self .device_id )
196195
197196 if device_entry and device_entry .created_at .year > 1970 :
198- last_replaced = device_entry .created_at .strftime (
199- "%Y-%m-%dT%H:%M:%S.%f"
200- )
197+ last_replaced = device_entry .created_at
201198 elif self .source_entity_id :
202199 entity = entity_registry .async_get (self .source_entity_id )
203200 if entity and entity .created_at .year > 1970 :
204- last_replaced = entity .created_at . strftime ( "%Y-%m-%dT%H:%M:%S.%f" )
201+ last_replaced = entity .created_at
205202
206203 _LOGGER .debug (
207204 "Defaulting %s battery last replaced to %s" ,
@@ -210,11 +207,11 @@ def __init__( # noqa: PLR0912
210207 )
211208
212209 if last_replaced :
213- self .last_replaced = datetime . fromisoformat ( last_replaced )
210+ self .last_replaced = last_replaced
214211
215212 # If there is not a last_reported set to now
216213 if not self .last_reported :
217- last_reported = utcnow_no_timezone ()
214+ last_reported = dt_util . utcnow ()
218215 _LOGGER .debug (
219216 "Defaulting %s battery last reported to %s" ,
220217 self .source_entity_id or self .device_id ,
@@ -617,7 +614,7 @@ def current_battery_level(self, value):
617614 _LOGGER .debug ("battery_increased event fired" )
618615
619616 if self ._current_battery_level not in [STATE_UNAVAILABLE , STATE_UNKNOWN ]:
620- self .last_reported = utcnow_no_timezone ()
617+ self .last_reported = dt_util . utcnow ()
621618 self .last_reported_level = cast (float , self ._current_battery_level )
622619 self ._previous_battery_low = self .battery_low
623620 self ._previous_battery_level = self ._current_battery_level
@@ -647,15 +644,7 @@ def last_replaced(self) -> datetime | None:
647644 )
648645
649646 if entry and LAST_REPLACED in entry and entry [LAST_REPLACED ] is not None :
650- entry_last_replaced = str (entry [LAST_REPLACED ])
651- if not entry_last_replaced .endswith ("+00:00" ):
652- entry_last_replaced += "+00:00"
653-
654- try :
655- return datetime .fromisoformat (entry_last_replaced )
656- except ValueError :
657- entry_last_replaced = fix_datetime_string (entry_last_replaced )
658- return datetime .fromisoformat (entry_last_replaced )
647+ return datetime .fromisoformat (str (entry [LAST_REPLACED ]))
659648 return None
660649
661650 @last_replaced .setter
@@ -688,20 +677,12 @@ def last_reported(self) -> datetime | None:
688677 )
689678
690679 if entry and LAST_REPORTED in entry and entry [LAST_REPORTED ] is not None :
691- entry_last_reported = str (entry [LAST_REPORTED ])
692- if not entry_last_reported .endswith ("+00:00" ):
693- entry_last_reported += "+00:00"
694-
695- try :
696- return datetime .fromisoformat (entry_last_reported )
697- except ValueError :
698- entry_last_reported = fix_datetime_string (entry_last_reported )
699- return datetime .fromisoformat (entry_last_reported )
680+ return datetime .fromisoformat (str (entry [LAST_REPORTED ]))
700681
701682 return None
702683
703684 @last_reported .setter
704- def last_reported (self , value ):
685+ def last_reported (self , value : datetime ):
705686 """Set the last reported datetime and store it."""
706687
707688 if not hasattr (self .config_entry , "runtime_data" ):
0 commit comments