Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set_inside_pet method to allow this state to be configured #212

Draft
wants to merge 18 commits into
base: dev
Choose a base branch
from
39 changes: 38 additions & 1 deletion surepy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -428,3 +428,40 @@ 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: 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": int(profile_id.value)}

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!")

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
)

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
16 changes: 15 additions & 1 deletion surepy/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
14 changes: 13 additions & 1 deletion surepy/entities/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from typing import Any
from urllib.parse import urlparse

from surepy.entities import PetActivity, PetLocation, StateDrinking, StateFeeding, SurepyEntity
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

Expand Down Expand Up @@ -133,3 +134,14 @@ def last_lunch(self) -> datetime | None:
@property
def last_drink(self) -> datetime | None:
return self.drinking.at if self.drinking else None

@property
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=await get_inside_pet(self, device_id, tag_id),
)

6 changes: 6 additions & 0 deletions surepy/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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