4
0
Эх сурвалжийг харах

Enable general lint checking for main source.

Fix several issues picked up.
  - comparing types with is instead of instanceof
  - unused code
  - undefined _LOGGER used in exceptional case (untested branch)
  - f-strings without format specifiers inside (converted to sprintf type)
  - bare except
  - incorrect handling of multiple command list in remote.send_command
Jason Rumney 2 жил өмнө
parent
commit
eb2bdb2b1a

+ 4 - 2
.github/workflows/linting.yml

@@ -17,7 +17,9 @@ jobs:
         run: |
           python -m pip install --upgrade pip
           pip install -r requirements-dev.txt
-      - name: isort
+      - name: Lint check
+        run: ruff check custom_components/tuya_local
+      - name: Check include order
         run: ruff check --select I --diff .
-      - name: Black
+      - name: Check coding style
         run: ruff format --check --diff .

+ 0 - 2
custom_components/tuya_local/const.py

@@ -1,5 +1,3 @@
-from datetime import timedelta
-
 DOMAIN = "tuya_local"
 
 CONF_DEVICE_ID = "device_id"

+ 2 - 2
custom_components/tuya_local/device.py

@@ -196,7 +196,7 @@ class TuyaLocalDevice(object):
         """Coroutine wrapper for async_receive generator."""
         try:
             async for poll in self.async_receive():
-                if type(poll) is dict:
+                if isinstance(poll, dict):
                     _LOGGER.debug(
                         "%s received %s",
                         self.name,
@@ -521,7 +521,7 @@ class TuyaLocalDevice(object):
             try:
                 if not self._hass.is_stopping:
                     retval = await self._hass.async_add_executor_job(func)
-                    if type(retval) is dict and "Error" in retval:
+                    if isinstance(retval, dict) and "Error" in retval:
                         raise AttributeError(retval["Error"])
                     self._api_protocol_working = True
                     self._api_working_protocol_failures = 0

+ 4 - 0
custom_components/tuya_local/event.py

@@ -1,6 +1,8 @@
 """
 Implementation of Tuya events
 """
+import logging
+
 from homeassistant.components.event import EventDeviceClass, EventEntity
 
 from .device import TuyaLocalDevice
@@ -8,6 +10,8 @@ from .helpers.config import async_tuya_setup_platform
 from .helpers.device_config import TuyaEntityConfig
 from .helpers.mixin import TuyaLocalEntity
 
+_LOGGER = logging.getLogger(__name__)
+
 
 async def async_setup_entry(hass, config_entry, async_add_entities):
     config = {**config_entry.data, **config_entry.options}

+ 4 - 4
custom_components/tuya_local/helpers/config.py

@@ -28,10 +28,10 @@ async def async_tuya_setup_platform(
             entities.append(data[ecfg.config_id])
             if ecfg.deprecated:
                 _LOGGER.warning(ecfg.deprecation_message)
-            _LOGGER.debug(f"Adding %s for %s", platform, ecfg.config_id)
+            _LOGGER.debug("Adding %s for %s", platform, ecfg.config_id)
         except Exception as e:
             _LOGGER.error(
-                f"Error adding %s for %s: %s",
+                "Error adding %s for %s: %s",
                 ecfg.config_id,
                 cfg.config,
                 e,
@@ -44,10 +44,10 @@ async def async_tuya_setup_platform(
                 entities.append(data[ecfg.config_id])
                 if ecfg.deprecated:
                     _LOGGER.warning(ecfg.deprecation_message)
-                _LOGGER.debug(f"Adding %s for %s", platform, ecfg.config_id)
+                _LOGGER.debug("Adding %s for %s", platform, ecfg.config_id)
             except Exception as e:
                 _LOGGER.error(
-                    f"Error adding %s for %s: %s",
+                    "Error adding %s for %s: %s",
                     ecfg.config_id,
                     cfg.config,
                     e,

+ 1 - 1
custom_components/tuya_local/helpers/device_config.py

@@ -710,7 +710,7 @@ class TuyaDpsConfig:
                 try:
                     result = datetime.fromtimestamp(result)
                     replaced = True
-                except:
+                except Exception:
                     _LOGGER.warning("Invalid timestamp %d", result)
 
             if replaced:

+ 3 - 4
custom_components/tuya_local/remote.py

@@ -165,7 +165,7 @@ class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
                         f"Command {repr(cmd)} not found for {subdevice}"
                     ) from err
                 if isinstance(codes, list):
-                    codes = code[:]
+                    codes = codes[:]
                 else:
                     codes = [codes]
 
@@ -216,7 +216,6 @@ class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
         """Send remote commands"""
         kwargs[ATTR_COMMAND] = command
         kwargs = SERVICE_SEND_SCHEMA(kwargs)
-        commands = kwargs[ATTR_COMMAND]
         subdevice = kwargs.get(ATTR_DEVICE)
         repeat = kwargs.get(ATTR_NUM_REPEATS)
         delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS) * 1000
@@ -256,7 +255,6 @@ class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
         commands = kwargs[ATTR_COMMAND]
         subdevice = kwargs[ATTR_DEVICE]
         toggle = kwargs[ATTR_ALTERNATIVE]
-        service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
 
         if not self._storage_loaded:
             await self._async_load_storage()
@@ -280,6 +278,7 @@ class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
 
     async def _async_learn_command(self, command):
         """Learn a single command"""
+        service = f"{RM_DOMAIN}.{SERVICE_LEARN_COMMAND}"
         if self._control_dp:
             await self._control_dp.async_set_value(self._device, CMD_LEARN)
         else:
@@ -301,7 +300,7 @@ class TuyaLocalRemote(TuyaLocalEntity, RemoteEntity):
                 code = self._receive_dp.get_value(self._device)
                 if code is not None:
                     return code
-
+            _LOGGER.warning("Timed out without receiving code in %s", service)
             raise TimeoutError(
                 f"No remote code received within {LEARNING_TIMEOUT.total_seconds()} seconds",
             )