From 29f83d171fa262da35c3ef5ef8e4bef5dd7e50a7 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 15:17:45 +0100 Subject: [PATCH 01/18] Added set_inside_pet method to client.py --- surepy/client.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/surepy/client.py b/surepy/client.py index 8ac750c..c3d00c3 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -428,3 +428,25 @@ async def _remove_tag_from_device(self, device_id: int, tag_id: int) -> dict[str if response := await self.call(method="DELETE", resource=resource): return response + + async def set_inside_pet(self, device_id: int, tag_id: int, profile_id: int) -> dict[str, Any] | None: + """Set a pet as inside only or outside""" + resource = DEVICE_TAG_RESOURCE.format( + BASE_RESOURCE=BASE_RESOURCE, device_id=device_id, tag_id=tag_id + ) + data = {"profile": profile_id} + + if ( + response := await self.call( + method="PUT", resource=resource, device_id=device_id, data=data + ) + ) and (response_data := response.get("data")): + desired_state = data.get("profile") + state = response_data.get("profile") + + # check if the state is correctly updated + if state == desired_state: + return response + + # return None + raise SurePetcareError("ERROR SETTING INSIDE PET STATE - PLEASE CHECK IMMEDIATELY!") From c96e7b34f6c1093c69dc4d07fc7d75c07bc3728b Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 15:31:23 +0100 Subject: [PATCH 02/18] added set_inside_pet method --- surepy/client.py | 6 +++--- surepy/enums.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/surepy/client.py b/surepy/client.py index c3d00c3..2ae3d3a 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -47,7 +47,7 @@ SUREPY_USER_AGENT, USER_AGENT, ) -from .enums import Location, LockState +from .enums import Location, LockState, InsidePet from .exceptions import SurePetcareAuthenticationError, SurePetcareConnectionError, SurePetcareError @@ -429,12 +429,12 @@ async def _remove_tag_from_device(self, device_id: int, tag_id: int) -> dict[str if response := await self.call(method="DELETE", resource=resource): return response - async def set_inside_pet(self, device_id: int, tag_id: int, profile_id: int) -> dict[str, Any] | None: + async def set_inside_pet(self, device_id: int, tag_id: int, profile_id: InsidePet) -> dict[str, Any] | None: """Set a pet as inside only or outside""" resource = DEVICE_TAG_RESOURCE.format( BASE_RESOURCE=BASE_RESOURCE, device_id=device_id, tag_id=tag_id ) - data = {"profile": profile_id} + data = {"profile": int(profile_id.value)} if ( response := await self.call( diff --git a/surepy/enums.py b/surepy/enums.py index 6853e02..31a01cd 100644 --- a/surepy/enums.py +++ b/surepy/enums.py @@ -76,3 +76,9 @@ class TimelineEvent(SureEnum): DRINK = 29 REMINDER_FRESH_WATER = 32 ANONYMOUS_DRINK = 34 + +class InsidePet(SureEnum): + """Inside Pet Profiles""" + + INSIDE_ONLY = 3 + OUTSIDE = 2 From f5ae4095132e68fcca4c82dc792fdc8aa3e35c36 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 20:51:13 +0100 Subject: [PATCH 03/18] missing entities --- surepy/entities/__init__.py | 16 +++++++++++++++- surepy/entities/pet.py | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/surepy/entities/__init__.py b/surepy/entities/__init__.py index 2afa0cd..1a93864 100644 --- a/surepy/entities/__init__.py +++ b/surepy/entities/__init__.py @@ -6,7 +6,7 @@ from pprint import pformat from typing import Any -from surepy.enums import EntityType, Location +from surepy.enums import EntityType, Location, InsidePet class SurepyEntity(ABC): @@ -86,3 +86,17 @@ class PetActivity(PetLocationData): @dataclass class PetLocation(PetLocationData): pass + + +@dataclass +class PetInsideData: + + profile: InsidePet + + def __str__(self) -> str: + return self.where.name.title() + + +@dataclass +class StateInside(PetInsideData): + pass diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index ee298ae..b163d0b 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -12,9 +12,9 @@ from typing import Any from urllib.parse import urlparse -from surepy.entities import PetActivity, PetLocation, StateDrinking, StateFeeding, SurepyEntity +from surepy.entities import PetActivity, PetLocation, StateDrinking, StateFeeding, SurepyEntity, StateInside from surepy.entities.states import PetState -from surepy.enums import EntityType, FoodType, Location +from surepy.enums import EntityType, FoodType, Location, InsidePet class Pet(SurepyEntity): @@ -133,3 +133,18 @@ def last_lunch(self) -> datetime | None: @property def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None + + @property + def inside_only(self) -> bool: + """State of inside only.""" + return bool(self.profile_id.profile == InsidePet.INSIDE_ONLY) + + @property + def profile_id(self) -> StateInside: + """State of inside only.""" + profile = self._data.get("profile", {}) + # pylint: disable=no-member + return StateInside( + profile=StateInside(profile.get("profile", InsidePet.INSIDE_ONLY.value)), + ) + \ No newline at end of file From 67972ed6a26fda0daa8d75565b6ad2b025820f41 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 21:06:47 +0100 Subject: [PATCH 04/18] fixes --- surepy/entities/pet.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index b163d0b..29c8215 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -134,11 +134,6 @@ def last_lunch(self) -> datetime | None: def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None - @property - def inside_only(self) -> bool: - """State of inside only.""" - return bool(self.profile_id.profile == InsidePet.INSIDE_ONLY) - @property def profile_id(self) -> StateInside: """State of inside only.""" From aaf7e0abd0f9b9a15a26b91e2fa2aefc8be977a2 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 21:07:03 +0100 Subject: [PATCH 05/18] fixes --- surepy/entities/pet.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 29c8215..9c86b3e 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -133,13 +133,11 @@ def last_lunch(self) -> datetime | None: @property def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None - + @property def profile_id(self) -> StateInside: """State of inside only.""" profile = self._data.get("profile", {}) # pylint: disable=no-member - return StateInside( - profile=StateInside(profile.get("profile", InsidePet.INSIDE_ONLY.value)), - ) + return profile \ No newline at end of file From 2f7d79fb1ea0f7335f68987fa4c204b384536887 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 21:50:51 +0100 Subject: [PATCH 06/18] fix profile_id --- surepy/entities/pet.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 9c86b3e..b1150e7 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -137,7 +137,8 @@ def last_drink(self) -> datetime | None: @property def profile_id(self) -> StateInside: """State of inside only.""" - profile = self._data.get("profile", {}) # pylint: disable=no-member - return profile + return StateInside( + profile = self._data.get("profile", {}) + ) \ No newline at end of file From 3bb9dc6b02a04663fb51228a448108bb68f877a4 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:21:00 +0100 Subject: [PATCH 07/18] add new method --- surepy/client.py | 18 ++++++++++++++++++ surepy/entities/pet.py | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/surepy/client.py b/surepy/client.py index 2ae3d3a..868cb55 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -450,3 +450,21 @@ async def set_inside_pet(self, device_id: int, tag_id: int, profile_id: InsidePe # return None raise SurePetcareError("ERROR SETTING INSIDE PET STATE - PLEASE CHECK IMMEDIATELY!") + +async def get_inside_pet(self, device_id: int, tag_id: int) -> dict[str, Any] | None: + """Set a pet as inside only or outside""" + resource = DEVICE_TAG_RESOURCE.format( + BASE_RESOURCE=BASE_RESOURCE, device_id=device_id, tag_id=tag_id + ) + + if ( + response := await self.call( + method="GET", resource=resource, device_id=device_id + ) + ) and (response_data := response.get("data")): + id = response_data.get("profile") + + return id + + # return None + raise SurePetcareError("ERROR GETTING INSIDE PET STATE - PLEASE CHECK IMMEDIATELY!") diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index b1150e7..7482ff4 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -12,6 +12,7 @@ from typing import Any from urllib.parse import urlparse +from surepy.client import get_inside_pet from surepy.entities import PetActivity, PetLocation, StateDrinking, StateFeeding, SurepyEntity, StateInside from surepy.entities.states import PetState from surepy.enums import EntityType, FoodType, Location, InsidePet @@ -139,6 +140,6 @@ def profile_id(self) -> StateInside: """State of inside only.""" # pylint: disable=no-member return StateInside( - profile = self._data.get("profile", {}) + profile=InsidePet(get_inside_pet(tag_id := self._data.get("device_id",tag_id := self._data.get("tag_id")))) ) \ No newline at end of file From 54b857d945f12ec0c7dc8c64f2463f545b2fdce3 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:29:46 +0100 Subject: [PATCH 08/18] further fixes --- surepy/entities/pet.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 7482ff4..56b9fb8 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -138,6 +138,8 @@ def last_drink(self) -> datetime | None: @property def profile_id(self) -> StateInside: """State of inside only.""" + device_id = self._data.get("device_id", {}), + tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( profile=InsidePet(get_inside_pet(tag_id := self._data.get("device_id",tag_id := self._data.get("tag_id")))) From e412d9fbbc5d74c2da92ebfb58fceb59f95cf249 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:30:00 +0100 Subject: [PATCH 09/18] further fixes --- surepy/entities/pet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 56b9fb8..a8e3a6f 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -142,6 +142,6 @@ def profile_id(self) -> StateInside: tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( - profile=InsidePet(get_inside_pet(tag_id := self._data.get("device_id",tag_id := self._data.get("tag_id")))) + profile=InsidePet(get_inside_pet(device_id,tag_id)) ) \ No newline at end of file From 3521dbc62df186b86ef2a1d37692ec20063141dc Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:43:47 +0100 Subject: [PATCH 10/18] more fixes --- surepy/client.py | 7 ++----- surepy/entities/__init__.py | 14 -------------- surepy/entities/pet.py | 6 +++--- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/surepy/client.py b/surepy/client.py index 868cb55..990e8d3 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -451,8 +451,8 @@ async def set_inside_pet(self, device_id: int, tag_id: int, profile_id: InsidePe # return None raise SurePetcareError("ERROR SETTING INSIDE PET STATE - PLEASE CHECK IMMEDIATELY!") -async def get_inside_pet(self, device_id: int, tag_id: int) -> dict[str, Any] | None: - """Set a pet as inside only or outside""" +async def get_inside_pet(self, device_id: int, tag_id: int) -> int | None: + """Get info on inside only or outside""" resource = DEVICE_TAG_RESOURCE.format( BASE_RESOURCE=BASE_RESOURCE, device_id=device_id, tag_id=tag_id ) @@ -465,6 +465,3 @@ async def get_inside_pet(self, device_id: int, tag_id: int) -> dict[str, Any] | id = response_data.get("profile") return id - - # return None - raise SurePetcareError("ERROR GETTING INSIDE PET STATE - PLEASE CHECK IMMEDIATELY!") diff --git a/surepy/entities/__init__.py b/surepy/entities/__init__.py index 1a93864..486d892 100644 --- a/surepy/entities/__init__.py +++ b/surepy/entities/__init__.py @@ -86,17 +86,3 @@ class PetActivity(PetLocationData): @dataclass class PetLocation(PetLocationData): pass - - -@dataclass -class PetInsideData: - - profile: InsidePet - - def __str__(self) -> str: - return self.where.name.title() - - -@dataclass -class StateInside(PetInsideData): - pass diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index a8e3a6f..e2a57d5 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -138,10 +138,10 @@ def last_drink(self) -> datetime | None: @property def profile_id(self) -> StateInside: """State of inside only.""" - device_id = self._data.get("device_id", {}), - tag_id = self._data.get("tag_id", {}), + device_id = int(self._data.get("device_id", {})), + tag_id = int(self._data.get("tag_id", {})), # pylint: disable=no-member return StateInside( - profile=InsidePet(get_inside_pet(device_id,tag_id)) + profile=InsidePet(int(get_inside_pet(device_id, tag_id))) ) \ No newline at end of file From 87e401fde53a247c605fb7c9ee07e2137c3db063 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:48:12 +0100 Subject: [PATCH 11/18] fix --- surepy/entities/__init__.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/surepy/entities/__init__.py b/surepy/entities/__init__.py index 486d892..1a93864 100644 --- a/surepy/entities/__init__.py +++ b/surepy/entities/__init__.py @@ -86,3 +86,17 @@ class PetActivity(PetLocationData): @dataclass class PetLocation(PetLocationData): pass + + +@dataclass +class PetInsideData: + + profile: InsidePet + + def __str__(self) -> str: + return self.where.name.title() + + +@dataclass +class StateInside(PetInsideData): + pass From 750f31802071c146874775d8d80bc0f698ac3f82 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 22:53:15 +0100 Subject: [PATCH 12/18] int mistakes --- surepy/entities/pet.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index e2a57d5..2036b09 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -138,10 +138,10 @@ def last_drink(self) -> datetime | None: @property def profile_id(self) -> StateInside: """State of inside only.""" - device_id = int(self._data.get("device_id", {})), - tag_id = int(self._data.get("tag_id", {})), + device_id = self._data.get("device_id", {}), + tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( - profile=InsidePet(int(get_inside_pet(device_id, tag_id))) + profile=InsidePet(get_inside_pet(device_id, tag_id)) ) \ No newline at end of file From 6c712c5717485d6c677f749927a0c9a6afb9a0c9 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:09:08 +0100 Subject: [PATCH 13/18] fix --- surepy/entities/pet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 2036b09..bfaee9e 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -142,6 +142,6 @@ def profile_id(self) -> StateInside: tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( - profile=InsidePet(get_inside_pet(device_id, tag_id)) + profile=InsidePet(get_inside_pet(self, device_id, tag_id)) ) \ No newline at end of file From de752db669c45893478b700f4eb04bfd4faac331 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:13:34 +0100 Subject: [PATCH 14/18] fix --- surepy/entities/pet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index bfaee9e..915b351 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -142,6 +142,6 @@ def profile_id(self) -> StateInside: tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( - profile=InsidePet(get_inside_pet(self, device_id, tag_id)) + profile=get_inside_pet(self, device_id, tag_id) ) \ No newline at end of file From 80b6b929d51be34ff13e6171695f42ffda5c44fa Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:28:45 +0100 Subject: [PATCH 15/18] fix again --- surepy/entities/pet.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index 915b351..d85bd9c 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -15,7 +15,7 @@ from surepy.client import get_inside_pet from surepy.entities import PetActivity, PetLocation, StateDrinking, StateFeeding, SurepyEntity, StateInside from surepy.entities.states import PetState -from surepy.enums import EntityType, FoodType, Location, InsidePet +from surepy.enums import EntityType, FoodType, Location class Pet(SurepyEntity): @@ -136,12 +136,11 @@ def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None @property - def profile_id(self) -> StateInside: + def profile_id(self) -> int: """State of inside only.""" device_id = self._data.get("device_id", {}), tag_id = self._data.get("tag_id", {}), + profile=get_inside_pet(self, device_id, tag_id), # pylint: disable=no-member - return StateInside( - profile=get_inside_pet(self, device_id, tag_id) - ) + return profile \ No newline at end of file From 2cb870527681fbbaba940701199c8610e619c092 Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:37:36 +0100 Subject: [PATCH 16/18] . --- surepy/entities/pet.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index d85bd9c..bfc55ad 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -136,11 +136,12 @@ def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None @property - def profile_id(self) -> int: + def profile_id(self) -> StateInside: """State of inside only.""" device_id = self._data.get("device_id", {}), tag_id = self._data.get("tag_id", {}), - profile=get_inside_pet(self, device_id, tag_id), # pylint: disable=no-member - return profile + return StateInside( + profile=get_inside_pet(self, device_id, tag_id), + ) \ No newline at end of file From 23790043911132e12bd2e8feee36c6d3a9845c7a Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:52:16 +0100 Subject: [PATCH 17/18] . --- surepy/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/surepy/client.py b/surepy/client.py index 990e8d3..2065707 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -462,6 +462,6 @@ async def get_inside_pet(self, device_id: int, tag_id: int) -> int | None: method="GET", resource=resource, device_id=device_id ) ) and (response_data := response.get("data")): - id = response_data.get("profile") + id = await response_data.get("profile") return id From b85370918df009c06e408a9adb425e5926b421ce Mon Sep 17 00:00:00 2001 From: bushbrother Date: Sat, 20 Jul 2024 23:59:39 +0100 Subject: [PATCH 18/18] added await --- surepy/client.py | 2 +- surepy/entities/pet.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/surepy/client.py b/surepy/client.py index 2065707..990e8d3 100644 --- a/surepy/client.py +++ b/surepy/client.py @@ -462,6 +462,6 @@ async def get_inside_pet(self, device_id: int, tag_id: int) -> int | None: method="GET", resource=resource, device_id=device_id ) ) and (response_data := response.get("data")): - id = await response_data.get("profile") + id = response_data.get("profile") return id diff --git a/surepy/entities/pet.py b/surepy/entities/pet.py index bfc55ad..bbd9183 100644 --- a/surepy/entities/pet.py +++ b/surepy/entities/pet.py @@ -136,12 +136,12 @@ def last_drink(self) -> datetime | None: return self.drinking.at if self.drinking else None @property - def profile_id(self) -> StateInside: + async def profile_id(self) -> StateInside: """State of inside only.""" device_id = self._data.get("device_id", {}), tag_id = self._data.get("tag_id", {}), # pylint: disable=no-member return StateInside( - profile=get_inside_pet(self, device_id, tag_id), + profile=await get_inside_pet(self, device_id, tag_id), ) \ No newline at end of file