From 4f8148f9eab51b717594bec0aaa034e68fd46cc4 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 17 Oct 2021 20:24:34 +0200 Subject: [PATCH] Use assignment expressions 07 (#57787) --- homeassistant/components/ihc/light.py | 3 +-- homeassistant/components/influxdb/sensor.py | 3 +-- .../components/insteon/insteon_entity.py | 3 +-- homeassistant/components/ipp/sensor.py | 4 +-- homeassistant/components/isy994/sensor.py | 3 +-- homeassistant/components/izone/climate.py | 3 +-- homeassistant/components/kodi/media_player.py | 3 +-- homeassistant/components/lifx/light.py | 3 +-- homeassistant/components/light/__init__.py | 4 +-- homeassistant/components/lock/__init__.py | 3 +-- homeassistant/components/logbook/__init__.py | 9 +++---- .../components/media_player/__init__.py | 26 ++++++++----------- homeassistant/components/melcloud/__init__.py | 3 +-- homeassistant/components/melcloud/climate.py | 6 ++--- .../components/melcloud/config_flow.py | 3 +-- .../components/mqtt/light/schema_basic.py | 9 +++---- homeassistant/components/mvglive/sensor.py | 3 +-- homeassistant/components/nest/__init__.py | 3 +-- homeassistant/components/nest/camera_sdm.py | 3 +-- homeassistant/components/nest/climate_sdm.py | 9 +++---- homeassistant/components/nest/device_info.py | 3 +-- homeassistant/components/nut/config_flow.py | 3 +-- 22 files changed, 39 insertions(+), 73 deletions(-) diff --git a/homeassistant/components/ihc/light.py b/homeassistant/components/ihc/light.py index c8fe9ef54de..d4a3bd8b1f3 100644 --- a/homeassistant/components/ihc/light.py +++ b/homeassistant/components/ihc/light.py @@ -84,8 +84,7 @@ class IhcLight(IHCDevice, LightEntity): if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] else: - brightness = self._brightness - if brightness == 0: + if (brightness := self._brightness) == 0: brightness = 255 if self._dimmable: diff --git a/homeassistant/components/influxdb/sensor.py b/homeassistant/components/influxdb/sensor.py index bdbfafaf790..24ef6a1020e 100644 --- a/homeassistant/components/influxdb/sensor.py +++ b/homeassistant/components/influxdb/sensor.py @@ -234,8 +234,7 @@ class InfluxSensor(SensorEntity): def update(self): """Get the latest data from Influxdb and updates the states.""" self.data.update() - value = self.data.value - if value is None: + if (value := self.data.value) is None: value = STATE_UNKNOWN if self._value_template is not None: value = self._value_template.render_with_possible_json_value( diff --git a/homeassistant/components/insteon/insteon_entity.py b/homeassistant/components/insteon/insteon_entity.py index 3f83440d690..e555725a188 100644 --- a/homeassistant/components/insteon/insteon_entity.py +++ b/homeassistant/components/insteon/insteon_entity.py @@ -65,8 +65,7 @@ class InsteonEntity(Entity): def name(self): """Return the name of the node (used for Entity_ID).""" # Set a base description - description = self._insteon_device.description - if description is None: + if (description := self._insteon_device.description) is None: description = "Unknown Device" # Get an extension label if there is one extension = self._get_label() diff --git a/homeassistant/components/ipp/sensor.py b/homeassistant/components/ipp/sensor.py index e7c0d5c38f5..02e2082bbd3 100644 --- a/homeassistant/components/ipp/sensor.py +++ b/homeassistant/components/ipp/sensor.py @@ -36,9 +36,7 @@ async def async_setup_entry( coordinator: IPPDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] # config flow sets this to either UUID, serial number or None - unique_id = entry.unique_id - - if unique_id is None: + if (unique_id := entry.unique_id) is None: unique_id = entry.entry_id sensors = [] diff --git a/homeassistant/components/isy994/sensor.py b/homeassistant/components/isy994/sensor.py index f12f3cb6bdd..c15da2af0da 100644 --- a/homeassistant/components/isy994/sensor.py +++ b/homeassistant/components/isy994/sensor.py @@ -69,8 +69,7 @@ class ISYSensorEntity(ISYNodeEntity, SensorEntity): @property def native_value(self) -> str: """Get the state of the ISY994 sensor device.""" - value = self._node.status - if value == ISY_VALUE_UNKNOWN: + if (value := self._node.status) == ISY_VALUE_UNKNOWN: return None # Get the translated ISY Unit of Measurement diff --git a/homeassistant/components/izone/climate.py b/homeassistant/components/izone/climate.py index 968f13748b2..06dfee0e7fb 100644 --- a/homeassistant/components/izone/climate.py +++ b/homeassistant/components/izone/climate.py @@ -316,8 +316,7 @@ class ControllerDevice(ClimateEntity): """Return current operation ie. heat, cool, idle.""" if not self._controller.is_on: return HVAC_MODE_OFF - mode = self._controller.mode - if mode == Controller.Mode.FREE_AIR: + if (mode := self._controller.mode) == Controller.Mode.FREE_AIR: return HVAC_MODE_FAN_ONLY for (key, value) in self._state_to_pizone.items(): if value == mode: diff --git a/homeassistant/components/kodi/media_player.py b/homeassistant/components/kodi/media_player.py index 42943cffb13..9d23a5fa212 100644 --- a/homeassistant/components/kodi/media_player.py +++ b/homeassistant/components/kodi/media_player.py @@ -238,8 +238,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): connection = data[DATA_CONNECTION] kodi = data[DATA_KODI] name = config_entry.data[CONF_NAME] - uid = config_entry.unique_id - if uid is None: + if (uid := config_entry.unique_id) is None: uid = config_entry.entry_id entity = KodiEntity(connection, kodi, name, uid) diff --git a/homeassistant/components/lifx/light.py b/homeassistant/components/lifx/light.py index a4412d042a8..48896cea8a5 100644 --- a/homeassistant/components/lifx/light.py +++ b/homeassistant/components/lifx/light.py @@ -462,8 +462,7 @@ class LIFXLight(LightEntity): "manufacturer": "LIFX", } - version = self.bulb.host_firmware_version - if version is not None: + if (version := self.bulb.host_firmware_version) is not None: info["sw_version"] = version product_map = aiolifx().products.product_map diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index e195ab0f09b..a09a2fcd58e 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -675,9 +675,7 @@ class LightEntity(ToggleEntity): @property def _light_internal_color_mode(self) -> str: """Return the color mode of the light with backwards compatibility.""" - color_mode = self.color_mode - - if color_mode is None: + if (color_mode := self.color_mode) is None: # Backwards compatibility for color_mode added in 2021.4 # Add warning in 2021.6, remove in 2021.10 supported = self._light_internal_supported_color_modes diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index 860778d61f3..aa3662da0c8 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -177,8 +177,7 @@ class LockEntity(Entity): return STATE_LOCKING if self.is_unlocking: return STATE_UNLOCKING - locked = self.is_locked - if locked is None: + if (locked := self.is_locked) is None: return None return STATE_LOCKED if locked else STATE_UNLOCKED diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index 9bab6d0812e..1dc13c9fb14 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -315,8 +315,7 @@ def humanify(hass, events, entity_attr_cache, context_lookup): "entity_id": entity_id, } - icon = event.attributes_icon - if icon: + if icon := event.attributes_icon: data["icon"] = icon if event.context_user_id: @@ -581,8 +580,7 @@ def _keep_event(hass, event, entities_filter): if event.event_type in HOMEASSISTANT_EVENTS: return entities_filter is None or entities_filter(HA_DOMAIN_ENTITY_ID) - entity_id = event.data_entity_id - if entity_id: + if entity_id := event.data_entity_id: return entities_filter is None or entities_filter(entity_id) if event.event_type in hass.data[DOMAIN]: @@ -615,10 +613,9 @@ def _augment_data_with_context( return event_type = context_event.event_type - context_entity_id = context_event.entity_id # State change - if context_entity_id: + if context_entity_id := context_event.entity_id: data["context_entity_id"] = context_entity_id data["context_entity_id_name"] = _entity_name_from_event( context_entity_id, context_event, entity_attr_cache diff --git a/homeassistant/components/media_player/__init__.py b/homeassistant/components/media_player/__init__.py index c84ccdfc105..8cf271d09cf 100644 --- a/homeassistant/components/media_player/__init__.py +++ b/homeassistant/components/media_player/__init__.py @@ -482,16 +482,14 @@ class MediaPlayerEntity(Entity): if hasattr(self, "_attr_media_image_hash"): return self._attr_media_image_hash - url = self.media_image_url - if url is not None: + if (url := self.media_image_url) is not None: return hashlib.sha256(url.encode("utf-8")).hexdigest()[:16] return None async def async_get_media_image(self): """Fetch media image of current playing image.""" - url = self.media_image_url - if url is None: + if (url := self.media_image_url) is None: return None, None return await self._async_fetch_image_from_cache(url) @@ -871,9 +869,7 @@ class MediaPlayerEntity(Entity): @property def media_image_local(self): """Return local url to media image.""" - image_hash = self.media_image_hash - - if image_hash is None: + if (image_hash := self.media_image_hash) is None: return None return ( @@ -887,15 +883,15 @@ class MediaPlayerEntity(Entity): supported_features = self.supported_features or 0 data = {} - if supported_features & SUPPORT_SELECT_SOURCE: - source_list = self.source_list - if source_list: - data[ATTR_INPUT_SOURCE_LIST] = source_list + if supported_features & SUPPORT_SELECT_SOURCE and ( + source_list := self.source_list + ): + data[ATTR_INPUT_SOURCE_LIST] = source_list - if supported_features & SUPPORT_SELECT_SOUND_MODE: - sound_mode_list = self.sound_mode_list - if sound_mode_list: - data[ATTR_SOUND_MODE_LIST] = sound_mode_list + if supported_features & SUPPORT_SELECT_SOUND_MODE and ( + sound_mode_list := self.sound_mode_list + ): + data[ATTR_SOUND_MODE_LIST] = sound_mode_list return data diff --git a/homeassistant/components/melcloud/__init__.py b/homeassistant/components/melcloud/__init__.py index 69efa26ac44..4547e690eb8 100644 --- a/homeassistant/components/melcloud/__init__.py +++ b/homeassistant/components/melcloud/__init__.py @@ -134,8 +134,7 @@ class MelCloudDevice: "manufacturer": "Mitsubishi Electric", "name": self.name, } - unit_infos = self.device.units - if unit_infos is not None: + if (unit_infos := self.device.units) is not None: _device_info["model"] = ", ".join( [x["model"] for x in unit_infos if x["model"]] ) diff --git a/homeassistant/components/melcloud/climate.py b/homeassistant/components/melcloud/climate.py index d4eeb9354c8..25165cf1a71 100644 --- a/homeassistant/components/melcloud/climate.py +++ b/homeassistant/components/melcloud/climate.py @@ -143,8 +143,7 @@ class AtaDeviceClimate(MelCloudClimate): """Return the optional state attributes with device specific additions.""" attr = {} - vane_horizontal = self._device.vane_horizontal - if vane_horizontal: + if vane_horizontal := self._device.vane_horizontal: attr.update( { ATTR_VANE_HORIZONTAL: vane_horizontal, @@ -152,8 +151,7 @@ class AtaDeviceClimate(MelCloudClimate): } ) - vane_vertical = self._device.vane_vertical - if vane_vertical: + if vane_vertical := self._device.vane_vertical: attr.update( { ATTR_VANE_VERTICAL: vane_vertical, diff --git a/homeassistant/components/melcloud/config_flow.py b/homeassistant/components/melcloud/config_flow.py index 48ee84382fa..a04364ea20f 100644 --- a/homeassistant/components/melcloud/config_flow.py +++ b/homeassistant/components/melcloud/config_flow.py @@ -48,8 +48,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): try: with timeout(10): - acquired_token = token - if acquired_token is None: + if (acquired_token := token) is None: acquired_token = await pymelcloud.login( username, password, diff --git a/homeassistant/components/mqtt/light/schema_basic.py b/homeassistant/components/mqtt/light/schema_basic.py index 9a900c296d3..409eb9d648c 100644 --- a/homeassistant/components/mqtt/light/schema_basic.py +++ b/homeassistant/components/mqtt/light/schema_basic.py @@ -656,8 +656,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity): @property def brightness(self): """Return the brightness of this light between 0..255.""" - brightness = self._brightness - if brightness: + if brightness := self._brightness: brightness = min(round(brightness), 255) return brightness @@ -728,10 +727,8 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity): @property def white_value(self): """Return the white property.""" - white_value = self._white_value - if white_value: - white_value = min(round(white_value), 255) - return white_value + if white_value := self._white_value: + return min(round(white_value), 255) return None @property diff --git a/homeassistant/components/mvglive/sensor.py b/homeassistant/components/mvglive/sensor.py index 416ce21cbaf..29554e89c5d 100644 --- a/homeassistant/components/mvglive/sensor.py +++ b/homeassistant/components/mvglive/sensor.py @@ -115,8 +115,7 @@ class MVGLiveSensor(SensorEntity): @property def extra_state_attributes(self): """Return the state attributes.""" - dep = self.data.departures - if not dep: + if not (dep := self.data.departures): return None attr = dep[0] # next depature attributes attr["departures"] = deepcopy(dep) # all departures dictionary diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index ff340d38424..9ca7d530f79 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -115,8 +115,7 @@ class SignalUpdateCallback: if not event_message.resource_update_name: return device_id = event_message.resource_update_name - events = event_message.resource_update_events - if not events: + if not (events := event_message.resource_update_events): return _LOGGER.debug("Event Update %s", events.keys()) device_registry = await self._hass.helpers.device_registry.async_get_registry() diff --git a/homeassistant/components/nest/camera_sdm.py b/homeassistant/components/nest/camera_sdm.py index 5620653819f..9ce485cee56 100644 --- a/homeassistant/components/nest/camera_sdm.py +++ b/homeassistant/components/nest/camera_sdm.py @@ -217,8 +217,7 @@ class NestCamera(Camera): """Return image from any active events happening.""" if CameraEventImageTrait.NAME not in self._device.traits: return None - trait = self._device.active_event_trait - if not trait: + if not (trait := self._device.active_event_trait): return None # Reuse image bytes if they have already been fetched if not isinstance(trait, EventImageGenerator): diff --git a/homeassistant/components/nest/climate_sdm.py b/homeassistant/components/nest/climate_sdm.py index 04954cc7a07..fe1c3034fc8 100644 --- a/homeassistant/components/nest/climate_sdm.py +++ b/homeassistant/components/nest/climate_sdm.py @@ -153,8 +153,7 @@ class ThermostatEntity(ClimateEntity): @property def target_temperature(self) -> float | None: """Return the temperature currently set to be reached.""" - trait = self._target_temperature_trait - if not trait: + if not (trait := self._target_temperature_trait): return None if self.hvac_mode == HVAC_MODE_HEAT: return trait.heat_celsius @@ -167,8 +166,7 @@ class ThermostatEntity(ClimateEntity): """Return the upper bound target temperature.""" if self.hvac_mode != HVAC_MODE_HEAT_COOL: return None - trait = self._target_temperature_trait - if not trait: + if not (trait := self._target_temperature_trait): return None return trait.cool_celsius @@ -177,8 +175,7 @@ class ThermostatEntity(ClimateEntity): """Return the lower bound target temperature.""" if self.hvac_mode != HVAC_MODE_HEAT_COOL: return None - trait = self._target_temperature_trait - if not trait: + if not (trait := self._target_temperature_trait): return None return trait.heat_celsius diff --git a/homeassistant/components/nest/device_info.py b/homeassistant/components/nest/device_info.py index 383c6d22258..605f3c48446 100644 --- a/homeassistant/components/nest/device_info.py +++ b/homeassistant/components/nest/device_info.py @@ -48,8 +48,7 @@ class NestDeviceInfo: return trait.custom_name # Build a name from the room/structure. Note: This room/structure name # is not associated with a home assistant Area. - parent_relations = self._device.parent_relations - if parent_relations: + if parent_relations := self._device.parent_relations: items = sorted(parent_relations.items()) names = [name for id, name in items] return " ".join(names) diff --git a/homeassistant/components/nut/config_flow.py b/homeassistant/components/nut/config_flow.py index 9b45e270448..ed490eddd37 100644 --- a/homeassistant/components/nut/config_flow.py +++ b/homeassistant/components/nut/config_flow.py @@ -88,8 +88,7 @@ async def validate_input(hass: core.HomeAssistant, data): data = PyNUTData(host, port, alias, username, password) await hass.async_add_executor_job(data.update) - status = data.status - if not status: + if not (status := data.status): raise CannotConnect return {"ups_list": data.ups_list, "available_resources": status}