Parcourir la source

Code cleanup

- rename variables that shadow builtins, and eliminate some.
- eliminate return None lines at the end of functions, and reorder some logic so that others can be eliminated too.

Remove Sonarcloud debug step, as it is working now.
Remove the filter on push only, as the repository owner should be enough. According to SonarCloud docs it should work out of the box for GitHub pull requests now.
Jason Rumney il y a 4 ans
Parent
commit
113632c0cb
2 fichiers modifiés avec 25 ajouts et 34 suppressions
  1. 1 6
      .github/workflows/tests.yml
  2. 24 28
      custom_components/tuya_local/generic/light.py

+ 1 - 6
.github/workflows/tests.yml

@@ -26,14 +26,9 @@ jobs:
         run: pytest --cov=. --cov-config=.coveragerc --cov-report xml:coverage.xml
         run: pytest --cov=. --cov-config=.coveragerc --cov-report xml:coverage.xml
       - name: Track master branch
       - name: Track master branch
         run: git fetch --no-tags https://$GITHUB_ACTOR:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY +refs/heads/main:refs/remotes/origin/main
         run: git fetch --no-tags https://$GITHUB_ACTOR:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY +refs/heads/main:refs/remotes/origin/main
-      - name: SonarCloud Debug
-        env:
-          GH_EVENT: ${{ github.event_name }}
-          GH_REPO: ${{ github.event.repository.owner.login }}
-        run: echo "event $GH_EVENT repo $GH_REPO"
       - name: SonarCloud scan
       - name: SonarCloud scan
         uses: sonarsource/sonarcloud-github-action@master
         uses: sonarsource/sonarcloud-github-action@master
-        if: github.event_name == 'push' && github.event.repository.owner.login == 'make-all'
+        if: github.event.repository.owner.login == 'make-all'
         env:
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
           SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

+ 24 - 28
custom_components/tuya_local/generic/light.py

@@ -98,14 +98,11 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
         """Return the color temperature in mireds"""
         """Return the color temperature in mireds"""
         if self._color_temp_dps:
         if self._color_temp_dps:
             unscaled = self._color_temp_dps.get_value(self._device)
             unscaled = self._color_temp_dps.get_value(self._device)
-            range = self._color_temp_dps.range(self._device)
-            if range:
-                min = range["min"]
-                max = range["max"]
-                return round(unscaled * 347 / (max - min) + 153 - min)
+            r = self._color_temp_dps.range(self._device)
+            if r:
+                return round(unscaled * 347 / (r["max"] - r["min"]) + 153 - r["min"])
             else:
             else:
                 return unscaled
                 return unscaled
-        return None
 
 
     @property
     @property
     def is_on(self):
     def is_on(self):
@@ -135,28 +132,28 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             # Either RGB or HSV can be used.
             # Either RGB or HSV can be used.
             color = self._rgbhsv_dps.decoded_value(self._device)
             color = self._rgbhsv_dps.decoded_value(self._device)
 
 
-            format = self._rgbhsv_dps.format
-            if format:
-                vals = unpack(format.get("format"), color)
+            fmt = self._rgbhsv_dps.format
+            if fmt:
+                vals = unpack(fmt.get("format"), color)
                 rgbhsv = {}
                 rgbhsv = {}
                 idx = 0
                 idx = 0
                 for v in vals:
                 for v in vals:
                     # Range in HA is 0-100 for s, 0-255 for rgb and v, 0-360
                     # Range in HA is 0-100 for s, 0-255 for rgb and v, 0-360
                     # for h
                     # for h
-                    n = format["names"][idx]
-                    r = format["ranges"][idx]
+                    n = fmt["names"][idx]
+                    r = fmt["ranges"][idx]
                     if r["min"] != 0:
                     if r["min"] != 0:
                         raise AttributeError(
                         raise AttributeError(
                             f"Unhandled minimum range for {n} in RGBW value"
                             f"Unhandled minimum range for {n} in RGBW value"
                         )
                         )
-                    max = r["max"]
+                    mx = r["max"]
                     scale = 1
                     scale = 1
                     if n == "h":
                     if n == "h":
