From fdf25362dd7c0d531309a4c44fbe5ccfb9fc3986 Mon Sep 17 00:00:00 2001
From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com>
Date: Wed, 6 Nov 2024 13:29:08 -0500
Subject: [PATCH 1/3] prevent pet weight and use duration from resetting at
midnight
---
custom_components/petkit/sensor.py | 83 ++++++++++++++++++++----------
1 file changed, 55 insertions(+), 28 deletions(-)
diff --git a/custom_components/petkit/sensor.py b/custom_components/petkit/sensor.py
index 1f10500..3a6249d 100644
--- a/custom_components/petkit/sensor.py
+++ b/custom_components/petkit/sensor.py
@@ -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
@@ -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):
@@ -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."""
@@ -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."""
@@ -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):
@@ -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."""
@@ -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."""
From f0f531a0b66cbe9069d53e2da75b690e5f20580e Mon Sep 17 00:00:00 2001
From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com>
Date: Wed, 6 Nov 2024 13:57:09 -0500
Subject: [PATCH 2/3] update last use duration and latest weight description
---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index cb36b1a..c3cc159 100644
--- a/README.md
+++ b/README.md
@@ -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.
- Only available if PetKit account has litter box(es).
- 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.
- Only available if PetKit account has litter box(es).
- If multiple litter boxes, this will display data obtained from the most recent litter box used.
- 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.
- Only available if PetKit account has litter box(es).
- If multiple litter boxes, this will display data obtained from the most recent litter box used.
- 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.
- Only available if PetKit account has litter box(es).
- If multiple litter boxes, this will display data obtained from the most recent litter box used.
- By default the unit used is Kg. Unit can be changed in the settings of the entity.
- Will read 0 after first time setup if pet hasn't used litter box that day. |
# Help With Translations
From 28f5881d1059aafac72b62510e2263230048f30a Mon Sep 17 00:00:00 2001
From: "Dr. Drinovac" <52541649+RobertD502@users.noreply.github.com>
Date: Wed, 6 Nov 2024 13:57:53 -0500
Subject: [PATCH 3/3] bump to version 0.1.12.4
---
custom_components/petkit/manifest.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/custom_components/petkit/manifest.json b/custom_components/petkit/manifest.json
index a31ba5b..3bd7916 100644
--- a/custom_components/petkit/manifest.json
+++ b/custom_components/petkit/manifest.json
@@ -17,5 +17,5 @@
"petkitaio==0.1.11.3",
"tzlocal==5.1"
],
- "version": "0.1.12.3"
+ "version": "0.1.12.4"
}