Skip to content

Commit

Permalink
Merge pull request #13 from Jhonattan-Souza/master
Browse files Browse the repository at this point in the history
Adds: Infrared devices query to use in homeassistant
  • Loading branch information
tsutsuku authored Sep 2, 2021
2 parents 1743776 + dba5f79 commit 0dc507b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions tuya_iot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 52 additions & 0 deletions tuya_iot/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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})
46 changes: 46 additions & 0 deletions tuya_iot/infrared.py
Original file line number Diff line number Diff line change
@@ -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



0 comments on commit 0dc507b

Please sign in to comment.