Skip to content

Commit

Permalink
clean find my types
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNikiforov authored May 24, 2024
1 parent 700d74a commit 28e78a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/pyicloud_ipd/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from typing import Any, Dict, NoReturn, Optional, Sequence
from typing import Any, Dict, Iterable, NoReturn, Optional, Sequence
from typing_extensions import override
import typing
from uuid import uuid1
Expand All @@ -20,7 +20,7 @@
PyiCloud2SARequiredException,
PyiCloudServiceNotActivatedException,
)
from pyicloud_ipd.services.findmyiphone import FindMyiPhoneServiceManager
from pyicloud_ipd.services.findmyiphone import AppleDevice, FindMyiPhoneServiceManager
from pyicloud_ipd.services.calendar import CalendarService
from pyicloud_ipd.services.ubiquity import UbiquityService
from pyicloud_ipd.services.contacts import ContactsService
Expand Down Expand Up @@ -602,14 +602,14 @@ def _get_webservice_url(self, ws_key: str) -> str:
return typing.cast(str, self._webservices[ws_key]["url"])

@property
def devices(self): # type: ignore
def devices(self) -> Sequence[AppleDevice]:
""" Return all devices."""
service_root = self._get_webservice_url("findme")
return FindMyiPhoneServiceManager( # type: ignore
return typing.cast(Sequence[AppleDevice], FindMyiPhoneServiceManager(
service_root,
self.session,
self.params
)
))

@property
def account(self): # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion src/pyicloud_ipd/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

from pyicloud_ipd.base import PyiCloudService
from pyicloud_ipd.exceptions import PyiCloudFailedLoginException
from pyicloud_ipd.services.findmyiphone import AppleDevice
from . import utils

DEVICE_ERROR = "Please use the --device switch to indicate which device to use."


def create_pickled_data(idevice, filename: str) -> None:
def create_pickled_data(idevice: AppleDevice, filename: str) -> None:
"""
This helper will output the idevice to a pickled file named
after the passed filename.
Expand Down
65 changes: 33 additions & 32 deletions src/pyicloud_ipd/services/findmyiphone.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mypy: ignore-errors
import json
import sys
from typing import Any, Dict, Iterable, Optional, Sequence
import typing

import six

Expand All @@ -15,7 +16,7 @@ class FindMyiPhoneServiceManager(object):
"""

def __init__(self, service_root, session, params):
def __init__(self, service_root: str, session:Any, params: Dict[str, Any]):
self.session = session
self.params = params
self._service_root = service_root
Expand All @@ -25,10 +26,10 @@ def __init__(self, service_root, session, params):
self._fmip_message_url = '%s/sendMessage' % self._fmip_endpoint
self._fmip_lost_url = '%s/lostDevice' % self._fmip_endpoint

self._devices = {}
self._devices:Dict[str, AppleDevice] = {}
self.refresh_client()

def refresh_client(self):
def refresh_client(self) -> None:
""" Refreshes the FindMyiPhoneService endpoint,
This ensures that the location data is up-to-date.
Expand Down Expand Up @@ -67,35 +68,35 @@ def refresh_client(self):
if not self._devices:
raise PyiCloudNoDevicesException()

def __getitem__(self, key):
def __getitem__(self, key: Any) -> "AppleDevice":
if isinstance(key, int):
if six.PY3:
key = list(self.keys())[key]
key = list(self.keys())[key] # type: ignore[operator]
else:
key = self.keys()[key]
return self._devices[key]

def __getattr__(self, attr):
return getattr(self._devices, attr)
def __getattr__(self, attr: str) -> "AppleDevice":
return typing.cast(AppleDevice, getattr(self._devices, attr))

def __unicode__(self):
def __unicode__(self) -> str:
return six.text_type(self._devices)

def __str__(self):
def __str__(self) -> str:
as_unicode = self.__unicode__()
if sys.version_info[0] >= 3:
return as_unicode
else:
return as_unicode.encode('ascii', 'ignore')

def __repr__(self):
def __repr__(self) -> str:
return six.text_type(self)


class AppleDevice(object):
def __init__(
self, content, session, params, manager,
sound_url=None, lost_url=None, message_url=None
self, content:Dict[str,Any], session:Any, params:Dict[str, Any], manager: FindMyiPhoneServiceManager,
sound_url:Optional[str]=None, lost_url:Optional[str]=None, message_url:Optional[str]=None
):
self.content = content
self.manager = manager
Expand All @@ -106,27 +107,27 @@ def __init__(
self.lost_url = lost_url
self.message_url = message_url

def update(self, data):
def update(self, data: Dict[str, Any]) -> None:
self.content = data

def location(self):
def location(self) -> Any:
self.manager.refresh_client()
return self.content['location']

def status(self, additional=[]):
def status(self, additional:Sequence[str]=[]) -> Dict[str, Any]:
""" Returns status information for device.
This returns only a subset of possible properties.
"""
self.manager.refresh_client()
fields = ['batteryLevel', 'deviceDisplayName', 'deviceStatus', 'name']
fields += additional
properties = {}
properties: Dict[str, Any] = {}
for field in fields:
properties[field] = self.content.get(field)
return properties

def play_sound(self, subject='Find My iPhone Alert'):
def play_sound(self, subject:str='Find My iPhone Alert') -> None:
""" Send a request to the device to play a sound.
It's possible to pass a custom message by changing the `subject`.
Expand All @@ -145,9 +146,9 @@ def play_sound(self, subject='Find My iPhone Alert'):
)

def display_message(
self, subject='Find My iPhone Alert', message="This is a note",
sounds=False
):
self, subject:str='Find My iPhone Alert', message:str="This is a note",
sounds:bool=False
) -> None:
""" Send a request to the device to play a sound.
It's possible to pass a custom message by changing the `subject`.
Expand All @@ -168,10 +169,10 @@ def display_message(
)

def lost_device(
self, number,
text='This iPhone has been lost. Please call me.',
newpasscode=""
):
self, number:str,
text:str='This iPhone has been lost. Please call me.',
newpasscode:str=""
) -> None:
""" Send a request to the device to trigger 'lost mode'.
The device will show the message in `text`, and if a number has
Expand All @@ -194,29 +195,29 @@ def lost_device(
)

@property
def data(self):
def data(self) -> Dict[str, Any]:
return self.content

def __getitem__(self, key):
def __getitem__(self, key: str) -> Any:
return self.content[key]

def __getattr__(self, attr):
def __getattr__(self, attr: str) -> Any:
return getattr(self.content, attr)

def __unicode__(self):
def __unicode__(self) -> str:
display_name = self['deviceDisplayName']
name = self['name']
return '%s: %s' % (
display_name,
name,
)

def __str__(self):
def __str__(self) -> str:
as_unicode = self.__unicode__()
if sys.version_info[0] >= 3:
return as_unicode
else:
return as_unicode.encode('ascii', 'ignore')

def __repr__(self):
def __repr__(self) -> str:
return '<AppleDevice(%s)>' % str(self)

0 comments on commit 28e78a7

Please sign in to comment.