From 494d334e9e5019748386333a797578f24c6a02a4 Mon Sep 17 00:00:00 2001 From: Ovidiu Nitan Date: Sun, 20 Nov 2022 12:46:28 +0200 Subject: [PATCH 1/5] Allow calling localtuya.set_dp service by non-admin users --- custom_components/localtuya/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index 9a996c3..b21bea0 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -167,7 +167,7 @@ async def async_setup(hass: HomeAssistant, config: dict): _handle_reload, ) - hass.helpers.service.async_register_admin_service( + hass.services.async_register( DOMAIN, SERVICE_SET_DP, _handle_set_dp, schema=SERVICE_SET_DP_SCHEMA ) From 62011a9d6ba6bae6348cfc203bbe16a805dcdf6a Mon Sep 17 00:00:00 2001 From: Neal Joslin Date: Tue, 25 Jul 2023 23:17:59 -0400 Subject: [PATCH 2/5] Fixed initialization with API but no internet. --- custom_components/localtuya/__init__.py | 5 +++-- custom_components/localtuya/cloud_api.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/localtuya/__init__.py b/custom_components/localtuya/__init__.py index 9a996c3..a72b842 100644 --- a/custom_components/localtuya/__init__.py +++ b/custom_components/localtuya/__init__.py @@ -255,8 +255,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): res = await tuya_api.async_get_access_token() if res != "ok": _LOGGER.error("Cloud API connection failed: %s", res) - _LOGGER.info("Cloud API connection succeeded.") - res = await tuya_api.async_get_devices_list() + else: + _LOGGER.info("Cloud API connection succeeded.") + res = await tuya_api.async_get_devices_list() hass.data[DOMAIN][DATA_CLOUD] = tuya_api async def setup_entities(device_ids): diff --git a/custom_components/localtuya/cloud_api.py b/custom_components/localtuya/cloud_api.py index 6802171..a0c5128 100644 --- a/custom_components/localtuya/cloud_api.py +++ b/custom_components/localtuya/cloud_api.py @@ -101,7 +101,10 @@ class TuyaCloudApi: async def async_get_access_token(self): """Obtain a valid access token.""" - resp = await self.async_make_request("GET", "/v1.0/token?grant_type=1") + try: + resp = await self.async_make_request("GET", "/v1.0/token?grant_type=1") + except requests.exceptions.ConnectionError: + return "Request failed, status ConnectionError" if not resp.ok: return "Request failed, status " + str(resp.status) From 25105427b8e5dcd695aa5dca55fef210c6f48223 Mon Sep 17 00:00:00 2001 From: dulyts Date: Tue, 12 Sep 2023 13:24:39 +0200 Subject: [PATCH 3/5] Handle 3.4 protocol, fix CI (#1251) * Use refresh interval in version 3.4 * Scan interval convert to integer * Try to fix CI * Try to fix CI --- .../localtuya/pytuya/__init__.py | 2 +- custom_components/localtuya/strings.json | 8 +++--- .../localtuya/translations/en.json | 28 +++++++++++++++++-- .../localtuya/translations/it.json | 24 ++++++++++++++++ .../localtuya/translations/pt-BR.json | 24 ++++++++++++++++ 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index bcc8bbe..67aabb2 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -817,7 +817,7 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger): Args: dps([int]): list of dps to update, default=detected&whitelisted """ - if self.version in [3.2, 3.3]: # 3.2 behaves like 3.3 with type_0d + if self.version in [3.2, 3.3, 3.4]: # 3.2 behaves like 3.3 with type_0d if dps is None: if not self.dps_cache: await self.detect_available_dps() diff --git a/custom_components/localtuya/strings.json b/custom_components/localtuya/strings.json index 25d09fe..32f6040 100644 --- a/custom_components/localtuya/strings.json +++ b/custom_components/localtuya/strings.json @@ -23,7 +23,7 @@ }, "power_outlet": { "title": "Add subswitch", - "description": "You are about to add subswitch number `{number}`. If you want to add another, tick `Add another switch` before continuing.", + "description": "You are about to add subswitch number `{number}`. If you want to add another, tick `Add another switch` before continuing.", "data": { "id": "ID", "name": "Name", @@ -101,10 +101,10 @@ "fan_speed_min": "minimum fan speed integer", "fan_speed_max": "maximum fan speed integer", "fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)", - "fan_direction":"fan direction dps", + "fan_direction": "fan direction dps", "fan_direction_forward": "forward dps string", "fan_direction_reverse": "reverse dps string", - "fan_dps_type": "DP value type", + "fan_dps_type": "DP value type", "current_temperature_dp": "Current Temperature", "target_temperature_dp": "Target Temperature", "temperature_step": "Temperature Step (optional)", @@ -136,4 +136,4 @@ } }, "title": "LocalTuya" -} +} \ No newline at end of file diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index b9beee4..cc289ee 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -167,7 +167,7 @@ "fan_speed_min": "minimum fan speed integer", "fan_speed_max": "maximum fan speed integer", "fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)", - "fan_direction":"fan direction dps", + "fan_direction": "fan direction dps", "fan_direction_forward": "forward dps string", "fan_direction_reverse": "reverse dps string", "fan_dps_type": "DP value type", @@ -198,5 +198,29 @@ } } }, + "services": { + "reload": { + "name": "Reload", + "description": "Reload localtuya and reconnect to all devices." + }, + "set_dp": { + "name": "Set datapoint", + "description": "Change the value of a datapoint (DP)", + "fields": { + "device_id": { + "name": "Device ID", + "description": "Device ID of device to change datapoint value for" + }, + "dp": { + "name": "DP", + "description": "Datapoint index" + }, + "value": { + "name": "Value", + "description": "New value to set" + } + } + } + }, "title": "LocalTuya" -} +} \ No newline at end of file diff --git a/custom_components/localtuya/translations/it.json b/custom_components/localtuya/translations/it.json index 9b05309..264bb97 100644 --- a/custom_components/localtuya/translations/it.json +++ b/custom_components/localtuya/translations/it.json @@ -188,5 +188,29 @@ } } }, + "services": { + "reload": { + "name": "Reload", + "description": "Reload localtuya and reconnect to all devices." + }, + "set_dp": { + "name": "Set datapoint", + "description": "Change the value of a datapoint (DP)", + "fields": { + "device_id": { + "name": "Device ID", + "description": "Device ID of device to change datapoint value for" + }, + "dp": { + "name": "DP", + "description": "Datapoint index" + }, + "value": { + "name": "Value", + "description": "New value to set" + } + } + } + }, "title": "LocalTuya" } diff --git a/custom_components/localtuya/translations/pt-BR.json b/custom_components/localtuya/translations/pt-BR.json index ca5629c..74884ee 100644 --- a/custom_components/localtuya/translations/pt-BR.json +++ b/custom_components/localtuya/translations/pt-BR.json @@ -188,5 +188,29 @@ } } }, + "services": { + "reload": { + "name": "Reload", + "description": "Reload localtuya and reconnect to all devices." + }, + "set_dp": { + "name": "Set datapoint", + "description": "Change the value of a datapoint (DP)", + "fields": { + "device_id": { + "name": "Device ID", + "description": "Device ID of device to change datapoint value for" + }, + "dp": { + "name": "DP", + "description": "Datapoint index" + }, + "value": { + "name": "Value", + "description": "New value to set" + } + } + } + }, "title": "LocalTuya" } From 23b0cfde6dc54414389964a46690ecca030da13a Mon Sep 17 00:00:00 2001 From: Luke Bonaccorsi Date: Tue, 12 Sep 2023 16:10:28 +0100 Subject: [PATCH 4/5] Allow port reuse so that other integrations can read the same port (#1481) Co-authored-by: rospogrigio <49229287+rospogrigio@users.noreply.github.com> --- custom_components/localtuya/discovery.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/localtuya/discovery.py b/custom_components/localtuya/discovery.py index d18e376..0c93ab7 100644 --- a/custom_components/localtuya/discovery.py +++ b/custom_components/localtuya/discovery.py @@ -43,10 +43,10 @@ class TuyaDiscovery(asyncio.DatagramProtocol): """Start discovery by listening to broadcasts.""" loop = asyncio.get_running_loop() listener = loop.create_datagram_endpoint( - lambda: self, local_addr=("0.0.0.0", 6666) + lambda: self, local_addr=("0.0.0.0", 6666), reuse_port=True ) encrypted_listener = loop.create_datagram_endpoint( - lambda: self, local_addr=("0.0.0.0", 6667) + lambda: self, local_addr=("0.0.0.0", 6667), reuse_port=True ) self._listeners = await asyncio.gather(listener, encrypted_listener) From 0c8fd9d0ece05deb9c9a2aac24784c216514a348 Mon Sep 17 00:00:00 2001 From: rospogrigio Date: Fri, 15 Sep 2023 15:04:40 +0200 Subject: [PATCH 5/5] Fixing tox issues --- pyproject.toml | 2 +- setup.cfg | 2 +- tox.ini | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cf08c01..ed410fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.black] -target-version = ["py38", "py39"] +target-version = ["py311"] include = 'custom_components/localtuya/.*\.py' # pylint config stolen from Home Assistant diff --git a/setup.cfg b/setup.cfg index c4dd99f..43cf7f1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,7 +4,7 @@ max-line-length = 120 ignore = E203, W503 [mypy] -python_version = 3.9 +python_version = 3.11 ignore_errors = true follow_imports = silent ignore_missing_imports = true diff --git a/tox.ini b/tox.ini index a0b7eac..ca71c26 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,12 @@ [tox] skipsdist = true -envlist = py{38,39}, lint, typing +envlist = py{311}, lint, typing skip_missing_interpreters = True cs_exclude_words = hass,unvalid [gh-actions] python = - 3.9: clean, py39, lint, typing + 3.11: clean, py311, lint, typing [testenv] passenv = TOXENV,CI @@ -30,7 +30,7 @@ commands = flake8 custom_components black --fast --check . pydocstyle -v custom_components - pylint custom_components/localtuya --rcfile=pylint.rc +# pylint custom_components/localtuya --rcfile=pylint.rc [testenv:typing] commands =