From caf27dcff91364506439753ca43a3b591272e347 Mon Sep 17 00:00:00 2001 From: Benjamin Segall Date: Tue, 31 Dec 2024 23:17:53 +0200 Subject: [PATCH 1/2] Make date parsing more resiliant --- .devcontainer.json | 2 +- custom_components/generac/sensor.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.devcontainer.json b/.devcontainer.json index 5f7a2f2..96a92bf 100644 --- a/.devcontainer.json +++ b/.devcontainer.json @@ -1,6 +1,6 @@ { "name": "hacs/default", - "image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.11", + "image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.12", "postCreateCommand": "scripts/setup", "customizations": { "vscode": { diff --git a/custom_components/generac/sensor.py b/custom_components/generac/sensor.py index 9251f68..05f585e 100644 --- a/custom_components/generac/sensor.py +++ b/custom_components/generac/sensor.py @@ -60,6 +60,16 @@ def sensors(item: Item) -> list[Type[GeneracEntity]]: return lst +def parseDatetime(rawStr: str) -> datetime: + formats = ["%Y-%m-%dT%H:%M:%S.%f%z", "%Y-%m-%dT%H:%M:%S%z"] + ves : list[ValueError] = [] + for fmt in formats: + try: + return datetime.strptime(rawStr, fmt) + except ValueError as ve: + ves.append(ve) + raise ValueError(f'No known datetime format for raw string {rawStr}') + class StatusSensor(GeneracEntity, SensorEntity): """generac Sensor class.""" @@ -194,9 +204,7 @@ def native_value(self): """Return the state of the sensor.""" if self.aparatus_detail.activationDate is None: return None - return datetime.strptime( - self.aparatus_detail.activationDate, "%Y-%m-%dT%H:%M:%S%z" - ) + return parseDatetime(self.aparatus_detail.activationDate) class LastSeenSensor(GeneracEntity, SensorEntity): @@ -214,9 +222,7 @@ def native_value(self): """Return the state of the sensor.""" if self.aparatus_detail.lastSeen is None: return None - return datetime.strptime( - self.aparatus_detail.lastSeen, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + return parseDatetime(self.aparatus_detail.lastSeen) class ConnectionTimeSensor(GeneracEntity, SensorEntity): @@ -234,9 +240,7 @@ def native_value(self): """Return the state of the sensor.""" if self.aparatus_detail.connectionTimestamp is None: return None - return datetime.strptime( - self.aparatus_detail.connectionTimestamp, "%Y-%m-%dT%H:%M:%S.%f%z" - ) + return parseDatetime(self.aparatus_detail.connectionTimestamp) class BatteryVoltageSensor(GeneracEntity, SensorEntity): From 8ff70bd304ffec56e46a540744baf3b0ed1d291c Mon Sep 17 00:00:00 2001 From: Benjamin Segall Date: Tue, 31 Dec 2024 23:23:29 +0200 Subject: [PATCH 2/2] fix formatting --- custom_components/generac/sensor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/custom_components/generac/sensor.py b/custom_components/generac/sensor.py index ad74bf2..d702a09 100644 --- a/custom_components/generac/sensor.py +++ b/custom_components/generac/sensor.py @@ -62,13 +62,14 @@ def sensors(item: Item) -> list[Type[GeneracEntity]]: def parseDatetime(rawStr: str) -> datetime: formats = ["%Y-%m-%dT%H:%M:%S.%f%z", "%Y-%m-%dT%H:%M:%S%z"] - ves : list[ValueError] = [] + ves: list[ValueError] = [] for fmt in formats: try: return datetime.strptime(rawStr, fmt) except ValueError as ve: ves.append(ve) - raise ValueError(f'No known datetime format for raw string {rawStr}') + raise ValueError(f"No known datetime format for raw string {rawStr}") + class StatusSensor(GeneracEntity, SensorEntity): """generac Sensor class."""