Skip to content

Commit

Permalink
Merge pull request #90 from RobertD502/prevent_weight_use_reset
Browse files Browse the repository at this point in the history
Prevent weight use reset
  • Loading branch information
RobertD502 authored Nov 6, 2024
2 parents a217d05 + 28f5881 commit 41bf110
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,8 @@ ___
| Entity | Entity Type | Additional Comments |
|------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `Set weight` | `Number` | Set the weight of the cat. |
| `Last use duration` | `Sensor` | - Amount of time spent in the litter box during last use today. <br/>- Only available if PetKit account has litter box(es). <br/>- If multiple litter boxes, this will display data obtained from the most recent litter box used. |
| `Latest weight` | `Sensor` | - Most recent weight measurement obtained during last litter box use today. <br/>- Only available if PetKit account has litter box(es). <br/>- If multiple litter boxes, this will display data obtained from the most recent litter box used. <br/>- By default the unit used is Kg. Unit can be changed in the settings of the entity. |
| `Last use duration` | `Sensor` | - Amount of time spent in the litter box during last use. <br/>- Only available if PetKit account has litter box(es). <br/>- If multiple litter boxes, this will display data obtained from the most recent litter box used. <br/>- Will read 0 after first time setup if pet hasn't used litter box that day. |
| `Latest weight` | `Sensor` | - Most recent weight measurement obtained during last litter box use. <br/>- Only available if PetKit account has litter box(es). <br/>- If multiple litter boxes, this will display data obtained from the most recent litter box used. <br/>- By default the unit used is Kg. Unit can be changed in the settings of the entity. <br/>- Will read 0 after first time setup if pet hasn't used litter box that day. |
</details>

# Help With Translations
Expand Down
2 changes: 1 addition & 1 deletion custom_components/petkit/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"petkitaio==0.1.11.3",
"tzlocal==5.1"
],
"version": "0.1.12.3"
"version": "0.1.12.4"
}
83 changes: 55 additions & 28 deletions custom_components/petkit/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
UnitOfTime,
UnitOfVolume
)
from homeassistant.core import HomeAssistant
from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM

Expand Down Expand Up @@ -2230,7 +2231,7 @@ def sub_events_to_description(self, sub_events: list[dict[str, Any]]) -> list[st
event_list.append(description)
return event_list

class PetRecentWeight(CoordinatorEntity, SensorEntity):
class PetRecentWeight(CoordinatorEntity, SensorEntity, RestoreEntity):
"""Representation of most recent weight measured by litter box."""

def __init__(self, coordinator, pet_id):
Expand Down Expand Up @@ -2296,19 +2297,6 @@ def icon(self) -> str | None:
else:
return 'mdi:weight'

@property
def native_value(self) -> float:
"""Return most recent weight from today."""

sorted_dict = self.grab_recent_weight()
if sorted_dict:
last_key = list(sorted_dict)[-1]
latest_weight = sorted_dict[last_key]
weight_calculation = round((latest_weight / 1000), 1)
return weight_calculation
else:
return 0.0

@property
def native_unit_of_measurement(self) -> UnitOfMass:
"""Return kilograms as the native unit."""
Expand All @@ -2327,6 +2315,32 @@ def state_class(self) -> SensorStateClass:

return SensorStateClass.MEASUREMENT

@callback
def _handle_coordinator_update(self) -> None:
"""Handle entity update."""

sorted_dict = self.grab_recent_weight()
if sorted_dict:
last_key = list(sorted_dict)[-1]
latest_weight = sorted_dict[last_key]
weight_calculation = round((latest_weight / 1000), 1)
self._attr_native_value = weight_calculation
self.async_write_ha_state()
else:
return

async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""

await super().async_added_to_hass()

if last_state := await self.async_get_last_state():
self._attr_native_value = last_state.state
else:
# If user is setting up the integration when there is no weight yet
# for the current day, return 0.0
self._attr_native_value = 0.0

def grab_recent_weight(self) -> float:
"""Grab the most recent weight."""

Expand All @@ -2347,7 +2361,7 @@ def grab_recent_weight(self) -> float:
return sorted_dict


class PetLastUseDuration(CoordinatorEntity, SensorEntity):
class PetLastUseDuration(CoordinatorEntity, SensorEntity, RestoreEntity):
"""Representation of most recent litter box use duration."""

def __init__(self, coordinator, pet_id):
Expand Down Expand Up @@ -2413,18 +2427,6 @@ def icon(self) -> str | None:
else:
return 'mdi:clock'

@property
def native_value(self) -> int:
"""Return most recent duration from today."""

sorted_dict = self.grab_recent_duration()
if sorted_dict:
last_key = list(sorted_dict)[-1]
latest_duration = sorted_dict[last_key]
return latest_duration
else:
return 0

@property
def native_unit_of_measurement(self) -> UnitOfTime:
"""Return seconds as the native unit."""
Expand All @@ -2437,6 +2439,31 @@ def state_class(self) -> SensorStateClass:

return SensorStateClass.MEASUREMENT

@callback
def _handle_coordinator_update(self) -> None:
"""Handle entity update."""

sorted_dict = self.grab_recent_duration()
if sorted_dict:
last_key = list(sorted_dict)[-1]
latest_duration = sorted_dict[last_key]
self._attr_native_value = latest_duration
self.async_write_ha_state()
else:
return

async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""

await super().async_added_to_hass()

if last_state := await self.async_get_last_state():
self._attr_native_value = last_state.state
else:
# If user is setting up the integration when there is no duration yet
# for the current day, return 0
self._attr_native_value = 0

def grab_recent_duration(self) -> float:
"""Grab the most recent duration."""

Expand Down

0 comments on commit 41bf110

Please sign in to comment.