Skip to content

Commit f7cc4fe

Browse files
committed
Add method to get user profile info
1 parent c00bb9d commit f7cc4fe

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

karcher/auth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class Domains:
1313
"""Domains URLs class.
1414
15-
This class represents a Karcher Home access URLs.
15+
This class represents a Karcher Home Robots access URLs.
1616
"""
1717
app_api: str
1818
mqtt: str
@@ -31,7 +31,7 @@ def __init__(self, **kwargs):
3131
class Session:
3232
"""Authorized user session class.
3333
34-
This class represents a Karcher Home authorized user session.
34+
This class represents a Karcher Home Robots authorized user session.
3535
"""
3636

3737
register_id: str

karcher/device.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DeviceStatus(int, Enum):
2222
class DeviceVersion:
2323
"""Device version class.
2424
25-
This class represents a Karcher Home device version.
25+
This class represents a Karcher Home Robots device version.
2626
"""
2727
package_type: str = ''
2828
version: int = 0
@@ -84,7 +84,7 @@ class DevicePropertiesQuiet:
8484
class DeviceProperties:
8585
"""Device properties class.
8686
87-
This class represents a Karcher Home detailed device properties.
87+
This class represents a Karcher Home Robots detailed device properties.
8888
"""
8989

9090
firmware: str = ''
@@ -161,7 +161,7 @@ def update(self, data: dict[str, Any]) -> bool:
161161
class Device:
162162
"""Device class.
163163
164-
This class represents a Karcher Home device.
164+
This class represents a Karcher Home Robots device.
165165
"""
166166

167167
device_id: str

karcher/karcher.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import aiohttp
1212
import urllib.parse
1313

14-
1514
from .auth import Domains, Session
1615
from .countries import get_country_code, get_region_by_country
1716
from .consts import (
@@ -23,6 +22,7 @@
2322
from .exception import KarcherHomeAccessDenied, KarcherHomeException, handle_error_code
2423
from .map import Map
2524
from .mqtt import MqttClient, get_device_topic_property_get_reply, get_device_topics
25+
from .user import UserProfile
2626
from .utils import (
2727
decrypt, decrypt_map, encrypt, get_nonce, get_random_string,
2828
get_timestamp, get_timestamp_ms, is_email, md5
@@ -49,12 +49,12 @@ async def create(
4949
self._http_external = True
5050
self._http = session
5151

52-
d = await self.get_urls()
52+
data = await self.get_urls()
5353
# Update base URLs
54-
if d.app_api != '':
55-
self._base_url = d.app_api
56-
if d.mqtt != '':
57-
self._mqtt_url = d.mqtt
54+
if data.app_api != '':
55+
self._base_url = data.app_api
56+
if data.mqtt != '':
57+
self._mqtt_url = data.mqtt
5858

5959
return self
6060

@@ -218,8 +218,8 @@ async def get_urls(self) -> Domains:
218218
'version': PROTOCOL_VERSION,
219219
})
220220

221-
d = await self._process_response(resp, 'domain')
222-
return Domains(**d)
221+
data = await self._process_response(resp, 'domain')
222+
return Domains(**data)
223223

224224
async def login(self, username, password, register_id=None) -> Session:
225225
"""Login using provided credentials."""
@@ -249,8 +249,8 @@ async def login(self, username, password, register_id=None) -> Session:
249249
},
250250
})
251251

252-
d = await self._process_response(resp)
253-
self._session = Session(**d)
252+
data = await self._process_response(resp)
253+
self._session = Session(**data)
254254
self._session.register_id = register_id
255255

256256
return self._session
@@ -286,6 +286,20 @@ async def logout(self):
286286

287287
await self.close()
288288

289+
async def get_user_info(self) -> UserProfile:
290+
"""Get user profile information."""
291+
292+
if self._session is None \
293+
or self._session.auth_token == '' or self._session.user_id == '':
294+
raise KarcherHomeAccessDenied('Not authorized')
295+
296+
resp = await self._request(
297+
'GET',
298+
'/user-center/app/user/profile')
299+
data = await self._process_response(resp)
300+
301+
return UserProfile(**data)
302+
289303
async def get_devices(self) -> List[Device]:
290304
"""Get all user devices."""
291305

@@ -315,13 +329,13 @@ async def get_map_data(self, dev: Device, map: int = 1):
315329
'tenantId': TENANT_ID,
316330
})
317331

318-
d = await self._process_response(resp)
319-
downloadUrl = d['url']
320-
if 'cdnDomain' in d and d['cdnDomain'] != '':
321-
downloadUrl = 'https://' + d['cdnDomain'] + '/' + d['dir']
332+
data = await self._process_response(resp)
333+
downloadUrl = data['url']
334+
if 'cdnDomain' in data and data['cdnDomain'] != '':
335+
downloadUrl = 'https://' + data['cdnDomain'] + '/' + data['dir']
322336

323-
d = await self._download(downloadUrl)
324-
data = decrypt_map(dev.sn, dev.mac, dev.product_id, d)
337+
data = await self._download(downloadUrl)
338+
data = decrypt_map(dev.sn, dev.mac, dev.product_id, data)
325339
if map == 1 or map == 2:
326340
return Map.parse(data)
327341
else:

karcher/user.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -----------------------------------------------------------
2+
# Copyright (c) 2023 Lauris BH
3+
# SPDX-License-Identifier: MIT
4+
# -----------------------------------------------------------
5+
6+
from dataclasses import dataclass, fields
7+
8+
from .utils import decrypt, snake_case
9+
10+
@dataclass(init=False)
11+
class UserProfile:
12+
"""User profile class.
13+
14+
This class represents a Karcher Home Robots user profile.
15+
"""
16+
nickname: str = ''
17+
avatar_url: str = ''
18+
email: str = ''
19+
phone: str = ''
20+
device: int = 0
21+
22+
def __init__(self, **kwargs):
23+
names = set([f.name for f in fields(self)])
24+
for k, v in kwargs.items():
25+
if k == 'nickName':
26+
k = 'nickname'
27+
else:
28+
k = snake_case(k)
29+
if k in names:
30+
if k in ('email', 'phone'):
31+
v = decrypt(v)
32+
setattr(self, k, v)

0 commit comments

Comments
 (0)