forked from K4CZP3R/tapo-p100-python
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for s200 buttons (#139)
* support for s200 buttons * bump to 3.12.0
- Loading branch information
1 parent
8c6f648
commit b0757fa
Showing
13 changed files
with
231 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
from plugp100.api import * | ||
from plugp100.common import * | ||
|
||
__version__ = "3.11.0" | ||
__version__ = "3.12.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Union, Optional, Any | ||
|
||
from plugp100.api.hub.hub_device import HubDevice | ||
from plugp100.api.hub.s200b_device import S200ButtonDevice | ||
from plugp100.api.hub.switch_child_device import SwitchChildDevice | ||
from plugp100.api.hub.t100_device import T100MotionSensor | ||
from plugp100.api.hub.t110_device import T110SmartDoor | ||
from plugp100.api.hub.t31x_device import T31Device | ||
|
||
HubChildDevice = Union[ | ||
T100MotionSensor, T110SmartDoor, T31Device, S200ButtonDevice, SwitchChildDevice | ||
] | ||
|
||
|
||
def create_hub_child_device( | ||
hub: HubDevice, child_state: dict[str, Any] | ||
) -> Optional[HubChildDevice]: | ||
model = child_state.get("model").lower() | ||
device_id = child_state.get("device_id") | ||
if "t31" in model: | ||
return T31Device(hub, device_id) | ||
elif "t110" in model: | ||
return T110SmartDoor(hub, device_id) | ||
elif "s200" in model: | ||
return S200ButtonDevice(hub, device_id) | ||
elif "t100" in model: | ||
return T100MotionSensor(hub, device_id) | ||
elif any(supported in model for supported in ["s200", "s210"]): | ||
return SwitchChildDevice(hub, device_id) | ||
else: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from plugp100.api.hub.hub_device import HubDevice | ||
|
||
from plugp100.common.functional.tri import Try | ||
from plugp100.common.utils.json_utils import dataclass_encode_json | ||
from plugp100.requests.set_device_info.set_plug_info_params import SetPlugInfoParams | ||
from plugp100.requests.tapo_request import TapoRequest | ||
from plugp100.responses.hub_childs.switch_child_device_state import SwitchChildDeviceState | ||
|
||
|
||
class SwitchChildDevice: | ||
def __init__(self, hub: HubDevice, device_id: str): | ||
self._hub = hub | ||
self._device_id = device_id | ||
|
||
async def get_device_info(self) -> Try[SwitchChildDeviceState]: | ||
""" | ||
The function `get_device_info` sends a request to retrieve device information and returns either the device state or | ||
an exception. | ||
@ return: an instance of the `Either` class, which can hold either an instance of `S200BDeviceState` or an instance | ||
of `Exception`. | ||
""" | ||
return ( | ||
await self._hub.control_child(self._device_id, TapoRequest.get_device_info()) | ||
).flat_map(SwitchChildDeviceState.try_from_json) | ||
|
||
async def on(self) -> Try[bool]: | ||
request = TapoRequest.set_device_info( | ||
dataclass_encode_json(SetPlugInfoParams(device_on=True)) | ||
) | ||
return (await self._hub.control_child(self._device_id, request)).map( | ||
lambda _: True | ||
) | ||
|
||
async def off(self) -> Try[bool]: | ||
request = TapoRequest.set_device_info( | ||
dataclass_encode_json(SetPlugInfoParams(device_on=False)) | ||
) | ||
return (await self._hub.control_child(self._device_id, request)).map( | ||
lambda _: True | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import base64 | ||
from typing import Any | ||
|
||
import semantic_version | ||
|
||
from plugp100.common.functional.tri import Try | ||
|
||
|
||
class HubChildBaseInfo: | ||
hardware_version: str | ||
firmware_version: str | ||
device_id: str | ||
parent_device_id: str | ||
mac: str | ||
type: str | ||
model: str | ||
status: str | ||
rssi: int | ||
signal_level: int | ||
at_low_battery: bool | ||
nickname: str | ||
last_onboarding_timestamp: int | ||
|
||
@staticmethod | ||
def from_json(kwargs: dict[str, Any]) -> Try["HubChildBaseInfo"]: | ||
return Try.of(lambda: HubChildBaseInfo(**kwargs)) | ||
|
||
def __init__(self, **kwargs): | ||
self.firmware_version = kwargs["fw_ver"] | ||
self.hardware_version = kwargs["hw_ver"] | ||
self.device_id = kwargs["device_id"] | ||
self.parent_device_id = kwargs["parent_device_id"] | ||
self.mac = kwargs["mac"] | ||
self.type = kwargs["type"] | ||
self.model = kwargs["model"] | ||
self.status = kwargs.get("status", False) | ||
self.rssi = kwargs.get("rssi", 0) | ||
self.signal_level = kwargs.get("signal_level", 0) | ||
self.at_low_battery = kwargs.get("at_low_battery", False) | ||
self.nickname = base64.b64decode(kwargs["nickname"]).decode("UTF-8") | ||
self.last_onboarding_timestamp = kwargs.get("lastOnboardingTimestamp", 0) | ||
|
||
def get_semantic_firmware_version(self) -> semantic_version.Version: | ||
pieces = self.firmware_version.split("Build") | ||
try: | ||
if len(pieces) > 0: | ||
return semantic_version.Version(pieces[0].strip()) | ||
else: | ||
return semantic_version.Version("0.0.0") | ||
except ValueError: | ||
return semantic_version.Version("0.0.0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
plugp100/responses/hub_childs/switch_child_device_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from dataclasses import dataclass | ||
from typing import Any | ||
|
||
from plugp100.common.functional.tri import Try | ||
from plugp100.responses.hub_childs.hub_child_base_info import HubChildBaseInfo | ||
|
||
|
||
@dataclass | ||
class SwitchChildDeviceState: | ||
base_info: HubChildBaseInfo | ||
device_on: bool | ||
led_off: int | ||
|
||
@staticmethod | ||
def try_from_json(kwargs: dict[str, Any]) -> Try["SwitchChildDeviceState"]: | ||
return HubChildBaseInfo.from_json(kwargs).flat_map( | ||
lambda base_info: Try.of( | ||
lambda: SwitchChildDeviceState( | ||
base_info=base_info, | ||
device_on=kwargs["device_on"], | ||
led_off=kwargs.get("led_off", 0), | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.