diff --git a/homeassistant/components/compensation/sensor.py b/homeassistant/components/compensation/sensor.py index 257c6b4a354..110326bc1b2 100644 --- a/homeassistant/components/compensation/sensor.py +++ b/homeassistant/components/compensation/sensor.py @@ -130,8 +130,7 @@ class CompensationSensor(SensorEntity): @callback def _async_compensation_sensor_state_listener(self, event): """Handle sensor state changes.""" - new_state = event.data.get("new_state") - if new_state is None: + if (new_state := event.data.get("new_state")) is None: return if self._unit_of_measurement is None and self._source_attribute is None: diff --git a/homeassistant/components/ios/notify.py b/homeassistant/components/ios/notify.py index a096a43ac85..0c7ba59b533 100644 --- a/homeassistant/components/ios/notify.py +++ b/homeassistant/components/ios/notify.py @@ -76,9 +76,7 @@ class iOSNotificationService(BaseNotificationService): ): data[ATTR_TITLE] = kwargs.get(ATTR_TITLE) - targets = kwargs.get(ATTR_TARGET) - - if not targets: + if not (targets := kwargs.get(ATTR_TARGET)): targets = ios.enabled_push_ids(self.hass) if kwargs.get(ATTR_DATA) is not None: diff --git a/homeassistant/components/kodi/browse_media.py b/homeassistant/components/kodi/browse_media.py index c36f05fc0db..1b0c5d521c9 100644 --- a/homeassistant/components/kodi/browse_media.py +++ b/homeassistant/components/kodi/browse_media.py @@ -146,8 +146,7 @@ async def item_payload(item, get_thumbnail_url=None): elif "channelid" in item: media_content_type = MEDIA_TYPE_CHANNEL media_content_id = f"{item['channelid']}" - broadcasting = item.get("broadcastnow") - if broadcasting: + if broadcasting := item.get("broadcastnow"): show = broadcasting.get("title") title = f"{title} - {show}" can_play = True diff --git a/homeassistant/components/kodi/config_flow.py b/homeassistant/components/kodi/config_flow.py index ff3753af7ec..404540d47aa 100644 --- a/homeassistant/components/kodi/config_flow.py +++ b/homeassistant/components/kodi/config_flow.py @@ -57,8 +57,7 @@ async def validate_http(hass: core.HomeAssistant, data): async def validate_ws(hass: core.HomeAssistant, data): """Validate the user input allows us to connect over WS.""" - ws_port = data.get(CONF_WS_PORT) - if not ws_port: + if not (ws_port := data.get(CONF_WS_PORT)): return host = data[CONF_HOST] @@ -105,8 +104,7 @@ class KodiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._host = discovery_info["host"] self._port = int(discovery_info["port"]) self._name = discovery_info["hostname"][: -len(".local.")] - uuid = discovery_info["properties"].get("uuid") - if not uuid: + if not (uuid := discovery_info["properties"].get("uuid")): return self.async_abort(reason="no_uuid") self._discovery_name = discovery_info["name"] diff --git a/homeassistant/components/kodi/media_player.py b/homeassistant/components/kodi/media_player.py index 9d23a5fa212..9fd46de026c 100644 --- a/homeassistant/components/kodi/media_player.py +++ b/homeassistant/components/kodi/media_player.py @@ -533,9 +533,7 @@ class KodiEntity(MediaPlayerEntity): if self._properties.get("live"): return None - total_time = self._properties.get("totaltime") - - if total_time is None: + if (total_time := self._properties.get("totaltime")) is None: return None return ( @@ -547,9 +545,7 @@ class KodiEntity(MediaPlayerEntity): @property def media_position(self): """Position of current playing media in seconds.""" - time = self._properties.get("time") - - if time is None: + if (time := self._properties.get("time")) is None: return None return time["hours"] * 3600 + time["minutes"] * 60 + time["seconds"] @@ -562,8 +558,7 @@ class KodiEntity(MediaPlayerEntity): @property def media_image_url(self): """Image url of current playing media.""" - thumbnail = self._item.get("thumbnail") - if thumbnail is None: + if (thumbnail := self._item.get("thumbnail")) is None: return None return self._kodi.thumbnail_url(thumbnail) @@ -598,8 +593,7 @@ class KodiEntity(MediaPlayerEntity): @property def media_artist(self): """Artist of current playing media, music track only.""" - artists = self._item.get("artist", []) - if artists: + if artists := self._item.get("artist"): return artists[0] return None @@ -607,8 +601,7 @@ class KodiEntity(MediaPlayerEntity): @property def media_album_artist(self): """Album artist of current playing media, music track only.""" - artists = self._item.get("albumartist", []) - if artists: + if artists := self._item.get("albumartist"): return artists[0] return None diff --git a/homeassistant/components/linksys_smart/device_tracker.py b/homeassistant/components/linksys_smart/device_tracker.py index 3761c047997..0ccfe36da24 100644 --- a/homeassistant/components/linksys_smart/device_tracker.py +++ b/homeassistant/components/linksys_smart/device_tracker.py @@ -66,13 +66,11 @@ class LinksysSmartWifiDeviceScanner(DeviceScanner): result = data["responses"][0] devices = result["output"]["devices"] for device in devices: - macs = device["knownMACAddresses"] - if not macs: + if not (macs := device["knownMACAddresses"]): _LOGGER.warning("Skipping device without known MAC address") continue mac = macs[-1] - connections = device["connections"] - if not connections: + if not device["connections"]: _LOGGER.debug("Device %s is not connected", mac) continue diff --git a/homeassistant/components/lovelace/dashboard.py b/homeassistant/components/lovelace/dashboard.py index bb043028ae6..3c9fb03d863 100644 --- a/homeassistant/components/lovelace/dashboard.py +++ b/homeassistant/components/lovelace/dashboard.py @@ -123,9 +123,7 @@ class LovelaceStorage(LovelaceConfig): if self._data is None: await self._load() - config = self._data["config"] - - if config is None: + if (config := self._data["config"]) is None: raise ConfigNotFound return config diff --git a/homeassistant/components/minio/__init__.py b/homeassistant/components/minio/__init__.py index f33dd6c389c..bd83b2b1d04 100644 --- a/homeassistant/components/minio/__init__.py +++ b/homeassistant/components/minio/__init__.py @@ -183,8 +183,7 @@ class QueueListener(threading.Thread): """Listen to queue events, and forward them to Home Assistant event bus.""" _LOGGER.info("Running QueueListener") while True: - event = self._queue.get() - if event is None: + if (event := self._queue.get()) is None: break _, file_name = os.path.split(event[ATTR_KEY]) diff --git a/homeassistant/components/rfxtrx/binary_sensor.py b/homeassistant/components/rfxtrx/binary_sensor.py index 788c5dec436..914303f1468 100644 --- a/homeassistant/components/rfxtrx/binary_sensor.py +++ b/homeassistant/components/rfxtrx/binary_sensor.py @@ -109,9 +109,8 @@ async def async_setup_entry( discovery_info = config_entry.data def get_sensor_description(type_string: str): - description = SENSOR_TYPES_DICT.get(type_string) - if description is None: - description = BinarySensorEntityDescription(key=type_string) + if (description := SENSOR_TYPES_DICT.get(type_string)) is None: + return BinarySensorEntityDescription(key=type_string) return description for packet_id, entity_info in discovery_info[CONF_DEVICES].items(): diff --git a/homeassistant/components/rfxtrx/config_flow.py b/homeassistant/components/rfxtrx/config_flow.py index 01bcc6ea035..be4ec111390 100644 --- a/homeassistant/components/rfxtrx/config_flow.py +++ b/homeassistant/components/rfxtrx/config_flow.py @@ -386,9 +386,8 @@ class OptionsFlow(config_entries.OptionsFlow): def _can_replace_device(self, entry_id): """Check if device can be replaced with selected device.""" device_data = self._get_device_data(entry_id) - event_code = device_data[CONF_EVENT_CODE] - if event_code is not None: + if (event_code := device_data[CONF_EVENT_CODE]) is not None: rfx_obj = get_rfx_object(event_code) if ( rfx_obj.device.packettype @@ -452,8 +451,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors = {} if user_input is not None: - user_selection = user_input[CONF_TYPE] - if user_selection == "Serial": + if user_input[CONF_TYPE] == "Serial": return await self.async_step_setup_serial() return await self.async_step_setup_network() diff --git a/homeassistant/components/rfxtrx/sensor.py b/homeassistant/components/rfxtrx/sensor.py index 68c29e20328..afd1d6a12ce 100644 --- a/homeassistant/components/rfxtrx/sensor.py +++ b/homeassistant/components/rfxtrx/sensor.py @@ -305,12 +305,12 @@ class RfxtrxSensor(RfxtrxEntity, SensorEntity): """Restore device state.""" await super().async_added_to_hass() - if self._event is None: - old_state = await self.async_get_last_state() - if old_state is not None: - event = old_state.attributes.get(ATTR_EVENT) - if event: - self._apply_event(get_rfx_object(event)) + if ( + self._event is None + and (old_state := await self.async_get_last_state()) is not None + and (event := old_state.attributes.get(ATTR_EVENT)) + ): + self._apply_event(get_rfx_object(event)) @property def native_value(self): diff --git a/homeassistant/components/statistics/sensor.py b/homeassistant/components/statistics/sensor.py index ea90346fe7c..51f6478b4c0 100644 --- a/homeassistant/components/statistics/sensor.py +++ b/homeassistant/components/statistics/sensor.py @@ -147,8 +147,7 @@ class StatisticsSensor(SensorEntity): @callback def async_stats_sensor_state_listener(event): """Handle the sensor state changes.""" - new_state = event.data.get("new_state") - if new_state is None: + if (new_state := event.data.get("new_state")) is None: return self._unit_of_measurement = new_state.attributes.get( diff --git a/homeassistant/components/synology_srm/device_tracker.py b/homeassistant/components/synology_srm/device_tracker.py index f97b1735e21..5981686e9b4 100644 --- a/homeassistant/components/synology_srm/device_tracker.py +++ b/homeassistant/components/synology_srm/device_tracker.py @@ -112,8 +112,7 @@ class SynologySrmDeviceScanner(DeviceScanner): if not device: return filtered_attributes for attribute, alias in ATTRIBUTE_ALIAS.items(): - value = device.get(attribute) - if value is None: + if (value := device.get(attribute)) is None: continue attr = alias or attribute filtered_attributes[attr] = value diff --git a/homeassistant/components/tensorflow/image_processing.py b/homeassistant/components/tensorflow/image_processing.py index f4e90a83154..323dbcb7882 100644 --- a/homeassistant/components/tensorflow/image_processing.py +++ b/homeassistant/components/tensorflow/image_processing.py @@ -247,8 +247,7 @@ class TensorFlowImageProcessor(ImageProcessingEntity): # Handle global detection area self._area = [0, 0, 1, 1] - area_config = model_config.get(CONF_AREA) - if area_config: + if area_config := model_config.get(CONF_AREA): self._area = [ area_config.get(CONF_TOP), area_config.get(CONF_LEFT), @@ -334,8 +333,7 @@ class TensorFlowImageProcessor(ImageProcessingEntity): def process_image(self, image): """Process the image.""" - model = self.hass.data[DOMAIN][CONF_MODEL] - if not model: + if not (model := self.hass.data[DOMAIN][CONF_MODEL]): _LOGGER.debug("Model not yet ready") return diff --git a/homeassistant/components/xbee/__init__.py b/homeassistant/components/xbee/__init__.py index 5ca9e4ef6f7..299d7052934 100644 --- a/homeassistant/components/xbee/__init__.py +++ b/homeassistant/components/xbee/__init__.py @@ -115,9 +115,8 @@ class XBeeConfig: If an address has been provided, unhexlify it, otherwise return None as we're talking to our local XBee device. """ - address = self._config.get("address") - if address is not None: - address = unhexlify(address) + if (address := self._config.get("address")) is not None: + return unhexlify(address) return address @property