Преглед на файлове

Avoid explicit returning of None where easy.

Explicity returning None is not necessary at the end of functions in
Python, since None is the default return code.
Rearrange some functions to eliminate some returning of None just
before the end as well.

Shorter functions are easier to follow, so this change helps
readability for those accustomed to Python.
Jason Rumney преди 1 година
родител
ревизия
afa13cc063

+ 0 - 1
custom_components/tuya_local/binary_sensor.py

@@ -59,7 +59,6 @@ class TuyaLocalBinarySensor(TuyaLocalEntity, BinarySensorEntity):
                     self.name or "binary_sensor",
                     dclass,
                 )
-            return None
 
     @property
     def is_on(self):

+ 13 - 21
custom_components/tuya_local/climate.py

@@ -58,7 +58,8 @@ def validate_temp_unit(unit):
     try:
         return UnitOfTemperature(unit)
     except ValueError:
-        return None
+        if unit:
+            _LOGGER.warning("%s is not a valid temperature unit", unit)
 
 
 class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
@@ -268,9 +269,8 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def current_temperature(self):
         """Return the current measured temperature."""
-        if self._current_temperature_dps is None:
-            return None
-        return self._current_temperature_dps.get_value(self._device)
+        if self._current_temperature_dps:
+            return self._current_temperature_dps.get_value(self._device)
 
     @property
     def target_humidity(self):
@@ -304,9 +304,8 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def current_humidity(self):
         """Return the current measured humidity."""
-        if self._current_humidity_dps is None:
-            return None
-        return self._current_humidity_dps.get_value(self._device)
+        if self._current_humidity_dps:
+            return self._current_humidity_dps.get_value(self._device)
 
     @property
     def hvac_action(self):
@@ -323,7 +322,6 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
                 self.name or "climate",
                 action,
             )
-            return None
 
     @property
     def hvac_mode(self):
@@ -340,7 +338,6 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
                 self.name or "climate",
                 hvac_mode,
             )
-            return None
 
     @property
     def hvac_modes(self):
@@ -380,9 +377,7 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def is_aux_heat(self):
         """Return state of aux heater"""
-        if self._aux_heat_dps is None:
-            return None
-        else:
+        if self._aux_heat_dps:
             return self._aux_heat_dps.get_value(self._device)
 
     async def async_turn_aux_heat_on(self):
@@ -407,9 +402,8 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def preset_modes(self):
         """Return the list of presets that this device supports."""
-        if self._preset_mode_dps is None:
-            return None
-        return self._preset_mode_dps.values(self._device)
+        if self._preset_mode_dps:
+            return self._preset_mode_dps.values(self._device)
 
     async def async_set_preset_mode(self, preset_mode):
         """Set the preset mode."""
@@ -427,9 +421,8 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def swing_modes(self):
         """Return the list of swing modes that this device supports."""
-        if self._swing_mode_dps is None:
-            return None
-        return self._swing_mode_dps.values(self._device)
+        if self._swing_mode_dps:
+            return self._swing_mode_dps.values(self._device)
 
     async def async_set_swing_mode(self, swing_mode):
         """Set the preset mode."""
@@ -447,9 +440,8 @@ class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
     @property
     def fan_modes(self):
         """Return the list of fan modes that this device supports."""
-        if self._fan_mode_dps is None:
-            return None
-        return self._fan_mode_dps.values(self._device)
+        if self._fan_mode_dps:
+            return self._fan_mode_dps.values(self._device)
 
     async def async_set_fan_mode(self, fan_mode):
         """Set the fan mode."""

+ 0 - 1
custom_components/tuya_local/cover.py

@@ -74,7 +74,6 @@ class TuyaLocalCover(TuyaLocalEntity, CoverEntity):
                     self.name or "cover",
                     dclass,
                 )
-            return None
 
     @property
     def supported_features(self):

+ 7 - 8
custom_components/tuya_local/device.py

@@ -371,15 +371,14 @@ class TuyaLocalDevice(object):
                 best_quality = quality
                 best_match = config
 
-        if best_match is None:
-            _LOGGER.warning(
-                "Detection for %s with dps %s failed",
-                self.name,
-                log_json(cached_state),
-            )
-            return None
+        if best_match:
+            return best_match.config_type
 
