From 2792a5f01696ba1c25d3a94c9015e8d55c274115 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Mon, 11 Mar 2024 14:23:52 +0100 Subject: [PATCH] Enable even more SIM ruff rules (#113017) * SIM202 SIM211 SIM220 SIM221 SIM222 SIM223 * SIM910 SIM911 * SIM * Update homeassistant/components/mqtt/siren.py Co-authored-by: Jan Bouwhuis * Update homeassistant/components/mqtt/siren.py Co-authored-by: Jan Bouwhuis --------- Co-authored-by: Jan Bouwhuis --- homeassistant/components/bloomsky/sensor.py | 8 ++----- homeassistant/components/lifx/util.py | 2 +- homeassistant/components/matter/api.py | 2 +- .../components/modbus/base_platform.py | 6 +++--- homeassistant/components/modbus/validators.py | 8 ++++--- homeassistant/components/mqtt/siren.py | 5 +---- homeassistant/components/whirlpool/sensor.py | 2 +- homeassistant/components/yolink/sensor.py | 2 +- pyproject.toml | 21 ++++--------------- tests/components/lutron_caseta/__init__.py | 2 +- 10 files changed, 20 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/bloomsky/sensor.py b/homeassistant/components/bloomsky/sensor.py index c6a74731d37..1f63b4a7256 100644 --- a/homeassistant/components/bloomsky/sensor.py +++ b/homeassistant/components/bloomsky/sensor.py @@ -102,13 +102,9 @@ class BloomSkySensor(SensorEntity): self._attr_name = f"{device['DeviceName']} {sensor_name}" self._attr_unique_id = f"{self._device_id}-{sensor_name}" self._attr_device_class = SENSOR_DEVICE_CLASS.get(sensor_name) - self._attr_native_unit_of_measurement = SENSOR_UNITS_IMPERIAL.get( - sensor_name, None - ) + self._attr_native_unit_of_measurement = SENSOR_UNITS_IMPERIAL.get(sensor_name) if self._bloomsky.is_metric: - self._attr_native_unit_of_measurement = SENSOR_UNITS_METRIC.get( - sensor_name, None - ) + self._attr_native_unit_of_measurement = SENSOR_UNITS_METRIC.get(sensor_name) def update(self) -> None: """Request an update from the BloomSky API.""" diff --git a/homeassistant/components/lifx/util.py b/homeassistant/components/lifx/util.py index 5d41839f61d..bb77c7595d3 100644 --- a/homeassistant/components/lifx/util.py +++ b/homeassistant/components/lifx/util.py @@ -62,7 +62,7 @@ def infrared_brightness_value_to_option(value: int) -> str | None: def infrared_brightness_option_to_value(option: str) -> int | None: """Convert infrared brightness option to value.""" option_values = {v: k for k, v in INFRARED_BRIGHTNESS_VALUES_MAP.items()} - return option_values.get(option, None) + return option_values.get(option) def convert_8_to_16(value: int) -> int: diff --git a/homeassistant/components/matter/api.py b/homeassistant/components/matter/api.py index 94980617c0d..e6a2a6c54d5 100644 --- a/homeassistant/components/matter/api.py +++ b/homeassistant/components/matter/api.py @@ -166,7 +166,7 @@ async def websocket_commission_on_network( ) -> None: """Commission a device already on the network.""" await matter.matter_client.commission_on_network( - msg["pin"], ip_addr=msg.get("ip_addr", None) + msg["pin"], ip_addr=msg.get("ip_addr") ) connection.send_result(msg[ID]) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 24d6ce2b307..82b2b788e78 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -102,7 +102,7 @@ class BasePlatform(Entity): ) self._hub = hub - self._slave = entry.get(CONF_SLAVE, None) or entry.get(CONF_DEVICE_ADDRESS, 0) + self._slave = entry.get(CONF_SLAVE) or entry.get(CONF_DEVICE_ADDRESS, 0) self._address = int(entry[CONF_ADDRESS]) self._input_type = entry[CONF_INPUT_TYPE] self._value: str | None = None @@ -128,7 +128,7 @@ class BasePlatform(Entity): self._min_value = get_optional_numeric_config(CONF_MIN_VALUE) self._max_value = get_optional_numeric_config(CONF_MAX_VALUE) - self._nan_value = entry.get(CONF_NAN_VALUE, None) + self._nan_value = entry.get(CONF_NAN_VALUE) self._zero_suppress = get_optional_numeric_config(CONF_ZERO_SUPPRESS) @abstractmethod @@ -184,7 +184,7 @@ class BaseStructPlatform(BasePlatform, RestoreEntity): self._structure: str = config[CONF_STRUCTURE] self._scale = config[CONF_SCALE] self._offset = config[CONF_OFFSET] - self._slave_count = config.get(CONF_SLAVE_COUNT, None) or config.get( + self._slave_count = config.get(CONF_SLAVE_COUNT) or config.get( CONF_VIRTUAL_COUNT, 0 ) self._slave_size = self._count = config[CONF_COUNT] diff --git a/homeassistant/components/modbus/validators.py b/homeassistant/components/modbus/validators.py index c484b270afc..2e434933eae 100644 --- a/homeassistant/components/modbus/validators.py +++ b/homeassistant/components/modbus/validators.py @@ -118,8 +118,8 @@ def struct_validator(config: dict[str, Any]) -> dict[str, Any]: data_type = config[CONF_DATA_TYPE] if data_type == "int": data_type = config[CONF_DATA_TYPE] = DataType.INT16 - count = config.get(CONF_COUNT, None) - structure = config.get(CONF_STRUCTURE, None) + count = config.get(CONF_COUNT) + structure = config.get(CONF_STRUCTURE) slave_count = config.get(CONF_SLAVE_COUNT, config.get(CONF_VIRTUAL_COUNT)) validator = DEFAULT_STRUCT_FORMAT[data_type].validate_parm swap_type = config.get(CONF_SWAP) @@ -148,6 +148,8 @@ def struct_validator(config: dict[str, Any]) -> dict[str, Any]: raise vol.Invalid(error) if config[CONF_DATA_TYPE] == DataType.CUSTOM: + assert isinstance(structure, str) + assert isinstance(count, int) try: size = struct.calcsize(structure) except struct.error as err: @@ -332,7 +334,7 @@ def check_config(config: dict) -> dict: ): if conf_type in entity: addr += f"_{entity[conf_type]}" - inx = entity.get(CONF_SLAVE, None) or entity.get(CONF_DEVICE_ADDRESS, 0) + inx = entity.get(CONF_SLAVE) or entity.get(CONF_DEVICE_ADDRESS, 0) addr += f"_{inx}" loc_addr: set[str] = {addr} diff --git a/homeassistant/components/mqtt/siren.py b/homeassistant/components/mqtt/siren.py index 2725743735d..5eda05d531f 100644 --- a/homeassistant/components/mqtt/siren.py +++ b/homeassistant/components/mqtt/siren.py @@ -301,10 +301,7 @@ class MqttSiren(MqttEntity, SirenEntity): else {} ) if extra_attributes: - return ( - dict({*self._extra_attributes.items(), *extra_attributes.items()}) - or None - ) + return dict({*self._extra_attributes.items(), *extra_attributes.items()}) return self._extra_attributes or None async def _async_publish( diff --git a/homeassistant/components/whirlpool/sensor.py b/homeassistant/components/whirlpool/sensor.py index a710dfb81a8..d49459cc726 100644 --- a/homeassistant/components/whirlpool/sensor.py +++ b/homeassistant/components/whirlpool/sensor.py @@ -87,7 +87,7 @@ def washer_state(washer: WasherDryer) -> str | None: if func(washer): return cycle_name - return MACHINE_STATE.get(machine_state, None) + return MACHINE_STATE.get(machine_state) @dataclass(frozen=True, kw_only=True) diff --git a/homeassistant/components/yolink/sensor.py b/homeassistant/components/yolink/sensor.py index b69e29db031..17aa968840f 100644 --- a/homeassistant/components/yolink/sensor.py +++ b/homeassistant/components/yolink/sensor.py @@ -119,7 +119,7 @@ def cvt_volume(val: int | None) -> str | None: if val is None: return None volume_level = {1: "low", 2: "medium", 3: "high"} - return volume_level.get(val, None) + return volume_level.get(val) SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = ( diff --git a/pyproject.toml b/pyproject.toml index 6d4bfb9e4e2..bcfcfbc4a4b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -627,23 +627,7 @@ select = [ "S604", # call-with-shell-equals-true "S608", # hardcoded-sql-expression "S609", # unix-command-wildcard-injection - "SIM101", # Multiple isinstance calls for {name}, merge into a single call - "SIM103", # Return the condition {condition} directly - "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass - "SIM107", # Don't use return in try-except and finally - "SIM109", # Use {replacement} instead of multiple equality comparisons - "SIM110", # Use {replacement} instead of for loop - "SIM112", # Use capitalized environment variable {expected} instead of {actual} - "SIM113", # Use enumerate() for index variable {index} in for loop - "SIM114", # Combine if branches using logical or operator - "SIM116", # Use a dictionary instead of consecutive if statements - "SIM117", # Merge with-statements that use the same scope - "SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys() - "SIM201", # Use {left} != {right} instead of not {left} == {right} - "SIM208", # Use {expr} instead of not (not {expr}) - "SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a} - "SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'. - "SIM401", # Use get from dict with default instead of an if block + "SIM", # flake8-simplify "T100", # Trace found: {name} used "T20", # flake8-print "TID251", # Banned imports @@ -675,6 +659,9 @@ ignore = [ "PLR0915", # Too many statements ({statements} > {max_statements}) "PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable "PLW2901", # Outer {outer_kind} variable {name} overwritten by inner {inner_kind} target + "SIM102", # Use a single if statement instead of nested if statements + "SIM108", # Use ternary operator {contents} instead of if-else-block + "SIM115", # Use context handler for opening files "UP006", # keep type annotation style as is "UP007", # keep type annotation style as is # Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923 diff --git a/tests/components/lutron_caseta/__init__.py b/tests/components/lutron_caseta/__init__.py index c82a0c78ea1..cc785f71e19 100644 --- a/tests/components/lutron_caseta/__init__.py +++ b/tests/components/lutron_caseta/__init__.py @@ -283,7 +283,7 @@ class MockBridge: :param domain: one of 'light', 'switch', 'cover', 'fan' or 'sensor' :returns list of zero or more of the devices """ - types = _LEAP_DEVICE_TYPES.get(domain, None) + types = _LEAP_DEVICE_TYPES.get(domain) # return immediately if not a supported domain if types is None: