Skip to content

Commit

Permalink
fix sensor states typing
Browse files Browse the repository at this point in the history
  • Loading branch information
benleb committed Sep 4, 2021
1 parent 3432023 commit 470053c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def __init__(self, coordinator, _id: int, spc: SurePetcareAPI) -> None:
self._attr_state = LockState(locking["mode"]).name.casefold()

@property
def state(self) -> str:
def state(self) -> str | None:
"""Return battery level in percent."""
if (
state := cast(SureFlap, self.coordinator.data[self._id])
Expand All @@ -182,8 +182,6 @@ def state(self) -> str:
):
return LockState(state["locking"]["mode"]).name.casefold()

return "Unknown"


class Felaqua(SurePetcareSensor):
"""Sure Petcare Felaqua."""
Expand All @@ -196,10 +194,10 @@ def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
self._attr_unit_of_measurement = VOLUME_MILLILITERS

@property
def state(self) -> int | None:
def state(self) -> float | None:
"""Return the remaining water."""
if felaqua := cast(SureFelaqua, self.coordinator.data[self._id]):
return max(0, int(felaqua.water_remaining or 0))
return int(felaqua.water_remaining) if felaqua.water_remaining else None

@property
def extra_state_attributes(self) -> dict[str, Any]:
Expand Down Expand Up @@ -284,10 +282,13 @@ def __init__(
self._attr_unit_of_measurement = MASS_GRAMS

@property
def state(self) -> int | None:
def state(self) -> float | None:
"""Return the remaining water."""
if feeder := cast(SureFeeder, self.coordinator.data[self.feeder_id]):
return max(0, int(feeder.bowls[self.bowl_id].weight))

if (feeder := cast(SureFeeder, self.coordinator.data[self.feeder_id])) and (
weight := feeder.bowls[self.bowl_id].weight
):
return int(weight) if weight and weight > 0 else None


class Feeder(SurePetcareSensor):
Expand All @@ -301,10 +302,10 @@ def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
self._attr_unit_of_measurement = MASS_GRAMS

@property
def state(self) -> int | None:
def state(self) -> float | None:
"""Return the total remaining food."""
if feeder := cast(SureFeeder, self.coordinator.data[self._id]):
return int(feeder.total_weight)
return int(feeder.total_weight) if feeder.total_weight else None

@property
def device_info(self):
Expand Down Expand Up @@ -349,10 +350,8 @@ def __init__(self, coordinator, _id: int, spc: SurePetcareAPI):
@property
def state(self) -> int | None:
"""Return battery level in percent."""
if (
battery := cast(SurepyDevice, self.coordinator.data[self._id])
) and battery.battery_level:
return max(0, battery.battery_level)
if battery := cast(SurepyDevice, self.coordinator.data[self._id]):
return int(battery.battery_level) if battery.battery_level else None

@property
def extra_state_attributes(self) -> dict[str, Any]:
Expand Down

0 comments on commit 470053c

Please sign in to comment.