diff --git a/homeassistant/components/air_quality/__init__.py b/homeassistant/components/air_quality/__init__.py index 1e38bad55a8..d0aa1fd4a76 100644 --- a/homeassistant/components/air_quality/__init__.py +++ b/homeassistant/components/air_quality/__init__.py @@ -137,8 +137,7 @@ class AirQualityEntity(Entity): data: dict[str, str | int | float] = {} for prop, attr in PROP_TO_ATTR.items(): - value = getattr(self, prop) - if value is not None: + if (value := getattr(self, prop)) is not None: data[attr] = value return data diff --git a/homeassistant/components/asuswrt/__init__.py b/homeassistant/components/asuswrt/__init__.py index 2d067d0e608..534ce6bd7df 100644 --- a/homeassistant/components/asuswrt/__init__.py +++ b/homeassistant/components/asuswrt/__init__.py @@ -92,8 +92,7 @@ async def async_setup(hass, config): return True # remove not required config keys - pub_key = conf.pop(CONF_PUB_KEY, "") - if pub_key: + if pub_key := conf.pop(CONF_PUB_KEY, ""): conf[CONF_SSH_KEY] = pub_key conf.pop(CONF_REQUIRE_IP, True) diff --git a/homeassistant/components/coronavirus/sensor.py b/homeassistant/components/coronavirus/sensor.py index 92fdf232214..14f597299cf 100644 --- a/homeassistant/components/coronavirus/sensor.py +++ b/homeassistant/components/coronavirus/sensor.py @@ -58,8 +58,7 @@ class CoronavirusSensor(CoordinatorEntity, SensorEntity): if self.country == OPTION_WORLDWIDE: sum_cases = 0 for case in self.coordinator.data.values(): - value = getattr(case, self.info_type) - if value is None: + if (value := getattr(case, self.info_type)) is None: continue sum_cases += value diff --git a/homeassistant/components/darksky/sensor.py b/homeassistant/components/darksky/sensor.py index 228370be16a..3de47136d45 100644 --- a/homeassistant/components/darksky/sensor.py +++ b/homeassistant/components/darksky/sensor.py @@ -754,10 +754,9 @@ class DarkSkySensor(SensorEntity): """ sensor_type = self.entity_description.key lookup_type = convert_to_camel(sensor_type) - state = getattr(data, lookup_type, None) - if state is None: - return state + if (state := getattr(data, lookup_type, None)) is None: + return None if "summary" in sensor_type: self._icon = getattr(data, "icon", "") diff --git a/homeassistant/components/energy/sensor.py b/homeassistant/components/energy/sensor.py index bfcc2817633..0800b02330e 100644 --- a/homeassistant/components/energy/sensor.py +++ b/homeassistant/components/energy/sensor.py @@ -197,8 +197,7 @@ class SensorManager: ): return - current_entity = to_remove.pop(key, None) - if current_entity: + if current_entity := to_remove.pop(key, None): current_entity.update_config(config) return diff --git a/homeassistant/components/esphome/__init__.py b/homeassistant/components/esphome/__init__.py index 8965e5b9960..0c07d0732ef 100644 --- a/homeassistant/components/esphome/__init__.py +++ b/homeassistant/components/esphome/__init__.py @@ -456,8 +456,7 @@ async def _setup_services( for service in services: if service.key in old_services: # Already exists - matching = old_services.pop(service.key) - if matching != service: + if (matching := old_services.pop(service.key)) != service: # Need to re-register to_unregister.append(matching) to_register.append(service) diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index be7ded37dc2..bdb66b718e8 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -84,8 +84,7 @@ async def async_attach_trigger(hass, config, action, automation_info): def update_entity_trigger(entity_id, new_state=None): """Update the entity trigger for the entity_id.""" # If a listener was already set up for entity, remove it. - remove = entities.pop(entity_id, None) - if remove: + if remove := entities.pop(entity_id, None): remove() remove = None diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 9d8c3d04302..3e2a2a65d18 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -659,8 +659,7 @@ class HomeKit: async def async_remove_bridge_accessory(self, aid): """Try adding accessory to bridge if configured beforehand.""" - acc = self.bridge.accessories.pop(aid, None) - if acc: + if acc := self.bridge.accessories.pop(aid, None): await acc.stop() return acc diff --git a/homeassistant/components/homekit_controller/config_flow.py b/homeassistant/components/homekit_controller/config_flow.py index 02b4c396783..c8118f61b87 100644 --- a/homeassistant/components/homekit_controller/config_flow.py +++ b/homeassistant/components/homekit_controller/config_flow.py @@ -471,8 +471,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # available. Otherwise request a fresh copy from the API. # This removes the 'accessories' key from pairing_data at # the same time. - accessories = pairing_data.pop("accessories", None) - if not accessories: + if not (accessories := pairing_data.pop("accessories", None)): accessories = await pairing.list_accessories_and_characteristics() bridge_info = get_bridge_information(accessories) diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index b1261258bf4..0cc462ac1f4 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -218,8 +218,7 @@ class HomematicipBaseActionSensor(HomematicipGenericEntity, BinarySensorEntity): state_attr = super().extra_state_attributes for attr, attr_key in SAM_DEVICE_ATTRIBUTES.items(): - attr_value = getattr(self._device, attr, None) - if attr_value: + if attr_value := getattr(self._device, attr, None): state_attr[attr_key] = attr_value return state_attr @@ -490,8 +489,7 @@ class HomematicipSecurityZoneSensorGroup(HomematicipGenericEntity, BinarySensorE state_attr = super().extra_state_attributes for attr, attr_key in GROUP_ATTRIBUTES.items(): - attr_value = getattr(self._device, attr, None) - if attr_value: + if attr_value := getattr(self._device, attr, None): state_attr[attr_key] = attr_value window_state = getattr(self._device, "windowState", None) diff --git a/homeassistant/components/homematicip_cloud/generic_entity.py b/homeassistant/components/homematicip_cloud/generic_entity.py index d3a3b1e34a4..8bcea5d1435 100644 --- a/homeassistant/components/homematicip_cloud/generic_entity.py +++ b/homeassistant/components/homematicip_cloud/generic_entity.py @@ -238,16 +238,14 @@ class HomematicipGenericEntity(Entity): if isinstance(self._device, AsyncDevice): for attr, attr_key in DEVICE_ATTRIBUTES.items(): - attr_value = getattr(self._device, attr, None) - if attr_value: + if attr_value := getattr(self._device, attr, None): state_attr[attr_key] = attr_value state_attr[ATTR_IS_GROUP] = False if isinstance(self._device, AsyncGroup): for attr, attr_key in GROUP_ATTRIBUTES.items(): - attr_value = getattr(self._device, attr, None) - if attr_value: + if attr_value := getattr(self._device, attr, None): state_attr[attr_key] = attr_value state_attr[ATTR_IS_GROUP] = True diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index ae866bb42e2..d1c3f71a83f 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -282,8 +282,7 @@ class HomematicipIlluminanceSensor(HomematicipGenericEntity, SensorEntity): state_attr = super().extra_state_attributes for attr, attr_key in ILLUMINATION_DEVICE_ATTRIBUTES.items(): - attr_value = getattr(self._device, attr, None) - if attr_value: + if attr_value := getattr(self._device, attr, None): state_attr[attr_key] = attr_value return state_attr diff --git a/homeassistant/components/http/view.py b/homeassistant/components/http/view.py index bf8dc4b432b..6123f83563c 100644 --- a/homeassistant/components/http/view.py +++ b/homeassistant/components/http/view.py @@ -86,9 +86,7 @@ class HomeAssistantView: routes: list[AbstractRoute] = [] for method in ("get", "post", "delete", "put", "patch", "head", "options"): - handler = getattr(self, method, None) - - if not handler: + if not (handler := getattr(self, method, None)): continue handler = request_handler_factory(self, handler) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index c5ae88eaaa0..dcf4972706a 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -252,16 +252,14 @@ def preprocess_turn_on_alternatives(hass, params): if ATTR_PROFILE in params: hass.data[DATA_PROFILES].apply_profile(params.pop(ATTR_PROFILE), params) - color_name = params.pop(ATTR_COLOR_NAME, None) - if color_name is not None: + if (color_name := params.pop(ATTR_COLOR_NAME, None)) is not None: try: params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name) except ValueError: _LOGGER.warning("Got unknown color %s, falling back to white", color_name) params[ATTR_RGB_COLOR] = (255, 255, 255) - kelvin = params.pop(ATTR_KELVIN, None) - if kelvin is not None: + if (kelvin := params.pop(ATTR_KELVIN, None)) is not None: mired = color_util.color_temperature_kelvin_to_mired(kelvin) params[ATTR_COLOR_TEMP] = int(mired) diff --git a/homeassistant/components/lock/__init__.py b/homeassistant/components/lock/__init__.py index aa3662da0c8..1f2e87fc864 100644 --- a/homeassistant/components/lock/__init__.py +++ b/homeassistant/components/lock/__init__.py @@ -162,8 +162,7 @@ class LockEntity(Entity): """Return the state attributes.""" state_attr = {} for prop, attr in PROP_TO_ATTR.items(): - value = getattr(self, prop) - if value is not None: + if (value := getattr(self, prop)) is not None: state_attr[attr] = value return state_attr diff --git a/homeassistant/components/media_player/__init__.py b/homeassistant/components/media_player/__init__.py index 41ebd93f3b5..0b2a7959c13 100644 --- a/homeassistant/components/media_player/__init__.py +++ b/homeassistant/components/media_player/__init__.py @@ -905,8 +905,7 @@ class MediaPlayerEntity(Entity): return state_attr for attr in ATTR_TO_PROPERTY: - value = getattr(self, attr) - if value is not None: + if (value := getattr(self, attr)) is not None: state_attr[attr] = value if self.media_image_remotely_accessible: diff --git a/homeassistant/components/mqtt/debug_info.py b/homeassistant/components/mqtt/debug_info.py index f9bb3f1c91f..e462d76fa31 100644 --- a/homeassistant/components/mqtt/debug_info.py +++ b/homeassistant/components/mqtt/debug_info.py @@ -44,8 +44,7 @@ def log_messages( def add_subscription(hass, message_callback, subscription): """Prepare debug data for subscription.""" - entity_id = getattr(message_callback, "__entity_id", None) - if entity_id: + if entity_id := getattr(message_callback, "__entity_id", None): debug_info = hass.data.setdefault( DATA_MQTT_DEBUG_INFO, {"entities": {}, "triggers": {}} ) diff --git a/homeassistant/components/nest/legacy/sensor.py b/homeassistant/components/nest/legacy/sensor.py index f2c6670bf8b..7b3ba5ec2fe 100644 --- a/homeassistant/components/nest/legacy/sensor.py +++ b/homeassistant/components/nest/legacy/sensor.py @@ -210,8 +210,7 @@ class NestTempSensor(NestSensorDevice, SensorEntity): else: self._unit = TEMP_FAHRENHEIT - temp = getattr(self.device, self.variable) - if temp is None: + if (temp := getattr(self.device, self.variable)) is None: self._state = None if isinstance(temp, tuple): diff --git a/homeassistant/components/plant/__init__.py b/homeassistant/components/plant/__init__.py index 5e734c7ba62..fe5b4b2483b 100644 --- a/homeassistant/components/plant/__init__.py +++ b/homeassistant/components/plant/__init__.py @@ -238,8 +238,7 @@ class Plant(Entity): result = [] for sensor_name in self._sensormap.values(): params = self.READINGS[sensor_name] - value = getattr(self, f"_{sensor_name}") - if value is not None: + if (value := getattr(self, f"_{sensor_name}")) is not None: if value == STATE_UNAVAILABLE: result.append(f"{sensor_name} unavailable") else: diff --git a/homeassistant/components/plex/media_player.py b/homeassistant/components/plex/media_player.py index f210ebe8363..3bc0a080f8c 100644 --- a/homeassistant/components/plex/media_player.py +++ b/homeassistant/components/plex/media_player.py @@ -481,9 +481,7 @@ class PlexMediaPlayer(MediaPlayerEntity): if isinstance(src, int): src = {"plex_key": src} - playqueue_id = src.pop("playqueue_id", None) - - if playqueue_id: + if playqueue_id := src.pop("playqueue_id", None): try: playqueue = self.plex_server.get_playqueue(playqueue_id) except plexapi.exceptions.NotFound as err: @@ -518,8 +516,7 @@ class PlexMediaPlayer(MediaPlayerEntity): "media_summary", "username", ): - value = getattr(self, attr, None) - if value: + if value := getattr(self, attr, None): attributes[attr] = value return attributes diff --git a/homeassistant/components/plex/services.py b/homeassistant/components/plex/services.py index 32af3c429dc..e07d94f5a1f 100644 --- a/homeassistant/components/plex/services.py +++ b/homeassistant/components/plex/services.py @@ -107,8 +107,7 @@ def lookup_plex_media(hass, content_type, content_id): plex_server_name = content.pop("plex_server", None) plex_server = get_plex_server(hass, plex_server_name) - playqueue_id = content.pop("playqueue_id", None) - if playqueue_id: + if playqueue_id := content.pop("playqueue_id", None): try: playqueue = plex_server.get_playqueue(playqueue_id) except NotFound as err: diff --git a/homeassistant/components/repetier/__init__.py b/homeassistant/components/repetier/__init__.py index a71038ffcb8..4c5534d1a28 100644 --- a/homeassistant/components/repetier/__init__.py +++ b/homeassistant/components/repetier/__init__.py @@ -262,8 +262,7 @@ class PrinterAPI: printer = self.printers[printer_id] methods = API_PRINTER_METHODS[sensor_type] for prop, offline in methods.offline.items(): - state = getattr(printer, prop) - if state == offline: + if getattr(printer, prop) == offline: # if state matches offline, sensor is offline return None diff --git a/homeassistant/components/shelly/entity.py b/homeassistant/components/shelly/entity.py index e33a2d87317..f8383ccd297 100644 --- a/homeassistant/components/shelly/entity.py +++ b/homeassistant/components/shelly/entity.py @@ -433,9 +433,7 @@ class ShellyBlockAttributeEntity(ShellyBlockEntity, entity.Entity): @property def attribute_value(self) -> StateType: """Value of sensor.""" - value = getattr(self.block, self.attribute) - - if value is None: + if (value := getattr(self.block, self.attribute)) is None: return None return cast(StateType, self.description.value(value)) diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index 9f2bc829eac..f5389c7966a 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -663,8 +663,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): media_content_id, MEDIA_TYPES_TO_SONOS[media_content_type], ) - image_url = getattr(item, "album_art_uri", None) - if image_url: + if image_url := getattr(item, "album_art_uri", None): result = await self._async_fetch_image(image_url) # type: ignore[no-untyped-call] return result # type: ignore diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 0abbc6d9f97..b023b819a53 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -114,8 +114,7 @@ class SwitchEntity(ToggleEntity): data = {} for prop, attr in PROP_TO_ATTR.items(): - value = getattr(self, prop) - if value is not None: + if (value := getattr(self, prop)) is not None: data[attr] = value return data diff --git a/homeassistant/components/tplink/sensor.py b/homeassistant/components/tplink/sensor.py index 9bd4a056d33..0ffe375e6ff 100644 --- a/homeassistant/components/tplink/sensor.py +++ b/homeassistant/components/tplink/sensor.py @@ -100,8 +100,7 @@ def async_emeter_from_device( ) -> float | None: """Map a sensor key to the device attribute.""" if attr := description.emeter_attr: - val = getattr(device.emeter_realtime, attr) - if val is None: + if (val := getattr(device.emeter_realtime, attr)) is None: return None return round(cast(float, val), description.precision) diff --git a/homeassistant/components/wirelesstag/__init__.py b/homeassistant/components/wirelesstag/__init__.py index 24afb6b0465..201041ba616 100644 --- a/homeassistant/components/wirelesstag/__init__.py +++ b/homeassistant/components/wirelesstag/__init__.py @@ -74,15 +74,13 @@ class WirelessTagPlatform: def arm(self, switch): """Arm entity sensor monitoring.""" func_name = f"arm_{switch.sensor_type}" - arm_func = getattr(self.api, func_name) - if arm_func is not None: + if (arm_func := getattr(self.api, func_name)) is not None: arm_func(switch.tag_id, switch.tag_manager_mac) def disarm(self, switch): """Disarm entity sensor monitoring.""" func_name = f"disarm_{switch.sensor_type}" - disarm_func = getattr(self.api, func_name) - if disarm_func is not None: + if (disarm_func := getattr(self.api, func_name)) is not None: disarm_func(switch.tag_id, switch.tag_manager_mac) def start_monitoring(self): diff --git a/homeassistant/components/xiaomi_miio/air_quality.py b/homeassistant/components/xiaomi_miio/air_quality.py index 372a1b62e73..271beae131c 100644 --- a/homeassistant/components/xiaomi_miio/air_quality.py +++ b/homeassistant/components/xiaomi_miio/air_quality.py @@ -117,8 +117,7 @@ class AirMonitorB1(XiaomiMiioEntity, AirQualityEntity): data = {} for prop, attr in PROP_TO_ATTR.items(): - value = getattr(self, prop) - if value is not None: + if (value := getattr(self, prop)) is not None: data[attr] = value return data diff --git a/homeassistant/data_entry_flow.py b/homeassistant/data_entry_flow.py index 83135373b9b..2bc1a6c2278 100644 --- a/homeassistant/data_entry_flow.py +++ b/homeassistant/data_entry_flow.py @@ -293,8 +293,7 @@ class FlowManager(abc.ABC): @callback def _async_remove_flow_progress(self, flow_id: str) -> None: """Remove a flow from in progress.""" - flow = self._progress.pop(flow_id, None) - if flow is None: + if (flow := self._progress.pop(flow_id, None)) is None: raise UnknownFlow handler = flow.handler self._handler_progress_index[handler].remove(flow.flow_id) diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index fb1abcbf7e9..c190fd5fc35 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -173,9 +173,7 @@ class EntityComponent: """Unload a config entry.""" key = config_entry.entry_id - platform = self._platforms.pop(key, None) - - if platform is None: + if (platform := self._platforms.pop(key, None)) is None: raise ValueError("Config entry was never loaded!") await platform.async_reset()