pypetkitapi 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pypetkitapi
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Python client for PetKit API
5
5
  Home-page: https://github.com/Jezza34000/pypetkit
6
6
  License: MIT
@@ -19,10 +19,6 @@ Description-Content-Type: text/markdown
19
19
 
20
20
  ---
21
21
 
22
- # WIP - UNDER DEVELOPMENT
23
-
24
- ---
25
-
26
22
  [![PyPI](https://img.shields.io/pypi/v/pypetkitapi.svg)][pypi_]
27
23
  [![Python Version](https://img.shields.io/pypi/pyversions/pypetkitapi)][python version]
28
24
 
@@ -72,29 +68,29 @@ logging.basicConfig(level=logging.DEBUG)
72
68
 
73
69
  async def main():
74
70
  client = PetKitClient(
75
- username="username", # Your PetKit account username
71
+ username="username", # Your PetKit account username or id
76
72
  password="password", # Your PetKit account password
77
- region="France", # Your region
73
+ region="France", # Your region or country code (e.g. FR, US, etc.)
78
74
  timezone="Europe/Paris", # Your timezone
79
75
  )
80
76
 
81
77
  # To get the account and devices data attached to the account
82
78
  await client.get_devices_data()
79
+
83
80
  # Read the account data
84
81
  print(client.account_data)
82
+
85
83
  # Read the devices data
86
84
  print(client.device_list)
87
85
 
88
- # client.device_list[0] is the first device in the list in this example it's a Feeder
89
- # Get the Feeder from the device list
90
- my_feeder = client.device_list[0]
91
-
92
86
  # Send command to the devices
93
87
  ### Example 1 : Turn on the indicator light
94
- await client.send_api_request(my_feeder, DeviceCommand.UPDATE_SETTING, {"lightMode": 1})
88
+ ### Device_ID, Command, Payload
89
+ await client.send_api_request(012346789, DeviceCommand.UPDATE_SETTING, {"lightMode": 1})
95
90
 
96
91
  ### Example 2 : Feed the pet
97
- await client.send_api_request(my_feeder, FeederCommand.MANUAL_FEED, {"amount": 1})
92
+ ### Device_ID, Command, Payload
93
+ await client.send_api_request(0123467, FeederCommand.MANUAL_FEED, {"amount": 1})
98
94
 
99
95
 
100
96
  if __name__ == "__main__":
@@ -2,10 +2,6 @@
2
2
 
3
3
  ---
4
4
 
5
- # WIP - UNDER DEVELOPMENT
6
-
7
- ---
8
-
9
5
  [![PyPI](https://img.shields.io/pypi/v/pypetkitapi.svg)][pypi_]
10
6
  [![Python Version](https://img.shields.io/pypi/pyversions/pypetkitapi)][python version]
11
7
 
@@ -55,29 +51,29 @@ logging.basicConfig(level=logging.DEBUG)
55
51
 
56
52
  async def main():
57
53
  client = PetKitClient(
58
- username="username", # Your PetKit account username
54
+ username="username", # Your PetKit account username or id
59
55
  password="password", # Your PetKit account password
60
- region="France", # Your region
56
+ region="France", # Your region or country code (e.g. FR, US, etc.)
61
57
  timezone="Europe/Paris", # Your timezone
62
58
  )
63
59
 
64
60
  # To get the account and devices data attached to the account
65
61
  await client.get_devices_data()
62
+
66
63
  # Read the account data
67
64
  print(client.account_data)
65
+
68
66
  # Read the devices data
69
67
  print(client.device_list)
70
68
 
71
- # client.device_list[0] is the first device in the list in this example it's a Feeder
72
- # Get the Feeder from the device list
73
- my_feeder = client.device_list[0]
74
-
75
69
  # Send command to the devices
76
70
  ### Example 1 : Turn on the indicator light
77
- await client.send_api_request(my_feeder, DeviceCommand.UPDATE_SETTING, {"lightMode": 1})
71
+ ### Device_ID, Command, Payload
72
+ await client.send_api_request(012346789, DeviceCommand.UPDATE_SETTING, {"lightMode": 1})
78
73
 
79
74
  ### Example 2 : Feed the pet
80
- await client.send_api_request(my_feeder, FeederCommand.MANUAL_FEED, {"amount": 1})
75
+ ### Device_ID, Command, Payload
76
+ await client.send_api_request(0123467, FeederCommand.MANUAL_FEED, {"amount": 1})
81
77
 
82
78
 
83
79
  if __name__ == "__main__":
@@ -18,6 +18,7 @@ from pypetkitapi.const import (
18
18
  ERR_KEY,
19
19
  LOGIN_DATA,
20
20
  RES_KEY,
21
+ SUCCESS_KEY,
21
22
  Header,
22
23
  PetkitEndpoint,
23
24
  PetkitURL,
@@ -45,7 +46,7 @@ class PetKitClient:
45
46
  _session: SessionInfo | None = None
46
47
  _servers_list: list[RegionInfo] = []
47
48
  account_data: list[AccountData] = []
48
- device_list: list[Feeder | Litter | WaterFountain] = []
49
+ device_list: dict[int, Feeder | Litter | WaterFountain] = {}
49
50
 
50
51
  def __init__(
51
52
  self,
@@ -100,11 +101,16 @@ class PetKitClient:
100
101
  await self._get_api_server_list()
101
102
  _LOGGER.debug("Finding region server for region: %s", self.region)
102
103
 
104
+ if self.region == "china":
105
+ self._base_url = PetkitURL.CHINA_SRV
106
+ return
107
+
103
108
  regional_server = next(
104
109
  (
105
110
  server
106
111
  for server in self._servers_list
107
112
  if server.name.lower() == self.region
113
+ or server.id.lower() == self.region
108
114
  ),
109
115
  None,
110
116
  )
@@ -263,22 +269,28 @@ class PetKitClient:
263
269
  )
264
270
  device_data = data_class(**response)
265
271
  device_data.device_type = device.device_type # Add the device_type attribute
266
- _LOGGER.debug("Reading device type : %s", device.device_type)
267
- self.device_list.append(device_data)
272
+ _LOGGER.debug(
273
+ "Reading device type : %s (id=%s)", device.device_type, device.device_id
274
+ )
275
+ self.device_list[device.device_id] = device_data
268
276
 
269
277
  async def send_api_request(
270
278
  self,
271
- device: Feeder | Litter | WaterFountain,
279
+ device_id: int,
272
280
  action: StrEnum,
273
281
  setting: dict | None = None,
274
282
  ) -> None:
275
283
  """Control the device using the PetKit API."""
284
+ device = self.device_list.get(device_id)
285
+ if not device:
286
+ raise PypetkitError(f"Device with ID {device_id} not found.")
276
287
 
277
288
  _LOGGER.debug(
278
- "Control API: %s %s %s",
289
+ "Control API device=%s id=%s action=%s param=%s",
290
+ device.device_type,
291
+ device_id,
279
292
  action,
280
293
  setting,
281
- device,
282
294
  )
283
295
 
284
296
  if device.device_type:
@@ -312,12 +324,16 @@ class PetKitClient:
312
324
  params = action_info.params(device)
313
325
 
314
326
  prep_req = PrepReq(base_url=self._base_url)
315
- await prep_req.request(
327
+ res = await prep_req.request(
316
328
  method=HTTPMethod.POST,
317
329
  url=url,
318
330
  data=params,
319
331
  headers=headers,
320
332
  )
333
+ if res == SUCCESS_KEY:
334
+ _LOGGER.info("Command executed successfully")
335
+ else:
336
+ _LOGGER.error("Command execution failed")
321
337
 
322
338
 
323
339
  class PrepReq:
@@ -35,6 +35,7 @@ class PetkitURL(StrEnum):
35
35
  """Petkit URL constants"""
36
36
 
37
37
  REGION_SRV = "https://passport.petkt.com/v1/regionservers"
38
+ CHINA_SRV = "https://api.petkit.cn/6/"
38
39
 
39
40
 
40
41
  class Client(StrEnum):
@@ -26,7 +26,7 @@ class SessionInfo(BaseModel):
26
26
  id: str
27
27
  user_id: str = Field(alias="userId")
28
28
  expires_in: int = Field(alias="expiresIn")
29
- region: str
29
+ region: str | None = None
30
30
  created_at: str = Field(alias="createdAt")
31
31
 
32
32
 
@@ -187,7 +187,7 @@ build-backend = "poetry.core.masonry.api"
187
187
 
188
188
  [tool.poetry]
189
189
  name = "pypetkitapi"
190
- version = "0.2.0"
190
+ version = "0.3.0"
191
191
  description = "Python client for PetKit API"
192
192
  authors = ["Jezza34000 <info@mail.com>"]
193
193
  readme = "README.md"
@@ -204,7 +204,7 @@ black = "^24.10.0"
204
204
  ruff = "^0.8.1"
205
205
 
206
206
  [tool.bumpver]
207
- current_version = "0.2.0"
207
+ current_version = "0.3.0"
208
208
  version_pattern = "MAJOR.MINOR.PATCH"
209
209
  commit_message = "bump version {old_version} -> {new_version}"
210
210
  tag_message = "{new_version}"
File without changes