diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index 40ca8d9..6725fb4 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -14,8 +14,8 @@ jobs: platform: - ubuntu-latest python-version: - - 3.7 - - 3.8 + - 3.9 + - 3.10 steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} diff --git a/custom_components/localtuya/common.py b/custom_components/localtuya/common.py index 9e04579..36d9104 100644 --- a/custom_components/localtuya/common.py +++ b/custom_components/localtuya/common.py @@ -81,6 +81,7 @@ async def async_setup_entry( ] if entities_to_setup: + tuyainterface = hass.data[DOMAIN][TUYA_DEVICES][dev_id] dps_config_fields = list(get_dps_for_platform(flow_schema)) @@ -213,7 +214,7 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self._interface.start_heartbeat() self.status_updated(status) - except Exception as ex: # pylint: disable=broad-except + except Exception as ex: if (self._default_reset_dpids is not None) and ( len(self._default_reset_dpids) > 0 ): @@ -232,10 +233,10 @@ class TuyaDevice(pytuya.TuyaListener, pytuya.ContextualLogger): self._interface.start_heartbeat() self.status_updated(status) else: - self.error(f"Initial state update failed, giving up: %r", ex) + self.error("Initial state update failed, giving up: %r", ex) # return except (UnicodeDecodeError, json.decoder.JSONDecodeError) as ex: - self.exception(f"Initial state update failed, trying key update") + self.warning("Initial state update failed (%s), trying key update", ex) await self.update_local_key() if self._interface is not None: diff --git a/custom_components/localtuya/config_flow.py b/custom_components/localtuya/config_flow.py index ebde246..d4272d4 100644 --- a/custom_components/localtuya/config_flow.py +++ b/custom_components/localtuya/config_flow.py @@ -258,7 +258,9 @@ async def validate_input(hass: core.HomeAssistant, data): detected_dps = await interface.detect_available_dps() except Exception as ex: try: - _LOGGER.debug("Initial state update failed (%s), trying reset command", ex) + _LOGGER.debug( + "Initial state update failed (%s), trying reset command", ex + ) if len(reset_ids) > 0: await interface.reset(reset_ids) detected_dps = await interface.detect_available_dps() @@ -493,7 +495,7 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow): else: errors["base"] = "discovery_failed" except Exception as ex: - _LOGGER.exception("discovery failed") + _LOGGER.exception("discovery failed: %s", ex) errors["base"] = "discovery_failed" devices = { @@ -604,9 +606,8 @@ class LocalTuyaOptionsFlowHandler(config_entries.OptionsFlow): defaults[CONF_LOCAL_KEY], ) defaults[CONF_LOCAL_KEY] = cloud_devs[dev_id].get(CONF_LOCAL_KEY) - placeholders = { - "for_device": f" for device `{dev_id}`.\nNOTE: a new local_key has been retrieved using cloud API" - } + note = "\nNOTE: a new local_key has been retrieved using cloud API" + placeholders = {"for_device": f" for device `{dev_id}`.{note}"} schema = schema_defaults(options_schema(self.entities), **defaults) else: defaults[CONF_PROTOCOL_VERSION] = "3.3" diff --git a/custom_components/localtuya/pytuya/__init__.py b/custom_components/localtuya/pytuya/__init__.py index cfa0618..472274a 100644 --- a/custom_components/localtuya/pytuya/__init__.py +++ b/custom_components/localtuya/pytuya/__init__.py @@ -923,9 +923,11 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger): if not isinstance(payload, str): try: payload = payload.decode() - except Exception: + except Exception as ex: self.debug("payload was not string type and decoding failed") - return self.error_json(ERR_JSON, payload) + raise DecodeError("payload was not a string: %s", ex) + # return self.error_json(ERR_JSON, payload) + if "data unvalid" in payload: self.dev_type = "type_0d" self.debug( @@ -943,7 +945,8 @@ class TuyaProtocol(asyncio.Protocol, ContextualLogger): try: json_payload = json.loads(payload) except Exception: - json_payload = self.error_json(ERR_JSON, payload) + raise DecodeError("could not decrypt data: wrong local_key?") + # json_payload = self.error_json(ERR_JSON, payload) # v3.4 stuffs it into {"data":{"dps":{"1":true}}, ...} if ( diff --git a/tox.ini b/tox.ini index 2602e5e..c753d25 100644 --- a/tox.ini +++ b/tox.ini @@ -1,13 +1,13 @@ [tox] skipsdist = true -envlist = py{38,39}, lint, typing +envlist = py{39,310}, lint, typing skip_missing_interpreters = True cs_exclude_words = hass,unvalid [gh-actions] python = - 3.8: clean, py38, lint, typing 3.9: clean, py39, lint, typing + 3.10: clean, py310, lint, typing [testenv] passenv = TOXENV,CI