Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
zweckj committed Oct 22, 2023
1 parent d311f60 commit 390c8cf
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 89 deletions.
2 changes: 1 addition & 1 deletion pytedee_async/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Import modules for pytedee_async."""
from .tedee_client import *
from .exception import *
from .tedee_client import *
47 changes: 28 additions & 19 deletions pytedee_async/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Helper functions for pytedee_async."""
from typing import Any, Mapping

import aiohttp

from .const import API_URL_DEVICE, TIMEOUT
from .exception import TedeeAuthException, TedeeClientException, TedeeRateLimitException


async def is_personal_key_valid(personal_key, timeout=TIMEOUT) -> bool:
async def is_personal_key_valid(personal_key: str, timeout: int = TIMEOUT) -> bool:
"""Check if personal key is valid."""
try:
async with aiohttp.ClientSession(
headers={
Expand All @@ -15,43 +20,47 @@ async def is_personal_key_valid(personal_key, timeout=TIMEOUT) -> bool:
async with session.get(API_URL_DEVICE) as response:
if response.status == 200:
return True
else:
return False
except Exception:
return False
except aiohttp.ClientError:
return False


async def http_request(
url: str, http_method: str, headers, timeout, json_data: dict = None
):
url: str,
http_method: str,
headers: Mapping[str, str] | None,
timeout: int,
json_data: Any = None,
) -> Any:
"""HTTP request wrapper."""
async with aiohttp.ClientSession(
headers=headers, timeout=aiohttp.ClientTimeout(total=timeout)
) as session:
if http_method == "GET":
async with session.get(url) as response:
return await handle_response(response)
elif http_method == "POST":
if http_method == "POST":
async with session.post(url, json=json_data) as response:
return await handle_response(response)
elif http_method == "PUT":
if http_method == "PUT":
async with session.put(url, json=json_data) as response:
return await handle_response(response)
elif http_method == "DELETE":
if http_method == "DELETE":
async with session.delete(url) as response:
return await handle_response(response)
else:
raise ValueError(f"Unsupported HTTP method: {http_method}")
raise ValueError(f"Unsupported HTTP method: {http_method}")


async def handle_response(response):
async def handle_response(response) -> Any:
"""Handle HTTP response."""
status_code = response.status
if status_code == 200 or status_code == 202 or status_code == 204:
if status_code in (200, 202, 204):
return await response.json()
elif response.status == 401:
if response.status == 401:
raise TedeeAuthException("Authentication failed.")
elif response.status == 429:
if response.status == 429:
raise TedeeRateLimitException("Tedee API Rate Limit.")
else:
raise TedeeClientException(
f"Error during HTTP request. Status code {response.status}"
)

raise TedeeClientException(
f"Error during HTTP request. Status code {response.status}"
)
61 changes: 41 additions & 20 deletions pytedee_async/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
"""Tedee Lock Object."""

from enum import IntEnum


class TedeeLockState(IntEnum):
"""Tedee Lock State."""

UNKNOWN = 0
UNLOCKED = 2
HALF_OPEN = 3
UNLOCKING = 4
LOCKING = 5
LOCKED = 6
PULLED = 7


class TedeeLock:
"""Tedee Lock."""
Expand All @@ -10,13 +24,13 @@ def __init__(
lock_id: int,
lock_type: int,
state: int = 0,
battery_level: int = None,
battery_level: int | None = None,
is_connected: bool = False,
is_charging: bool = False,
state_change_result: int = 0,
is_enabled_pullspring: bool = False,
duration_pullspring: int = 0,
):
) -> None:
"""Initialize a new lock."""
self._lock_name = lock_name
self._lock_id = lock_id
Expand All @@ -31,14 +45,17 @@ def __init__(

@property
def lock_name(self) -> str:
"""Return the name of the lock."""
return self._lock_name

@property
def lock_id(self) -> int:
"""Return the id of the lock."""
return self._lock_id

@property
def lock_type(self) -> str:
"""Return the type of the lock."""
if self._lock_type == 2:
return "Tedee PRO"
elif self._lock_type == 4:
Expand All @@ -48,41 +65,40 @@ def lock_type(self) -> str:

@property
def is_state_locked(self) -> bool:
return self._state == 6
"""Return true if the lock is locked."""
return self._state == TedeeLockState.LOCKED

@property
def is_state_unlocked(self) -> bool:
return self._state == 2
"""Return true if the lock is unlocked."""
return self._state == TedeeLockState.UNLOCKED

@property
def is_state_jammed(self) -> bool:
"""Return true if the lock is jammed."""
return self._state_change_result == 1

@property
def state(self) -> int:
# Unknown = 0
# Unlocked = 2
# Half Open = 3
# Unlocking = 4
# Locking = 5
# Locked = 6
# Pull = 7
return self._state
def state(self) -> TedeeLockState:
"""Return the state of the lock."""
return TedeeLockState(self._state)

@state.setter
def state(self, status):
def state(self, status: int):
self._state = status

@property
def state_change_result(self) -> int:
"""Return the state change result of the lock."""
return self._state_change_result

@state_change_result.setter
def state_change_result(self, result):
def state_change_result(self, result: int):
self._state_change_result = result

@property
def battery_level(self) -> int:
def battery_level(self) -> int | None:
"""Return the battery level of the lock."""
return self._battery_level

@battery_level.setter
Expand All @@ -91,6 +107,7 @@ def battery_level(self, level):

@property
def is_connected(self) -> bool:
"""Return true if the lock is connected."""
return self._is_connected

@is_connected.setter
Expand All @@ -99,29 +116,33 @@ def is_connected(self, connected):

@property
def is_charging(self) -> bool:
"""Return true if the lock is charging."""
return self._is_charging

@is_charging.setter
def is_charging(self, value):
def is_charging(self, value: bool):
self._is_charging = value

@property
def is_enabled_pullspring(self) -> bool:
return self._is_enabled_pullspring
"""Return true if the lock is charging."""
return bool(self._is_enabled_pullspring)

@is_enabled_pullspring.setter
def is_enabled_pullspring(self, value):
def is_enabled_pullspring(self, value: bool):
self._is_enabled_pullspring = value

@property
def duration_pullspring(self) -> int:
"""Return the duration of the pullspring."""
return self._duration_pullspring

@duration_pullspring.setter
def duration_pullspring(self, duration):
def duration_pullspring(self, duration: int):
self._duration_pullspring = duration

def to_dict(self):
"""Return a dict representation of the lock."""
return {
"lock_name": self._lock_name,
"lock_id": self._lock_id,
Expand Down
Empty file added pytedee_async/py.typed
Empty file.
Loading

0 comments on commit 390c8cf

Please sign in to comment.