From f4cc4a0896358f4c8df5a193d900716fabb7ad32 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 26 Mar 2021 17:08:05 +0100 Subject: [PATCH] Merge of nested IF-IF cases - X-Z (#48373) --- .../components/xiaomi_miio/__init__.py | 15 +++++---- homeassistant/components/zha/climate.py | 23 ++++++++----- homeassistant/components/zha/core/gateway.py | 5 ++- homeassistant/components/zwave/climate.py | 15 +++++---- homeassistant/components/zwave/light.py | 10 +++--- homeassistant/components/zwave/lock.py | 22 ++++++------- .../components/zwave_js/binary_sensor.py | 12 +++---- .../components/zwave_js/discovery.py | 33 ++++++++++++------- 8 files changed, 77 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/xiaomi_miio/__init__.py b/homeassistant/components/xiaomi_miio/__init__.py index ccecc835b43..f97d4623d69 100644 --- a/homeassistant/components/xiaomi_miio/__init__.py +++ b/homeassistant/components/xiaomi_miio/__init__.py @@ -45,14 +45,15 @@ async def async_setup_entry( ): """Set up the Xiaomi Miio components from a config entry.""" hass.data.setdefault(DOMAIN, {}) - if entry.data[CONF_FLOW_TYPE] == CONF_GATEWAY: - if not await async_setup_gateway_entry(hass, entry): - return False - if entry.data[CONF_FLOW_TYPE] == CONF_DEVICE: - if not await async_setup_device_entry(hass, entry): - return False + if entry.data[ + CONF_FLOW_TYPE + ] == CONF_GATEWAY and not await async_setup_gateway_entry(hass, entry): + return False - return True + return bool( + entry.data[CONF_FLOW_TYPE] != CONF_DEVICE + or await async_setup_device_entry(hass, entry) + ) async def async_setup_gateway_entry( diff --git a/homeassistant/components/zha/climate.py b/homeassistant/components/zha/climate.py index 8292335ce23..475b0c5d0b8 100644 --- a/homeassistant/components/zha/climate.py +++ b/homeassistant/components/zha/climate.py @@ -448,15 +448,22 @@ class Thermostat(ZhaEntity, ClimateEntity): self.debug("preset mode '%s' is not supported", preset_mode) return - if self.preset_mode not in (preset_mode, PRESET_NONE): - if not await self.async_preset_handler(self.preset_mode, enable=False): - self.debug("Couldn't turn off '%s' preset", self.preset_mode) - return + if ( + self.preset_mode + not in ( + preset_mode, + PRESET_NONE, + ) + and not await self.async_preset_handler(self.preset_mode, enable=False) + ): + self.debug("Couldn't turn off '%s' preset", self.preset_mode) + return - if preset_mode != PRESET_NONE: - if not await self.async_preset_handler(preset_mode, enable=True): - self.debug("Couldn't turn on '%s' preset", preset_mode) - return + if preset_mode != PRESET_NONE and not await self.async_preset_handler( + preset_mode, enable=True + ): + self.debug("Couldn't turn on '%s' preset", preset_mode) + return self._preset = preset_mode self.async_write_ha_state() diff --git a/homeassistant/components/zha/core/gateway.py b/homeassistant/components/zha/core/gateway.py index d8baf89efcf..e944196a409 100644 --- a/homeassistant/components/zha/core/gateway.py +++ b/homeassistant/components/zha/core/gateway.py @@ -728,9 +728,8 @@ class LogRelayHandler(logging.Handler): def emit(self, record): """Relay log message via dispatcher.""" stack = [] - if record.levelno >= logging.WARN: - if not record.exc_info: - stack = [f for f, _, _, _ in traceback.extract_stack()] + if record.levelno >= logging.WARN and not record.exc_info: + stack = [f for f, _, _, _ in traceback.extract_stack()] entry = LogEntry(record, stack, _figure_out_source(record, stack, self.hass)) async_dispatcher_send( diff --git a/homeassistant/components/zwave/climate.py b/homeassistant/components/zwave/climate.py index ac8c47af9a3..75780eb314a 100644 --- a/homeassistant/components/zwave/climate.py +++ b/homeassistant/components/zwave/climate.py @@ -182,10 +182,12 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity): int(self.node.manufacturer_id, 16), int(self.node.product_id, 16), ) - if specific_sensor_key in DEVICE_MAPPINGS: - if DEVICE_MAPPINGS[specific_sensor_key] == WORKAROUND_ZXT_120: - _LOGGER.debug("Remotec ZXT-120 Zwave Thermostat workaround") - self._zxt_120 = 1 + if ( + specific_sensor_key in DEVICE_MAPPINGS + and DEVICE_MAPPINGS[specific_sensor_key] == WORKAROUND_ZXT_120 + ): + _LOGGER.debug("Remotec ZXT-120 Zwave Thermostat workaround") + self._zxt_120 = 1 self.update_properties() def _mode(self) -> None: @@ -567,9 +569,8 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity): def set_swing_mode(self, swing_mode): """Set new target swing mode.""" _LOGGER.debug("Set swing_mode to %s", swing_mode) - if self._zxt_120 == 1: - if self.values.zxt_120_swing_mode: - self.values.zxt_120_swing_mode.data = swing_mode + if self._zxt_120 == 1 and self.values.zxt_120_swing_mode: + self.values.zxt_120_swing_mode.data = swing_mode @property def extra_state_attributes(self): diff --git a/homeassistant/components/zwave/light.py b/homeassistant/components/zwave/light.py index 52014e37eea..140f601b1d9 100644 --- a/homeassistant/components/zwave/light.py +++ b/homeassistant/components/zwave/light.py @@ -138,10 +138,12 @@ class ZwaveDimmer(ZWaveDeviceEntity, LightEntity): int(self.node.manufacturer_id, 16), int(self.node.product_id, 16), ) - if specific_sensor_key in DEVICE_MAPPINGS: - if DEVICE_MAPPINGS[specific_sensor_key] == WORKAROUND_ZW098: - _LOGGER.debug("AEOTEC ZW098 workaround enabled") - self._zw098 = 1 + if ( + specific_sensor_key in DEVICE_MAPPINGS + and DEVICE_MAPPINGS[specific_sensor_key] == WORKAROUND_ZW098 + ): + _LOGGER.debug("AEOTEC ZW098 workaround enabled") + self._zw098 = 1 # Used for value change event handling self._refreshing = False diff --git a/homeassistant/components/zwave/lock.py b/homeassistant/components/zwave/lock.py index e6228a29334..bc49f9c0bd2 100644 --- a/homeassistant/components/zwave/lock.py +++ b/homeassistant/components/zwave/lock.py @@ -291,17 +291,17 @@ class ZwaveLock(ZWaveDeviceEntity, LockEntity): if self._state_workaround: self._state = LOCK_STATUS.get(str(notification_data)) _LOGGER.debug("workaround: lock state set to %s", self._state) - if self._v2btze: - if ( - self.values.v2btze_advanced - and self.values.v2btze_advanced.data == CONFIG_ADVANCED - ): - self._state = LOCK_STATUS.get(str(notification_data)) - _LOGGER.debug( - "Lock state set from Access Control value and is %s, get=%s", - str(notification_data), - self.state, - ) + if ( + self._v2btze + and self.values.v2btze_advanced + and self.values.v2btze_advanced.data == CONFIG_ADVANCED + ): + self._state = LOCK_STATUS.get(str(notification_data)) + _LOGGER.debug( + "Lock state set from Access Control value and is %s, get=%s", + str(notification_data), + self.state, + ) if self._track_message_workaround: this_message = self.node.stats["lastReceivedMessage"][5] diff --git a/homeassistant/components/zwave_js/binary_sensor.py b/homeassistant/components/zwave_js/binary_sensor.py index 47c374405b1..b97975b0507 100644 --- a/homeassistant/components/zwave_js/binary_sensor.py +++ b/homeassistant/components/zwave_js/binary_sensor.py @@ -285,12 +285,12 @@ class ZWaveBooleanBinarySensor(ZWaveBaseEntity, BinarySensorEntity): @property def entity_registry_enabled_default(self) -> bool: """Return if the entity should be enabled when first added to the entity registry.""" - if self.info.primary_value.command_class == CommandClass.SENSOR_BINARY: - # Legacy binary sensors are phased out (replaced by notification sensors) - # Disable by default to not confuse users - if self.info.node.device_class.generic.key != 0x20: - return False - return True + # Legacy binary sensors are phased out (replaced by notification sensors) + # Disable by default to not confuse users + return bool( + self.info.primary_value.command_class != CommandClass.SENSOR_BINARY + or self.info.node.device_class.generic.key == 0x20 + ) class ZWaveNotificationBinarySensor(ZWaveBaseEntity, BinarySensorEntity): diff --git a/homeassistant/components/zwave_js/discovery.py b/homeassistant/components/zwave_js/discovery.py index 5f1f04274ba..b59ae017da7 100644 --- a/homeassistant/components/zwave_js/discovery.py +++ b/homeassistant/components/zwave_js/discovery.py @@ -403,56 +403,64 @@ def async_discover_values(node: ZwaveNode) -> Generator[ZwaveDiscoveryInfo, None and value.node.manufacturer_id not in schema.manufacturer_id ): continue + # check product_id if ( schema.product_id is not None and value.node.product_id not in schema.product_id ): continue + # check product_type if ( schema.product_type is not None and value.node.product_type not in schema.product_type ): continue + # check firmware_version if ( schema.firmware_version is not None and value.node.firmware_version not in schema.firmware_version ): continue + # check device_class_basic if not check_device_class( value.node.device_class.basic, schema.device_class_basic ): continue + # check device_class_generic if not check_device_class( value.node.device_class.generic, schema.device_class_generic ): continue + # check device_class_specific if not check_device_class( value.node.device_class.specific, schema.device_class_specific ): continue + # check primary value if not check_value(value, schema.primary_value): continue + # check additional required values - if schema.required_values is not None: - if not all( - any(check_value(val, val_scheme) for val in node.values.values()) - for val_scheme in schema.required_values - ): - continue + if schema.required_values is not None and not all( + any(check_value(val, val_scheme) for val in node.values.values()) + for val_scheme in schema.required_values + ): + continue + # check for values that may not be present - if schema.absent_values is not None: - if any( - any(check_value(val, val_scheme) for val in node.values.values()) - for val_scheme in schema.absent_values - ): - continue + if schema.absent_values is not None and any( + any(check_value(val, val_scheme) for val in node.values.values()) + for val_scheme in schema.absent_values + ): + continue + # all checks passed, this value belongs to an entity yield ZwaveDiscoveryInfo( node=value.node, @@ -460,6 +468,7 @@ def async_discover_values(node: ZwaveNode) -> Generator[ZwaveDiscoveryInfo, None platform=schema.platform, platform_hint=schema.hint, ) + if not schema.allow_multi: # break out of loop, this value may not be discovered by other schemas/platforms break