Skip to content

Commit

Permalink
Merge pull request #8 from tache/datetime-strings
Browse files Browse the repository at this point in the history
BUG: Improve string to datetime compatability
  • Loading branch information
carterbox authored Jun 25, 2024
2 parents 14a15ef + 3224960 commit 11ede15
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions custom_components/kidde/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class KiddeSensorTimestampEntity(KiddeEntity, SensorEntity):
"""A KiddeSensoryEntity which returns a datetime.
Assume sensor returns datetime string e.g. '2024-06-14T03:40:39.667544824Z'
which needs to be converted to a python datetime.
or '2024-06-22T16:00:19Z' which needs to be converted to a python datetime.
"""

@property
Expand All @@ -160,7 +160,11 @@ def native_value(self) -> datetime.datetime | None:
logger.debug(f"{self.entity_description.key} of type {dtype} is {value}")
if value is None:
return value
return datetime.datetime.strptime(value[:-4], "%Y-%m-%dT%H:%M:%S.%f").replace(
# Last seen and last test return different precision for time, so we
# need to strip anything beyond microseconds
# https://github.com/tache/homeassistant-kidde/issues/7
stripped = value.strip('Z').split('.')[0]
return datetime.datetime.strptime(stripped, "%Y-%m-%dT%H:%M:%S").replace(
tzinfo=datetime.timezone.utc
)

Expand Down

0 comments on commit 11ede15

Please sign in to comment.