Skip to content

Commit

Permalink
Add Python 3.6-style variable type annotations (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya authored Sep 3, 2019
1 parent 21ee165 commit 9a8dc99
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
exclude_lines =
# Don't complain if tests don't hit defensive assertion code:
raise NotImplementedError
# Don't include lines that are specifically in place for type-checking:
TYPE_CHECKING

[run]
source = simplipy
Expand Down
47 changes: 23 additions & 24 deletions simplipy/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Define a SimpliSafe account."""
# pylint: disable=protected-access,too-many-instance-attributes

# pylint: disable=protected-access
import logging
from datetime import datetime, timedelta
from typing import Dict, Optional, Type, TypeVar
from uuid import uuid4
from uuid import UUID, uuid4

from aiohttp import BasicAuth, ClientSession
from aiohttp.client_exceptions import ClientError
Expand All @@ -14,31 +13,31 @@

_LOGGER = logging.getLogger(__name__)

DEFAULT_USER_AGENT = "SimpliSafe/2105 CFNetwork/902.2 Darwin/17.7.0"
DEFAULT_AUTH_USERNAME = "{0}.2074.0.0.com.simplisafe.mobile"
DEFAULT_USER_AGENT: str = "SimpliSafe/2105 CFNetwork/902.2 Darwin/17.7.0"
DEFAULT_AUTH_USERNAME: str = "{0}.2074.0.0.com.simplisafe.mobile"

SYSTEM_MAP = {2: SystemV2, 3: SystemV3}
SYSTEM_MAP: Dict[int, Type[System]] = {2: SystemV2, 3: SystemV3}

URL_HOSTNAME = "api.simplisafe.com"
URL_BASE = f"https://{URL_HOSTNAME}/v1"
URL_HOSTNAME: str = "api.simplisafe.com"
URL_BASE: str = f"https://{URL_HOSTNAME}/v1"

ApiType = TypeVar("ApiType", bound="API")


class API:
class API: # pylint: disable=too-many-instance-attributes
"""Define an API object to interact with the SimpliSafe cloud."""

def __init__(self, websession: ClientSession) -> None:
"""Initialize."""
self._access_token = ""
self._access_token_expire = None # type: Optional[datetime]
self._actively_refreshing = False
self._email = None # type: Optional[str]
self._refresh_token = ""
self._uuid = uuid4()
self._websession = websession
self.refresh_token_dirty = False
self.user_id = None
self._access_token: str = ""
self._access_token_expire: Optional[datetime] = None
self._actively_refreshing: bool = False
self._email: Optional[str] = None
self._refresh_token: str = ""
self._uuid: UUID = uuid4()
self._websession: ClientSession = websession
self.refresh_token_dirty: bool = False
self.user_id: Optional[str] = None

@property
def refresh_token(self) -> str:
Expand Down Expand Up @@ -82,7 +81,7 @@ async def login_via_token(

async def _authenticate(self, payload_data: dict) -> None:
"""Request token data and parse it."""
token_resp = await self.request(
token_resp: dict = await self.request(
"post",
"api/token",
data=payload_data,
Expand All @@ -99,7 +98,7 @@ async def _authenticate(self, payload_data: dict) -> None:
)
self.refresh_token = token_resp["refresh_token"]

auth_check_resp = await self.request("get", "api/authCheck")
auth_check_resp: dict = await self.request("get", "api/authCheck")
self.user_id = auth_check_resp["userId"]

async def _refresh_access_token(self, refresh_token: str) -> None:
Expand All @@ -116,9 +115,9 @@ async def _refresh_access_token(self, refresh_token: str) -> None:

async def get_systems(self) -> Dict[str, System]:
"""Get systems associated to this account."""
subscription_resp = await self.get_subscription_data()
subscription_resp: dict = await self.get_subscription_data()

systems = {}
systems: Dict[str, System] = {}
for system_data in subscription_resp["subscriptions"]:
version = system_data["location"]["system"]["version"]
system_class = SYSTEM_MAP[version]
Expand All @@ -130,7 +129,7 @@ async def get_systems(self) -> Dict[str, System]:

async def get_subscription_data(self) -> dict:
"""Get the latest location-level data."""
subscription_resp = await self.request(
subscription_resp: dict = await self.request(
"get", f"users/{self.user_id}/subscriptions", params={"activeOnly": "true"}
)

Expand Down Expand Up @@ -158,7 +157,7 @@ async def request(
self._actively_refreshing = True
await self._refresh_access_token(self._refresh_token)

url = f"{URL_BASE}/{endpoint}"
url: str = f"{URL_BASE}/{endpoint}"

if not headers:
headers = {}
Expand Down
4 changes: 2 additions & 2 deletions simplipy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class Sensor:

def __init__(self, sensor_data: dict) -> None:
"""Initialize."""
self.sensor_data = sensor_data
self.sensor_data: dict = sensor_data

try:
self._type = SensorTypes(sensor_data["type"])
self._type: SensorTypes = SensorTypes(sensor_data["type"])
except ValueError:
_LOGGER.error("Unknown sensor type: %s", self.sensor_data["type"])
self._type = SensorTypes.unknown
Expand Down
Loading

0 comments on commit 9a8dc99

Please sign in to comment.