-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from EnzoD86/2024.8.2
2024.8.2
- Loading branch information
Showing
11 changed files
with
300 additions
and
333 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
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 |
---|---|---|
@@ -1,61 +1,73 @@ | ||
import logging | ||
from .const import DOMAIN, TUYA_API_CLIENT | ||
|
||
|
||
_LOGGER = logging.getLogger(__package__) | ||
|
||
|
||
class TuyaAPI: | ||
def __init__(self, hass, infrared_id, climate_id): | ||
def __init__(self, hass, client): | ||
self._hass = hass | ||
self._client = hass.data.get(DOMAIN).get(TUYA_API_CLIENT) | ||
self._infrared_id = infrared_id | ||
self._climate_id = climate_id | ||
self._client = client | ||
|
||
async def async_fetch_status(self): | ||
url = f"/v2.0/infrareds/{self._infrared_id}/remotes/{self._climate_id}/ac/status" | ||
async def async_fetch_all_data(self, climate_ids): | ||
try: | ||
url = f"/v1.0/cloud/rc/infrared/ac/status/batch?device_ids={",".join(climate_ids)}" | ||
_LOGGER.debug(f"API fetch_all_data url: {url}") | ||
result = await self._hass.async_add_executor_job(self._client.get, url) | ||
_LOGGER.debug(f"Climate {self._climate_id} fetch status response: {str(result)}") | ||
_LOGGER.debug(f"API fetch_all_data response: {str(result)}") | ||
if result.get("success"): | ||
return result.get("result") | ||
raise Exception(TuyaError("", result).to_dict()) | ||
raise Exception(TuyaDetails(url, "", result).to_dict()) | ||
except Exception as e: | ||
_LOGGER.error(f"Error fetching status for climate {self._climate_id}: {e}") | ||
return None | ||
_LOGGER.error(f"Error fetching all data for climates {str(climate_ids)}: {e}") | ||
raise Exception(e) | ||
|
||
async def async_fetch_data(self, infrared_id, climate_id): | ||
try: | ||
url = f"/v2.0/infrareds/{infrared_id}/remotes/{climate_id}/ac/status" | ||
_LOGGER.debug(f"API fetch_data url: {url}") | ||
result = await self._hass.async_add_executor_job(self._client.get, url) | ||
_LOGGER.debug(f"API fetch_data response: {str(result)}") | ||
if result.get("success"): | ||
return result.get("result") | ||
raise Exception(TuyaDetails(url, "", result).to_dict()) | ||
except Exception as e: | ||
_LOGGER.error(f"Error fetching data for climate {climate_id}: {e}") | ||
raise Exception(e) | ||
|
||
async def async_send_command(self, code, value): | ||
url = f"/v2.0/infrareds/{self._infrared_id}/air-conditioners/{self._climate_id}/command" | ||
command = { "code": code, "value": value } | ||
async def async_send_command(self, infrared_id, climate_id, code, value): | ||
try: | ||
_LOGGER.debug(f"Climate {self._climate_id} send command request: {str(command)}") | ||
url = f"/v2.0/infrareds/{infrared_id}/air-conditioners/{climate_id}/command" | ||
_LOGGER.debug(f"API send_command url: {url}") | ||
command = { "code": code, "value": value } | ||
_LOGGER.debug(f"API send_command request: {command}") | ||
result = await self._hass.async_add_executor_job(self._client.post, url, command) | ||
_LOGGER.debug(f"Climate {self._climate_id} send command response: {str(result)}") | ||
_LOGGER.debug(f"API send_command response: {str(result)}") | ||
if not result.get("success"): | ||
raise Exception(TuyaError(command, result).to_dict()) | ||
raise Exception(TuyaDetails(url, command, result).to_dict()) | ||
except Exception as e: | ||
_LOGGER.error(f"Error sending command to climate {self._climate_id}: {e}") | ||
_LOGGER.error(f"Error sending command to climate {climate_id}: {e}") | ||
raise Exception(e) | ||
|
||
async def async_send_multiple_command(self, power, mode, temp, wind): | ||
url = f"/v2.0/infrareds/{self._infrared_id}/air-conditioners/{self._climate_id}/scenes/command" | ||
command = { "power": power, "mode": mode, "temp": temp, "wind": wind } | ||
async def async_send_multiple_command(self, infrared_id, climate_id, power, mode, temp, wind): | ||
try: | ||
_LOGGER.debug(f"Climate {self._climate_id} send multiple command request: {str(command)}") | ||
url = f"/v2.0/infrareds/{infrared_id}/air-conditioners/{climate_id}/scenes/command" | ||
_LOGGER.debug(f"API send_multiple_command url: {url}") | ||
command = { "power": power, "mode": mode, "temp": temp, "wind": wind } | ||
_LOGGER.debug(f"API send_multiple_command request: {command}") | ||
result = await self._hass.async_add_executor_job(self._client.post, url, command) | ||
_LOGGER.debug(f"Climate {self._climate_id} send multiple command response: {str(result)}") | ||
_LOGGER.debug(f"API send_multiple_command response: {str(result)}") | ||
if not result.get("success"): | ||
raise Exception(TuyaError(command, result).to_dict()) | ||
return result | ||
raise Exception(TuyaDetails(url, command, result).to_dict()) | ||
except Exception as e: | ||
_LOGGER.error(f"Error sending multiple command to climate {self._climate_id}: {e}") | ||
_LOGGER.error(f"Error sending multiple command to climate {climate_id}: {e}") | ||
raise Exception(e) | ||
|
||
|
||
class TuyaError(object): | ||
def __init__(self, request, response): | ||
class TuyaDetails(object): | ||
def __init__(self, url, request, response): | ||
self.url = url | ||
self.request = request | ||
self.response = response | ||
|
||
def to_dict(self): | ||
return vars(self) | ||
return vars(self) |
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.