From 3aed84560fe92b66d511d585fafefa2b49d2c77f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 27 Mar 2021 10:38:57 +0100 Subject: [PATCH] Merge of nested IF-IF cases - O-R (#48371) --- homeassistant/components/obihai/sensor.py | 5 ++-- .../components/octoprint/__init__.py | 15 +++++------ homeassistant/components/octoprint/sensor.py | 23 ++++++++--------- homeassistant/components/onewire/sensor.py | 5 ++-- homeassistant/components/person/__init__.py | 19 +++++++------- .../components/philips_js/media_player.py | 5 ++-- homeassistant/components/philips_js/remote.py | 7 +++--- homeassistant/components/plaato/sensor.py | 8 +++--- homeassistant/components/plex/server.py | 25 +++++++++++-------- homeassistant/components/plugwise/gateway.py | 5 ++-- .../components/rainforest_eagle/sensor.py | 16 +++++++----- homeassistant/components/recorder/__init__.py | 6 +---- .../ruckus_unleashed/device_tracker.py | 17 +++++++------ 13 files changed, 77 insertions(+), 79 deletions(-) diff --git a/homeassistant/components/obihai/sensor.py b/homeassistant/components/obihai/sensor.py index 9aacaa84193..639b9eb332f 100644 --- a/homeassistant/components/obihai/sensor.py +++ b/homeassistant/components/obihai/sensor.py @@ -146,9 +146,8 @@ class ObihaiServiceSensors(SensorEntity): services = self._pyobihai.get_line_state() - if services is not None: - if self._service_name in services: - self._state = services.get(self._service_name) + if services is not None and self._service_name in services: + self._state = services.get(self._service_name) call_direction = self._pyobihai.get_call_direction() diff --git a/homeassistant/components/octoprint/__init__.py b/homeassistant/components/octoprint/__init__.py index 66b804927c7..918f0258f78 100644 --- a/homeassistant/components/octoprint/__init__.py +++ b/homeassistant/components/octoprint/__init__.py @@ -212,14 +212,12 @@ class OctoPrintAPI: now = time.time() if endpoint == "job": last_time = self.job_last_reading[1] - if last_time is not None: - if now - last_time < 30.0: - return self.job_last_reading[0] + if last_time is not None and now - last_time < 30.0: + return self.job_last_reading[0] elif endpoint == "printer": last_time = self.printer_last_reading[1] - if last_time is not None: - if now - last_time < 30.0: - return self.printer_last_reading[0] + if last_time is not None and now - last_time < 30.0: + return self.printer_last_reading[0] url = self.api_url + endpoint try: @@ -300,8 +298,7 @@ def get_value_from_json(json_dict, sensor_type, group, tool): return json_dict[group][sensor_type] - if tool is not None: - if sensor_type in json_dict[group][tool]: - return json_dict[group][tool][sensor_type] + if tool is not None and sensor_type in json_dict[group][tool]: + return json_dict[group][tool][sensor_type] return None diff --git a/homeassistant/components/octoprint/sensor.py b/homeassistant/components/octoprint/sensor.py index f2c5c56c58a..16f6efce004 100644 --- a/homeassistant/components/octoprint/sensor.py +++ b/homeassistant/components/octoprint/sensor.py @@ -25,18 +25,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None): octoprint_api = hass.data[COMPONENT_DOMAIN][base_url] tools = octoprint_api.get_tools() - if "Temperatures" in monitored_conditions: - if not tools: - hass.components.persistent_notification.create( - "Your printer appears to be offline.
" - "If you do not want to have your printer on
" - " at all times, and you would like to monitor
" - "temperatures, please add
" - "bed and/or number_of_tools to your configuration
" - "and restart.", - title=NOTIFICATION_TITLE, - notification_id=NOTIFICATION_ID, - ) + if "Temperatures" in monitored_conditions and not tools: + hass.components.persistent_notification.create( + "Your printer appears to be offline.
" + "If you do not want to have your printer on
" + " at all times, and you would like to monitor
" + "temperatures, please add
" + "bed and/or number_of_tools to your configuration
" + "and restart.", + title=NOTIFICATION_TITLE, + notification_id=NOTIFICATION_ID, + ) devices = [] types = ["actual", "target"] diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 3ea29a904db..02af7a89ae3 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -266,9 +266,8 @@ def get_entities(onewirehub: OneWireHub, config): """Get a list of entities.""" entities = [] device_names = {} - if CONF_NAMES in config: - if isinstance(config[CONF_NAMES], dict): - device_names = config[CONF_NAMES] + if CONF_NAMES in config and isinstance(config[CONF_NAMES], dict): + device_names = config[CONF_NAMES] conf_type = config[CONF_TYPE] # We have an owserver on a remote(or local) host/port diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 7774694ff4e..1eb9d4eda7a 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -267,16 +267,15 @@ async def filter_yaml_data(hass: HomeAssistantType, persons: list[dict]) -> list for person_conf in persons: user_id = person_conf.get(CONF_USER_ID) - if user_id is not None: - if await hass.auth.async_get_user(user_id) is None: - _LOGGER.error( - "Invalid user_id detected for person %s", - person_conf[collection.CONF_ID], - ) - person_invalid_user.append( - f"- Person {person_conf[CONF_NAME]} (id: {person_conf[collection.CONF_ID]}) points at invalid user {user_id}" - ) - continue + if user_id is not None and await hass.auth.async_get_user(user_id) is None: + _LOGGER.error( + "Invalid user_id detected for person %s", + person_conf[collection.CONF_ID], + ) + person_invalid_user.append( + f"- Person {person_conf[CONF_NAME]} (id: {person_conf[collection.CONF_ID]}) points at invalid user {user_id}" + ) + continue filtered.append(person_conf) diff --git a/homeassistant/components/philips_js/media_player.py b/homeassistant/components/philips_js/media_player.py index 83adf61ed1e..7376d34e308 100644 --- a/homeassistant/components/philips_js/media_player.py +++ b/homeassistant/components/philips_js/media_player.py @@ -170,9 +170,8 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity): @property def state(self): """Get the device state. An exception means OFF state.""" - if self._tv.on: - if self._tv.powerstate == "On" or self._tv.powerstate is None: - return STATE_ON + if self._tv.on and (self._tv.powerstate == "On" or self._tv.powerstate is None): + return STATE_ON return STATE_OFF @property diff --git a/homeassistant/components/philips_js/remote.py b/homeassistant/components/philips_js/remote.py index 30b46499e28..f4d34904f1b 100644 --- a/homeassistant/components/philips_js/remote.py +++ b/homeassistant/components/philips_js/remote.py @@ -52,10 +52,9 @@ class PhilipsTVRemote(RemoteEntity): @property def is_on(self): """Return true if device is on.""" - if self._tv.on: - if self._tv.powerstate == "On" or self._tv.powerstate is None: - return True - return False + return bool( + self._tv.on and (self._tv.powerstate == "On" or self._tv.powerstate is None) + ) @property def should_poll(self): diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 0812a2fd585..9af16a1cacd 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -65,9 +65,11 @@ class PlaatoSensor(PlaatoEntity, SensorEntity): @property def device_class(self) -> str | None: """Return the class of this device, from component DEVICE_CLASSES.""" - if self._coordinator is not None: - if self._sensor_type == PlaatoKeg.Pins.TEMPERATURE: - return DEVICE_CLASS_TEMPERATURE + if ( + self._coordinator is not None + and self._sensor_type == PlaatoKeg.Pins.TEMPERATURE + ): + return DEVICE_CLASS_TEMPERATURE if self._sensor_type == ATTR_TEMP: return DEVICE_CLASS_TEMPERATURE return None diff --git a/homeassistant/components/plex/server.py b/homeassistant/components/plex/server.py index 841a9e7cc0d..d4bd4b09ef2 100644 --- a/homeassistant/components/plex/server.py +++ b/homeassistant/components/plex/server.py @@ -365,17 +365,20 @@ class PlexServer: PLAYER_SOURCE, source ) - if device.machineIdentifier not in ignored_clients: - if self.option_ignore_plexweb_clients and device.product == "Plex Web": - ignored_clients.add(device.machineIdentifier) - if device.machineIdentifier not in self._known_clients: - _LOGGER.debug( - "Ignoring %s %s: %s", - "Plex Web", - source, - device.machineIdentifier, - ) - return + if ( + device.machineIdentifier not in ignored_clients + and self.option_ignore_plexweb_clients + and device.product == "Plex Web" + ): + ignored_clients.add(device.machineIdentifier) + if device.machineIdentifier not in self._known_clients: + _LOGGER.debug( + "Ignoring %s %s: %s", + "Plex Web", + source, + device.machineIdentifier, + ) + return if device.machineIdentifier not in ( self._created_clients | ignored_clients | new_clients diff --git a/homeassistant/components/plugwise/gateway.py b/homeassistant/components/plugwise/gateway.py index 4e0e31810cb..3f8bc7ea626 100644 --- a/homeassistant/components/plugwise/gateway.py +++ b/homeassistant/components/plugwise/gateway.py @@ -110,9 +110,8 @@ async def async_setup_entry_gw(hass: HomeAssistant, entry: ConfigEntry) -> bool: api.get_all_devices() - if entry.unique_id is None: - if api.smile_version[0] != "1.8.0": - hass.config_entries.async_update_entry(entry, unique_id=api.smile_hostname) + if entry.unique_id is None and api.smile_version[0] != "1.8.0": + hass.config_entries.async_update_entry(entry, unique_id=api.smile_hostname) undo_listener = entry.add_update_listener(_update_listener) diff --git a/homeassistant/components/rainforest_eagle/sensor.py b/homeassistant/components/rainforest_eagle/sensor.py index 80475b4c21b..d333f9437f1 100644 --- a/homeassistant/components/rainforest_eagle/sensor.py +++ b/homeassistant/components/rainforest_eagle/sensor.py @@ -55,14 +55,18 @@ def hwtest(cloud_id, install_code, ip_address): response = reader.get_network_info() # Branch to test if target is Legacy Model - if "NetworkInfo" in response: - if response["NetworkInfo"].get("ModelId", None) == "Z109-EAGLE": - return reader + if ( + "NetworkInfo" in response + and response["NetworkInfo"].get("ModelId", None) == "Z109-EAGLE" + ): + return reader # Branch to test if target is Eagle-200 Model - if "Response" in response: - if response["Response"].get("Command", None) == "get_network_info": - return EagleReader(ip_address, cloud_id, install_code) + if ( + "Response" in response + and response["Response"].get("Command", None) == "get_network_info" + ): + return EagleReader(ip_address, cloud_id, install_code) # Catch-all if hardware ID tests fail raise ValueError("Couldn't determine device model.") diff --git a/homeassistant/components/recorder/__init__.py b/homeassistant/components/recorder/__init__.py index cff8119356f..f93d965a4b9 100644 --- a/homeassistant/components/recorder/__init__.py +++ b/homeassistant/components/recorder/__init__.py @@ -304,11 +304,7 @@ class Recorder(threading.Thread): return False entity_id = event.data.get(ATTR_ENTITY_ID) - if entity_id is not None: - if not self.entity_filter(entity_id): - return False - - return True + return bool(entity_id is None or self.entity_filter(entity_id)) def do_adhoc_purge(self, **kwargs): """Trigger an adhoc purge retaining keep_days worth of data.""" diff --git a/homeassistant/components/ruckus_unleashed/device_tracker.py b/homeassistant/components/ruckus_unleashed/device_tracker.py index 140aa3a8692..90a848b663b 100644 --- a/homeassistant/components/ruckus_unleashed/device_tracker.py +++ b/homeassistant/components/ruckus_unleashed/device_tracker.py @@ -67,14 +67,17 @@ def restore_entities(registry, coordinator, entry, async_add_entities, tracked): missing = [] for entity in registry.entities.values(): - if entity.config_entry_id == entry.entry_id and entity.platform == DOMAIN: - if entity.unique_id not in coordinator.data[API_CLIENTS]: - missing.append( - RuckusUnleashedDevice( - coordinator, entity.unique_id, entity.original_name - ) + if ( + entity.config_entry_id == entry.entry_id + and entity.platform == DOMAIN + and entity.unique_id not in coordinator.data[API_CLIENTS] + ): + missing.append( + RuckusUnleashedDevice( + coordinator, entity.unique_id, entity.original_name ) - tracked.add(entity.unique_id) + ) + tracked.add(entity.unique_id) if missing: async_add_entities(missing)