diff --git a/tuya_iot/__init__.py b/tuya_iot/__init__.py index 9e88796..f01adea 100644 --- a/tuya_iot/__init__.py +++ b/tuya_iot/__init__.py @@ -6,6 +6,7 @@ from .device import TuyaDeviceManager, TuyaDevice, TuyaDeviceListener from .tuya_enums import AuthType, TuyaCloudOpenAPIEndpoint from .home import TuyaHomeManager, TuyaScene +from .infrared import TuyaRemote from .openlogging import TUYA_LOGGER from .version import VERSION diff --git a/tuya_iot/home.py b/tuya_iot/home.py index 6f87c41..a0c21c5 100644 --- a/tuya_iot/home.py +++ b/tuya_iot/home.py @@ -10,6 +10,7 @@ from .tuya_enums import AuthType from .asset import TuyaAssetManager from .device import TuyaDeviceManager +from .infrared import TuyaRemote, TuyaRemoteDevice, TuyaRemoteDeviceKey class TuyaScene(SimpleNamespace): @@ -106,3 +107,54 @@ def trigger_scene(self, return self.api.post(f"/v1.0/homes/{home_id}/scenes/{scene_id}/trigger") return dict() + + def query_infrared_devices(self) -> list: + """Query infrared devices, only in SMART_HOME project type.""" + if self.api.auth_type == AuthType.CUSTOM: + return [] + + remote_ids = [] + for (device_id, device) in self.device_manager.device_map.items(): + if device.category == "qt": + remote_ids.append(device_id) + + remotes = [] + for remote_id in remote_ids: + remotes_response = self.api.get(f"/v1.0/infrareds/{remote_id}/remotes") + if not remotes_response.get("success", False): + continue + + remote_device_response = remotes_response.get("result") + + remote_devices = [] + for remote_device in remote_device_response: + # Air conditioners not implemented yet ( but it will be in the future ) + if remote_device["category_id"] == "5": + continue + + keys_response = self.api.get(f"/v1.0/infrareds/{remote_id}/remotes/{remote_device['remote_id']}/keys") + + if not keys_response.get("success", False): + continue + + keys_result = keys_response.get("result") + key_values = keys_result.get("key_list") + + tuya_remote_device_keys = [] + for tuya_key in key_values: + tuya_remote_device_keys.append(TuyaRemoteDeviceKey(tuya_key["key"], tuya_key["key_id"], + tuya_key["key_name"], tuya_key["standard_key"])) + + remote_devices.append(TuyaRemoteDevice(remote_device, tuya_remote_device_keys)) + + if remote_devices: + remotes.append(TuyaRemote(remote_id, remote_devices)) + + return remotes + + def trigger_infrared_commands(self, remote_id, device_id, key) -> None: + """Send infrared commands, only in SMART_HOME project type.""" + if self.api.auth_type == AuthType.CUSTOM: + return [] + + self.api.post("/v1.0/infrareds/{}/remotes/{}/command".format(remote_id, device_id), {"key": key}) diff --git a/tuya_iot/infrared.py b/tuya_iot/infrared.py new file mode 100644 index 0000000..efaefaa --- /dev/null +++ b/tuya_iot/infrared.py @@ -0,0 +1,46 @@ +from types import SimpleNamespace + + +class TuyaRemoteDeviceKey(SimpleNamespace): + key: str + key_id: int + key_name: str + standard_key: bool + + def __init__(self, key: str, key_id: int, key_name: str, standard_key: bool): + self.key = key + self.key_id = key_id + self.key_name = key_name + self.standard_key = standard_key + + +class TuyaRemoteDevice(SimpleNamespace): + remote_name = str + category_id = str + brand_id = str + remote_index = str + remote_id = str + keys: list + + def __init__(self, props: dict, keys: list): + self.map(props) + self.keys = keys + + def map(self, props: dict): + self.remote_name = props["remote_name"] + self.category_id = props["category_id"] + self.brand_id = props["brand_id"] + self.remote_index = props["remote_index"] + self.remote_id = props["remote_id"] + + +class TuyaRemote(SimpleNamespace): + remote_id: str + remote_devices = list + + def __init__(self, remote_id: str, remote_devices: list): + self.remote_id = remote_id + self.remote_devices = remote_devices + + +