Skip to content

Commit

Permalink
Handle error on setup when charge dates a None or datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
nickknissen committed Jan 15, 2024
1 parent 2a9d34a commit 6427028
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions custom_components/monta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any

from dateutil import parser
from datetime import datetime
from homeassistant.components.sensor import (
ENTITY_ID_FORMAT,
SensorDeviceClass,
Expand All @@ -23,6 +24,7 @@
from .entity import MontaEntity
from .utils import snake_case


@dataclass
class MontaSensorEntityDescriptionMixin:
"""Mixin for required keys."""
Expand Down Expand Up @@ -67,8 +69,13 @@ def last_charge_extra_attributes(data: dict[str, Any]) -> dict[str, Any]:
return None


def _parse_date(chargedate) -> str:
return parser.parse(chargedate) if chargedate else None
def _parse_date(chargedate: str):
if isinstance(chargedate, str):
return parser.parse(chargedate)
elif isinstance(chargedate, datetime):
return chargedate
else:
return None


ENTITY_DESCRIPTIONS: tuple[MontaSensorEntityDescription, ...] = (
Expand Down

0 comments on commit 6427028

Please sign in to comment.