-                        scale = 360 / max
+                        scale = 360 / mx
                     elif n == "s":
                     elif n == "s":
-                        scale = 100 / max
+                        scale = 100 / mx
                     else:
                     else:
-                        scale = 255 / max
+                        scale = 255 / mx
 
 
                     rgbhsv[n] = round(scale * v)
                     rgbhsv[n] = round(scale * v)
                     idx += 1
                     idx += 1
@@ -187,9 +184,8 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
             return self._effect_dps.get_value(self._device)
             return self._effect_dps.get_value(self._device)
         elif self._color_mode_dps:
         elif self._color_mode_dps:
             mode = self._color_mode_dps.get_value(self._device)
             mode = self._color_mode_dps.get_value(self._device)
-            if mode in VALID_COLOR_MODES:
-                return None
-            return mode
+            if mode not in VALID_COLOR_MODES:
+                return mode
 
 
     async def async_turn_on(self, **params):
     async def async_turn_on(self, **params):
         settings = {}
         settings = {}
@@ -205,12 +201,12 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                     **self._color_mode_dps.get_values_to_set(self._device, color_mode),
                     **self._color_mode_dps.get_values_to_set(self._device, color_mode),
                 }
                 }
             color_temp = params.get(ATTR_COLOR_TEMP)
             color_temp = params.get(ATTR_COLOR_TEMP)
-            range = self._color_temp_dps.range(self._device)
+            r = self._color_temp_dps.range(self._device)
 
 
-            if range and color_temp:
-                min = range["min"]
-                max = range["max"]
-                color_temp = round((color_temp - 153 + min) * (max - min) / 347)
+            if r and color_temp:
+                color_temp = round(
+                    (color_temp - 153 + r["min"]) * (r["max"] - r["min"]) / 347
+                )
 
 
             _LOGGER.debug(f"Setting color temp to {color_temp}")
             _LOGGER.debug(f"Setting color temp to {color_temp}")
             settings = {
             settings = {
@@ -231,8 +227,8 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                 }
                 }
             rgbw = params.get(ATTR_RGBW_COLOR, self.rgbw_color or (0, 0, 0, 0))
             rgbw = params.get(ATTR_RGBW_COLOR, self.rgbw_color or (0, 0, 0, 0))
             brightness = params.get(ATTR_BRIGHTNESS, rgbw[3])
             brightness = params.get(ATTR_BRIGHTNESS, rgbw[3])
-            format = self._rgbhsv_dps.format
-            if rgbw and format:
+            fmt = self._rgbhsv_dps.format
+            if rgbw and fmt:
                 rgb = (rgbw[0], rgbw[1], rgbw[2])
                 rgb = (rgbw[0], rgbw[1], rgbw[2])
                 hs = color_util.color_RGB_to_hs(rgbw[0], rgbw[1], rgbw[2])
                 hs = color_util.color_RGB_to_hs(rgbw[0], rgbw[1], rgbw[2])
                 rgbhsv = {
                 rgbhsv = {
@@ -248,8 +244,8 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                 )
                 )
                 ordered = []
                 ordered = []
                 idx = 0
                 idx = 0
-                for n in format["names"]:
-                    r = format["ranges"][idx]
+                for n in fmt["names"]:
+                    r = fmt["ranges"][idx]
                     scale = 1
                     scale = 1
                     if n == "s":
                     if n == "s":
                         scale = r["max"] / 100
                         scale = r["max"] / 100
@@ -259,7 +255,7 @@ class TuyaLocalLight(TuyaLocalEntity, LightEntity):
                         scale = r["max"] / 255
                         scale = r["max"] / 255
                     ordered.append(round(rgbhsv[n] * scale))
                     ordered.append(round(rgbhsv[n] * scale))
                     idx += 1
                     idx += 1
-                binary = pack(format["format"], *ordered)
+                binary = pack(fmt["format"], *ordered)
                 settings = {
                 settings = {
                     **settings,
                     **settings,
                     **self._rgbhsv_dps.get_values_to_set(
                     **self._rgbhsv_dps.get_values_to_set(