Skip to content

Commit

Permalink
improved typing
Browse files Browse the repository at this point in the history
  • Loading branch information
jafar-atili committed Sep 21, 2022
1 parent 20dd614 commit 8f19940
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyswitchbee"
version = "1.4.8"
version = "1.5.0"
description = "SwitchBee Python Integration."
readme = "README.md"
authors = [{ name = "Jafar Atili", email = "[email protected]" }]
Expand Down
56 changes: 18 additions & 38 deletions src/switchbee/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from datetime import timedelta
from json import JSONDecodeError
from logging import getLogger
from typing import List, Union
from typing import Any, List, Union

from aiohttp import ClientSession

Expand All @@ -26,6 +26,7 @@
SwitchBeeTimerSwitch,
SwitchBeeTwoWay,
SwitchBeeSomfy,
SwitchBeeBaseDevice,
)

from .utils import timestamp_now
Expand Down Expand Up @@ -66,64 +67,45 @@ def __init__(
self._user: str = user
self._password: str = password
self._session: ClientSession = websession
self._token: str = None
self._token: str | None = None
self._token_expiration: int = 0
self._login_count: int = -1 # we don't count the first login
self._mac = str
self._version = str
self._name = str
self._last_conf_change = int
self._mac: str | None = None
self._version: str | None = None
self._name: str | None = None
self._last_conf_change: int = 0
self._devices_map: dict[
int,
Union[
SwitchBeeDimmer,
SwitchBeeSwitch,
SwitchBeeShutter,
SwitchBeeScenario,
SwitchBeeTimerSwitch,
SwitchBeeGroupSwitch,
SwitchBeeThermostat,
],
SwitchBeeBaseDevice,
] = {}

self._modules_map: dict[int, set] = {}

@property
def name(self) -> str:
def name(self) -> str | None:
return self._name

@property
def version(self) -> str:
def version(self) -> str | None:
return self._version

@property
def mac(self) -> str:
def mac(self) -> str | None:
return self._mac

@property
def devices(self) -> dict:
def devices(self) -> dict[int, SwitchBeeBaseDevice]:
return self._devices_map

@property
def last_conf_change(self) -> str:
def last_conf_change(self) -> int:
return self._last_conf_change

@property
def devices_list(
self,
) -> List[
Union[
SwitchBeeSwitch,
SwitchBeeDimmer,
SwitchBeeShutter,
SwitchBeeTimerSwitch,
SwitchBeeScenario,
SwitchBeeRollingScenario,
SwitchBeeGroupSwitch,
SwitchBeeThermostat,
]
]:
return self._devices_map.values()
) -> List[SwitchBeeBaseDevice]:
return list(self._devices_map.values())

@property
def reconnect_count(self) -> int:
Expand Down Expand Up @@ -189,7 +171,7 @@ async def _post(self, body: dict) -> dict:
except ClientConnectorError as exp:
raise SwitchBeeError("Failed to communicate with the Central Unit") from exp

async def _send_request(self, command: ApiCommand, params: dict = {}) -> dict:
async def _send_request(self, command: str, params: Any = {}) -> dict:

return await self._post(
{
Expand All @@ -199,7 +181,7 @@ async def _send_request(self, command: ApiCommand, params: dict = {}) -> dict:
}
)

async def _login(self) -> str:
async def _login(self) -> None:
try:
resp = await self._post(
{
Expand All @@ -223,8 +205,6 @@ async def _login(self) -> str:
# self._token_expiration = resp[ApiAttribute.DATA][ApiAttribute.EXPIRATION]
self._token_expiration = timestamp_now() + TOKEN_EXPIRATION

return self._token

async def get_configuration(self):
await self.login_if_needed()
return await self._send_request(ApiCommand.GET_CONF)
Expand Down Expand Up @@ -253,7 +233,7 @@ async def get_stats(self):

async def fetch_configuration(
self,
include: list[DeviceType] = [],
include: list[DeviceType] | None = [],
):
await self.login_if_needed()
data = await self.get_configuration()
Expand Down
2 changes: 1 addition & 1 deletion src/switchbee/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ThermostatMode:

class ThermostatFanSpeed:
LOW = "LOW"
MEDIUM = "MEDIUM"
MEDIUM = "MED"
HIGH = "HIGH"
AUTO = "AUTO"

Expand Down
28 changes: 14 additions & 14 deletions src/switchbee/device/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def __hash__(self):

@dataclass
class SwitchBeeBaseSwitch(ABC):
_state: ApiStateCommand = field(init=False, default=None)
_state: str | None = field(init=False, default=None)

@property
def state(self) -> ApiStateCommand:
def state(self) -> str | None:
return self._state

@state.setter
Expand All @@ -106,10 +106,10 @@ def state(self, value: str) -> None:

@dataclass
class SwitchBeeBaseShutter(ABC):
_position: int = field(init=False, repr=False, default=None)
_position: int | None = field(init=False, repr=False, default=None)

@property
def position(self) -> int:
def position(self) -> int | None:
return self._position

@position.setter
Expand All @@ -134,10 +134,9 @@ def brightness(self) -> int:

@brightness.setter
def brightness(self, value: Union[str, int]) -> None:

if value == ApiStateCommand.OFF or value == 0:
if value == ApiStateCommand.OFF:
self._brightness = 0
elif value == ApiStateCommand.ON or value == 100:
elif value == ApiStateCommand.ON:
self._brightness = 100
else:
self._brightness = int(value)
Expand All @@ -146,18 +145,14 @@ def brightness(self, value: Union[str, int]) -> None:
@dataclass
class SwitchBeeBaseTimer(ABC):
_minutes_left: int = field(init=False)
_state: ApiStateCommand = field(init=False)
_state: str | int = field(init=False)

@property
def state(self) -> ApiStateCommand:
def state(self) -> str | int:
return self._state

@property
def minutes_left(self) -> int:
return self._minutes_left

@state.setter
def state(self, value: Union[str, int]) -> None:
def state(self, value: str | int) -> None:

if value:
if value == ApiStateCommand.OFF:
Expand All @@ -167,6 +162,11 @@ def state(self, value: Union[str, int]) -> None:
self._minutes_left = int(value)
self._state = ApiStateCommand.ON

@property
def minutes_left(self) -> int:
return self._minutes_left



@dataclass
class SwitchBeeBaseThermostat(ABC):
Expand Down
Empty file added src/switchbee/py.typed
Empty file.

0 comments on commit 8f19940

Please sign in to comment.