Skip to content

Commit

Permalink
Catch KeyError when value does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
dupondje committed Aug 23, 2024
1 parent 0c693fd commit 481593b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion custom_components/nrgkick/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ class NRGKickNumber(NRGKickEntity, NumberEntity):
@property
def native_value(self) -> float | None:
"""Return the entity value to represent the entity state."""
return self.entity_description.value_fn(self.coordinator.data)
try:
return self.entity_description.value_fn(self.coordinator.data)
except KeyError:
return None

@property
def native_min_value(self) -> float:
Expand Down
5 changes: 4 additions & 1 deletion custom_components/nrgkick/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,7 @@ class NRGKickSensor(NRGKickEntity, SensorEntity):
@property
def native_value(self) -> StateType:
"""Return the value reported by the sensor."""
return self.entity_description.value_fn(self.coordinator.data)
try:
return self.entity_description.value_fn(self.coordinator.data)
except KeyError:
return None
5 changes: 4 additions & 1 deletion custom_components/nrgkick/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class NRGKickSwitch(NRGKickEntity, SwitchEntity):
@property
def is_on(self) -> bool:
"""Return the charging state."""
return self.entity_description.value_fn(self.coordinator.data)
try:
return self.entity_description.value_fn(self.coordinator.data)
except KeyError:
return False

async def async_turn_on(self):
"""Turn on charging."""
Expand Down

0 comments on commit 481593b

Please sign in to comment.