Skip to content

Commit

Permalink
feat: add geocoding functionality
Browse files Browse the repository at this point in the history
chore: bump version number (v0.0.9 -> v0.0.10)
  • Loading branch information
alryaz committed Mar 22, 2024
1 parent c013e7d commit 74aa24a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pandora-cas"
version = "0.0.9"
version = "0.0.10"
description = "Pandora Car Alarm System clientside API implementation"
authors = ["Alexander Ryazanov <[email protected]>"]
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion src/pandora_cas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.9"
__version__ = "0.0.10"
__author__ = "Alexander Ryazanov <[email protected]>"
__all__ = ("account", "data", "device", "enums", "errors")
32 changes: 32 additions & 0 deletions src/pandora_cas/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,38 @@ async def async_request_updates(

return device_new_attrs, events

async def async_geocode(
self,
latitude: float,
longitude: float,
language: str | None = None,
full: bool = False,
) -> str | dict[str, str] | None:
"""
Retrieve the geocoded location for the given latitude and longitude.
:param latitude: Latitude
:param longitude: Longitude
:param language: Language code
:param full: Whether to return the whole response
:return: (Whole response) OR (Short address) OR (None if empty)
"""
if not (access_token := self.access_token):
raise MissingAccessTokenError("Account is not authenticated")
if language is None:
language = "ru"
async with self._session.get(
self.BASE_URL + "/api/geo",
params={
"lang": language,
"lat": latitude,
"lon": longitude,
"access_token": access_token,
# "_": 0,
},
) as response:
response = await self._handle_dict_response(response)
return response if full else response.get("short") or None

def _process_ws_initial_state(
self, device: "PandoraOnlineDevice", data: Mapping[str, Any]
) -> tuple[CurrentState, dict[str, Any]]:
Expand Down
17 changes: 17 additions & 0 deletions src/pandora_cas/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,23 @@ def update_current_state(self, **state_args) -> tuple[CurrentState, dict[str, An
self.state = new_state
return new_state, state_args

async def async_geocode(
self, language: str | None = None, full: bool = False
) -> str | dict[str, str] | None:
"""
Retrieve the geocoded location for the given latitude and longitude.
:param language: Language code
:param full: Whether to return the whole response
:return: (Whole response) OR (Short address) OR (None if empty)
"""
if not (state := self.state):
raise ValueError("State is not available")
if None in (state.latitude, state.longitude):
raise ValueError("Both latitude and longitude are required")
return await self.account.async_geocode(
state.latitude, state.longitude, language, full
)

async def async_fetch_last_event(self) -> TrackingEvent | None:
try:
return next(iter(await self.async_fetch_events(0, None, 1)))
Expand Down

0 comments on commit 74aa24a

Please sign in to comment.