-        return best_match.config_type
+        _LOGGER.warning(
+            "Detection for %s with dps %s failed",
+            self.name,
+            log_json(cached_state),
+        )
 
     async def async_refresh(self):
         _LOGGER.debug("Refreshing device state for %s", self.name)

+ 6 - 9
custom_components/tuya_local/fan.py

@@ -168,9 +168,8 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
     @property
     def preset_mode(self):
         """Return the current preset mode."""
-        if self._preset_dps is None:
-            return None
-        return self._preset_dps.get_value(self._device)
+        if self._preset_dps:
+            return self._preset_dps.get_value(self._device)
 
     @property
     def preset_modes(self):
@@ -188,9 +187,8 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
     @property
     def current_direction(self):
         """Return the current direction [forward or reverse]."""
-        if self._direction_dps is None:
-            return None
-        return self._direction_dps.get_value(self._device)
+        if self._direction_dps:
+            return self._direction_dps.get_value(self._device)
 
     async def async_set_direction(self, direction):
         """Set the direction of the fan."""
@@ -201,9 +199,8 @@ class TuyaLocalFan(TuyaLocalEntity, FanEntity):
     @property
     def oscillating(self):
         """Return whether or not the fan is oscillating."""
-        if self._oscillate_dps is None:
-            return None
-        return self._oscillate_dps.get_value(self._device)
+        if self._oscillate_dps:
+            return self._oscillate_dps.get_value(self._device)
 
     async def async_oscillate(self, oscillating):
         """Oscillate the fan."""

+ 2 - 3
custom_components/tuya_local/humidifier.py

@@ -130,9 +130,8 @@ class TuyaLocalHumidifier(TuyaLocalEntity, HumidifierEntity):
     @property
     def available_modes(self):
         """Return the list of presets that this device supports."""
-        if self._mode_dp is None:
-            return None
-        return self._mode_dp.values(self._device)
+        if self._mode_dp:
+            return self._mode_dp.values(self._device)
 
     async def async_set_mode(self, mode):
         """Set the preset mode."""

+ 0 - 2
custom_components/tuya_local/sensor.py

@@ -69,8 +69,6 @@ class TuyaLocalSensor(TuyaLocalEntity, SensorEntity):
         sclass = self._sensor_dps.state_class
         if sclass in STATE_CLASSES:
             return sclass
-        else:
-            return None
 
     @property
     def native_value(self):

+ 15 - 5
custom_components/tuya_local/switch.py

@@ -2,6 +2,8 @@
 Setup for different kinds of Tuya switch devices
 """
 
+import logging
+
 from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
 
 from .device import TuyaLocalDevice
@@ -9,6 +11,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}
@@ -38,11 +42,17 @@ class TuyaLocalSwitch(TuyaLocalEntity, SwitchEntity):
     @property
     def device_class(self):
         """Return the class of this device"""
-        return (
-            SwitchDeviceClass.OUTLET
-            if self._config.device_class == "outlet"
-            else SwitchDeviceClass.SWITCH
-        )
+        dclass = self._config.device_class
+        try:
+            return SwitchDeviceClass(dclass)
+        except:
+            if dclass:
+                _LOGGER.warning(
+                    "%s/%s: Unrecognised switch device class of %s ignored",
+                    self._config._device.config,
+                    self.name or "switch",
+                    dclass,
+                )
 
     @property
     def is_on(self):

+ 3 - 4
custom_components/tuya_local/water_heater.py

@@ -37,7 +37,7 @@ def validate_temp_unit(unit):
     try:
         return UnitOfTemperature(unit)
     except ValueError:
-        return None
+        _LOGGER.warning("%s is not a valid temperature unit", unit)
 
 
 class TuyaLocalWaterHeater(TuyaLocalEntity, WaterHeaterEntity):
@@ -137,9 +137,8 @@ class TuyaLocalWaterHeater(TuyaLocalEntity, WaterHeaterEntity):
     @property
     def current_temperature(self):
         """Return the current temperature."""
-        if self._current_temperature_dps is None:
-            return None
-        return self._current_temperature_dps.get_value(self._device)
+        if self._current_temperature_dps:
+            return self._current_temperature_dps.get_value(self._device)
 
     @property
     def target_temperature(self):