diff --git a/homeassistant/components/darksky/weather.py b/homeassistant/components/darksky/weather.py index 0ad448ddfbd..b5aafd24c3d 100644 --- a/homeassistant/components/darksky/weather.py +++ b/homeassistant/components/darksky/weather.py @@ -91,8 +91,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): name = config.get(CONF_NAME) mode = config.get(CONF_MODE) - units = config.get(CONF_UNITS) - if not units: + if not (units := config.get(CONF_UNITS)): units = "ca" if hass.config.units.is_metric else "us" dark_sky = DarkSkyData(config.get(CONF_API_KEY), latitude, longitude, units) diff --git a/homeassistant/components/edl21/sensor.py b/homeassistant/components/edl21/sensor.py index 407f5902198..d9997a9c1e3 100644 --- a/homeassistant/components/edl21/sensor.py +++ b/homeassistant/components/edl21/sensor.py @@ -178,8 +178,7 @@ class EDL21: new_entities = [] for telegram in message_body.get("valList", []): - obis = telegram.get("objName") - if not obis: + if not (obis := telegram.get("objName")): continue if (electricity_id, obis) in self._registered_obis: @@ -187,8 +186,7 @@ class EDL21: self._hass, SIGNAL_EDL21_TELEGRAM, electricity_id, telegram ) else: - name = self._OBIS_NAMES.get(obis) - if name: + if name := self._OBIS_NAMES.get(obis): if self._name: name = f"{self._name}: {name}" new_entities.append( diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 9ebc29a683f..2c93f9db61e 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -399,8 +399,7 @@ def _async_register_events_and_services(hass: HomeAssistant): referenced = async_extract_referenced_entity_ids(hass, service) dev_reg = device_registry.async_get(hass) for device_id in referenced.referenced_devices: - dev_reg_ent = dev_reg.async_get(device_id) - if not dev_reg_ent: + if not (dev_reg_ent := dev_reg.async_get(device_id)): raise HomeAssistantError(f"No device found for device id: {device_id}") macs = [ cval @@ -697,8 +696,7 @@ class HomeKit: if not self._filter(entity_id): continue - ent_reg_ent = ent_reg.async_get(entity_id) - if ent_reg_ent: + if ent_reg_ent := ent_reg.async_get(entity_id): await self._async_set_device_info_attributes( ent_reg_ent, dev_reg, entity_id ) diff --git a/homeassistant/components/homekit/accessories.py b/homeassistant/components/homekit/accessories.py index 3b7f2c0f9eb..30ee2e72589 100644 --- a/homeassistant/components/homekit/accessories.py +++ b/homeassistant/components/homekit/accessories.py @@ -413,8 +413,7 @@ class HomeAccessory(Accessory): @ha_callback def async_update_linked_battery_callback(self, event): """Handle linked battery sensor state change listener callback.""" - 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.linked_battery_charging_sensor: battery_charging_state = None @@ -425,8 +424,7 @@ class HomeAccessory(Accessory): @ha_callback def async_update_linked_battery_charging_callback(self, event): """Handle linked battery charging sensor state change listener callback.""" - new_state = event.data.get("new_state") - if new_state is None: + if (new_state := event.data.get("new_state")) is None: return self.async_update_battery(None, new_state.state == STATE_ON) @@ -524,8 +522,7 @@ class HomeBridge(Bridge): async def async_get_snapshot(self, info): """Get snapshot from accessory if supported.""" - acc = self.accessories.get(info["aid"]) - if acc is None: + if (acc := self.accessories.get(info["aid"])) is None: raise ValueError("Requested snapshot for missing accessory") if not hasattr(acc, "async_get_snapshot"): raise ValueError( diff --git a/homeassistant/components/homekit/type_cameras.py b/homeassistant/components/homekit/type_cameras.py index 2cdcd600932..6cf8735b075 100644 --- a/homeassistant/components/homekit/type_cameras.py +++ b/homeassistant/components/homekit/type_cameras.py @@ -314,8 +314,7 @@ class Camera(HomeAccessory, PyhapCamera): async def _async_get_stream_source(self): """Find the camera stream source url.""" - stream_source = self.config.get(CONF_STREAM_SOURCE) - if stream_source: + if stream_source := self.config.get(CONF_STREAM_SOURCE): return stream_source try: stream_source = await self.hass.components.camera.async_get_stream_source( @@ -447,8 +446,7 @@ class Camera(HomeAccessory, PyhapCamera): async def stop_stream(self, session_info): """Stop the stream for the given ``session_id``.""" session_id = session_info["id"] - stream = session_info.get("stream") - if not stream: + if not (stream := session_info.get("stream")): _LOGGER.debug("No stream for session ID %s", session_id) return diff --git a/homeassistant/components/homekit/type_media_players.py b/homeassistant/components/homekit/type_media_players.py index 7be1b98dcdb..61c043ebcaa 100644 --- a/homeassistant/components/homekit/type_media_players.py +++ b/homeassistant/components/homekit/type_media_players.py @@ -303,8 +303,7 @@ class TelevisionMediaPlayer(RemoteInputSelectAccessory): def set_remote_key(self, value): """Send remote key value if call came from HomeKit.""" _LOGGER.debug("%s: Set remote key to %s", self.entity_id, value) - key_name = REMOTE_KEYS.get(value) - if key_name is None: + if (key_name := REMOTE_KEYS.get(value)) is None: _LOGGER.warning("%s: Unhandled key press for %s", self.entity_id, value) return diff --git a/homeassistant/components/homekit/type_remotes.py b/homeassistant/components/homekit/type_remotes.py index 53659adef77..41a76ca7fed 100644 --- a/homeassistant/components/homekit/type_remotes.py +++ b/homeassistant/components/homekit/type_remotes.py @@ -207,8 +207,7 @@ class ActivityRemote(RemoteInputSelectAccessory): def set_remote_key(self, value): """Send remote key value if call came from HomeKit.""" _LOGGER.debug("%s: Set remote key to %s", self.entity_id, value) - key_name = REMOTE_KEYS.get(value) - if key_name is None: + if (key_name := REMOTE_KEYS.get(value)) is None: _LOGGER.warning("%s: Unhandled key press for %s", self.entity_id, value) return self.hass.bus.async_fire( diff --git a/homeassistant/components/homekit/type_thermostats.py b/homeassistant/components/homekit/type_thermostats.py index c75cc95b169..804f0b86167 100644 --- a/homeassistant/components/homekit/type_thermostats.py +++ b/homeassistant/components/homekit/type_thermostats.py @@ -462,8 +462,7 @@ class Thermostat(HomeAccessory): ) # Set current operation mode for supported thermostats - hvac_action = new_state.attributes.get(ATTR_HVAC_ACTION) - if hvac_action: + if hvac_action := new_state.attributes.get(ATTR_HVAC_ACTION): homekit_hvac_action = HC_HASS_TO_HOMEKIT_ACTION[hvac_action] self.char_current_heat_cool.set_value(homekit_hvac_action) @@ -575,8 +574,7 @@ class WaterHeater(HomeAccessory): def set_heat_cool(self, value): """Change operation mode to value if call came from HomeKit.""" _LOGGER.debug("%s: Set heat-cool to %d", self.entity_id, value) - hass_value = HC_HOMEKIT_TO_HASS[value] - if hass_value != HVAC_MODE_HEAT: + if HC_HOMEKIT_TO_HASS[value] != HVAC_MODE_HEAT: self.char_target_heat_cool.set_value(1) # Heat def set_target_temperature(self, value): @@ -615,14 +613,12 @@ class WaterHeater(HomeAccessory): def _get_temperature_range_from_state(state, unit, default_min, default_max): """Calculate the temperature range from a state.""" - min_temp = state.attributes.get(ATTR_MIN_TEMP) - if min_temp: + if min_temp := state.attributes.get(ATTR_MIN_TEMP): min_temp = round(temperature_to_homekit(min_temp, unit) * 2) / 2 else: min_temp = default_min - max_temp = state.attributes.get(ATTR_MAX_TEMP) - if max_temp: + if max_temp := state.attributes.get(ATTR_MAX_TEMP): max_temp = round(temperature_to_homekit(max_temp, unit) * 2) / 2 else: max_temp = default_max diff --git a/homeassistant/components/imap_email_content/sensor.py b/homeassistant/components/imap_email_content/sensor.py index 87c18a56bbe..e2dab5b9ef2 100644 --- a/homeassistant/components/imap_email_content/sensor.py +++ b/homeassistant/components/imap_email_content/sensor.py @@ -55,8 +55,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): config.get(CONF_FOLDER), ) - value_template = config.get(CONF_VALUE_TEMPLATE) - if value_template is not None: + if (value_template := config.get(CONF_VALUE_TEMPLATE)) is not None: value_template.hass = hass sensor = EmailContentSensor( hass, diff --git a/homeassistant/components/manual_mqtt/alarm_control_panel.py b/homeassistant/components/manual_mqtt/alarm_control_panel.py index 2fa0e631c1d..a94b1013782 100644 --- a/homeassistant/components/manual_mqtt/alarm_control_panel.py +++ b/homeassistant/components/manual_mqtt/alarm_control_panel.py @@ -450,8 +450,7 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity): async def _async_state_changed_listener(self, event): """Publish state change to MQTT.""" - new_state = event.data.get("new_state") - if new_state is None: + if (new_state := event.data.get("new_state")) is None: return mqtt.async_publish( self.hass, self._state_topic, new_state.state, self._qos, True diff --git a/homeassistant/components/philips_js/media_player.py b/homeassistant/components/philips_js/media_player.py index 4499fb61e2a..1b2d5c25fd4 100644 --- a/homeassistant/components/philips_js/media_player.py +++ b/homeassistant/components/philips_js/media_player.py @@ -187,8 +187,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity): async def async_select_source(self, source): """Set the input source.""" - source_id = _inverted(self._sources).get(source) - if source_id: + if source_id := _inverted(self._sources).get(source): await self._tv.setSource(source_id) await self._async_update_soon() @@ -316,8 +315,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity): @property def app_name(self): """Name of the current running app.""" - app = self._tv.applications.get(self._tv.application_id) - if app: + if app := self._tv.applications.get(self._tv.application_id): return app.get("label") @property @@ -350,8 +348,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity): else: _LOGGER.error("Unable to find channel <%s>", media_id) elif media_type == MEDIA_TYPE_APP: - app = self._tv.applications.get(media_id) - if app: + if app := self._tv.applications.get(media_id): await self._tv.setApplication(app["intent"]) await self._async_update_soon() else: diff --git a/homeassistant/components/somfy/climate.py b/homeassistant/components/somfy/climate.py index d4461e91e1b..88451f5b7e4 100644 --- a/homeassistant/components/somfy/climate.py +++ b/homeassistant/components/somfy/climate.py @@ -94,8 +94,7 @@ class SomfyClimate(SomfyEntity, ClimateEntity): def set_temperature(self, **kwargs) -> None: """Set new target temperature.""" - temperature = kwargs.get(ATTR_TEMPERATURE) - if temperature is None: + if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: return self._climate.set_target(TargetMode.MANUAL, temperature, DurationType.NEXT_MODE) diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 52b7c4aa034..00e1dae6a8c 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -427,8 +427,7 @@ class SpeechManager: This method is a coroutine. """ - filename = self.file_cache.get(key) - if not filename: + if not (filename := self.file_cache.get(key)): raise HomeAssistantError(f"Key {key} not in file cache!") voice_file = os.path.join(self.cache_dir, filename) diff --git a/homeassistant/components/vera/__init__.py b/homeassistant/components/vera/__init__.py index b6e13eb3832..3b7c11b943e 100644 --- a/homeassistant/components/vera/__init__.py +++ b/homeassistant/components/vera/__init__.py @@ -68,9 +68,7 @@ async def async_setup(hass: HomeAssistant, base_config: ConfigType) -> bool: """Set up for Vera controllers.""" hass.data[DOMAIN] = {} - config = base_config.get(DOMAIN) - - if not config: + if not (config := base_config.get(DOMAIN)): return True hass.async_create_task( diff --git a/homeassistant/components/wemo/light.py b/homeassistant/components/wemo/light.py index 4339e964b62..980675bd4ff 100644 --- a/homeassistant/components/wemo/light.py +++ b/homeassistant/components/wemo/light.py @@ -117,8 +117,7 @@ class WemoLight(WemoEntity, LightEntity): @property def hs_color(self): """Return the hs color values of this light.""" - xy_color = self.light.state.get("color_xy") - if xy_color: + if xy_color := self.light.state.get("color_xy"): return color_util.color_xy_to_hs(*xy_color) return None diff --git a/homeassistant/components/youless/sensor.py b/homeassistant/components/youless/sensor.py index d4465bd0c09..16ed918914d 100644 --- a/homeassistant/components/youless/sensor.py +++ b/homeassistant/components/youless/sensor.py @@ -35,7 +35,7 @@ async def async_setup_entry( """Initialize the integration.""" coordinator = hass.data[DOMAIN][entry.entry_id] device = entry.data[CONF_DEVICE] - if device is None: + if (device := entry.data[CONF_DEVICE]) is None: device = entry.entry_id async_add_entities( diff --git a/homeassistant/components/zhong_hong/climate.py b/homeassistant/components/zhong_hong/climate.py index f20c9f7e328..ad7b81dfe7b 100644 --- a/homeassistant/components/zhong_hong/climate.py +++ b/homeassistant/components/zhong_hong/climate.py @@ -239,12 +239,10 @@ class ZhongHongClimate(ClimateEntity): def set_temperature(self, **kwargs): """Set new target temperature.""" - temperature = kwargs.get(ATTR_TEMPERATURE) - if temperature is not None: + if (temperature := kwargs.get(ATTR_TEMPERATURE)) is not None: self._device.set_temperature(temperature) - operation_mode = kwargs.get(ATTR_HVAC_MODE) - if operation_mode is not None: + if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None: self.set_hvac_mode(operation_mode) def set_hvac_mode(self, hvac_mode):