Skip to content

Commit

Permalink
Handle missing options
Browse files Browse the repository at this point in the history
  • Loading branch information
natekspencer committed Aug 2, 2024
1 parent d4438e7 commit ee2db1c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions custom_components/planta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dataclasses import dataclass
from datetime import datetime
import logging

from homeassistant.components.sensor import (
SensorDeviceClass,
Expand All @@ -19,6 +20,8 @@
from .coordinator import PlantaCoordinator
from .entity import PlantaEntity

_LOGGER = logging.getLogger(__name__)

PLANT_HEALTH_LIST = ["notset", "poor", "fair", "good", "verygood", "excellent"]


Expand Down Expand Up @@ -143,9 +146,12 @@ def native_value(self) -> int | str | None:
value = self.plant[self.entity_description.field]
if self.device_class == SensorDeviceClass.TIMESTAMP:
return datetime.fromisoformat(value)
if isinstance(value, str):
value = value.lower()
if self.device_class == SensorDeviceClass.ENUM and value not in self.options:
return None
return value.lower() if isinstance(value, str) else value
_LOGGER.warning("%s has an unknown value: %s", self.name, value)
self.entity_description.options.append(value)
return value


class PlantaHistorySensorEntity(PlantaEntity, SensorEntity):
Expand Down

0 comments on commit ee2db1c

Please sign in to comment.