Skip to content

Commit

Permalink
Avoid explicit returning of None where easy.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
make-all committed Mar 8, 2024
1 parent dfbf670 commit afa13cc
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 54 deletions.
1 change: 0 additions & 1 deletion custom_components/tuya_local/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def device_class(self):
self.name or "binary_sensor",
dclass,
)
return None

@property
def is_on(self):
Expand Down
34 changes: 13 additions & 21 deletions custom_components/tuya_local/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -268,9 +269,8 @@ async def async_set_target_temperature_range(self, low, high):
@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):
Expand Down Expand Up @@ -304,9 +304,8 @@ async def async_set_humidity(self, humidity: int):
@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):
Expand All @@ -323,7 +322,6 @@ def hvac_action(self):
self.name or "climate",
action,
)
return None

@property
def hvac_mode(self):
Expand All @@ -340,7 +338,6 @@ def hvac_mode(self):
self.name or "climate",
hvac_mode,
)
return None

@property
def hvac_modes(self):
Expand Down Expand Up @@ -380,9 +377,7 @@ async def async_turn_off(self):
@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):
Expand All @@ -407,9 +402,8 @@ def preset_mode(self):
@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."""
Expand All @@ -427,9 +421,8 @@ def swing_mode(self):
@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."""
Expand All @@ -447,9 +440,8 @@ def fan_mode(self):
@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."""
Expand Down
1 change: 0 additions & 1 deletion custom_components/tuya_local/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ def device_class(self):
self.name or "cover",
dclass,
)
return None

@property
def supported_features(self):
Expand Down
15 changes: 7 additions & 8 deletions custom_components/tuya_local/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,14 @@ async def async_inferred_type(self):
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)
Expand Down
15 changes: 6 additions & 9 deletions custom_components/tuya_local/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ async def async_set_percentage(self, percentage):
@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):
Expand All @@ -188,9 +187,8 @@ async def async_set_preset_mode(self, preset_mode):
@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."""
Expand All @@ -201,9 +199,8 @@ async def async_set_direction(self, direction):
@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."""
Expand Down
5 changes: 2 additions & 3 deletions custom_components/tuya_local/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,8 @@ def mode(self):
@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."""
Expand Down
2 changes: 0 additions & 2 deletions custom_components/tuya_local/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ def state_class(self):
sclass = self._sensor_dps.state_class
if sclass in STATE_CLASSES:
return sclass
else:
return None

@property
def native_value(self):
Expand Down
20 changes: 15 additions & 5 deletions custom_components/tuya_local/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
Setup for different kinds of Tuya switch devices
"""

import logging

from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity

from .device import TuyaLocalDevice
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}
Expand Down Expand Up @@ -38,11 +42,17 @@ def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
@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):
Expand Down
7 changes: 3 additions & 4 deletions custom_components/tuya_local/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -137,9 +137,8 @@ def is_away_mode_on(self):
@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):
Expand Down

0 comments on commit afa13cc

Please sign in to comment.