diff --git a/homeassistant/components/proxmoxve/binary_sensor.py b/homeassistant/components/proxmoxve/binary_sensor.py index fedb513e5b4..eb4bf77fa5e 100644 --- a/homeassistant/components/proxmoxve/binary_sensor.py +++ b/homeassistant/components/proxmoxve/binary_sensor.py @@ -25,10 +25,9 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None): for vm_id in node_config["vms"]: coordinator = host_name_coordinators[node_name][vm_id] - coordinator_data = coordinator.data # unfound vm case - if coordinator_data is None: + if (coordinator_data := coordinator.data) is None: continue vm_name = coordinator_data["name"] @@ -39,10 +38,9 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None): for container_id in node_config["containers"]: coordinator = host_name_coordinators[node_name][container_id] - coordinator_data = coordinator.data # unfound container case - if coordinator_data is None: + if (coordinator_data := coordinator.data) is None: continue container_name = coordinator_data["name"] @@ -88,9 +86,7 @@ class ProxmoxBinarySensor(ProxmoxEntity, BinarySensorEntity): @property def is_on(self): """Return the state of the binary sensor.""" - data = self.coordinator.data - - if data is None: + if (data := self.coordinator.data) is None: return None return data["status"] == "running" diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index be77ea04f1c..512894a9889 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -160,9 +160,7 @@ class PS4Device(MediaPlayerEntity): def _parse_status(self): """Parse status.""" - status = self._ps4.status - - if status is not None: + if (status := self._ps4.status) is not None: self._games = load_games(self.hass, self._unique_id) if self._games: self.get_source_list() @@ -384,13 +382,15 @@ class PS4Device(MediaPlayerEntity): @property def entity_picture(self): """Return picture.""" - if self._state == STATE_PLAYING and self._media_content_id is not None: - image_hash = self.media_image_hash - if image_hash is not None: - return ( - f"/api/media_player_proxy/{self.entity_id}?" - f"token={self.access_token}&cache={image_hash}" - ) + if ( + self._state == STATE_PLAYING + and self._media_content_id is not None + and (image_hash := self.media_image_hash) is not None + ): + return ( + f"/api/media_player_proxy/{self.entity_id}?" + f"token={self.access_token}&cache={image_hash}" + ) return MEDIA_IMAGE_DEFAULT @property diff --git a/homeassistant/components/rainforest_eagle/data.py b/homeassistant/components/rainforest_eagle/data.py index 70c2bddb4b3..91447392ea8 100644 --- a/homeassistant/components/rainforest_eagle/data.py +++ b/homeassistant/components/rainforest_eagle/data.py @@ -136,9 +136,7 @@ class EagleDataCoordinator(DataUpdateCoordinator): async def _async_update_data_200(self): """Get the latest data from the Eagle-200 device.""" - eagle200_meter = self.eagle200_meter - - if eagle200_meter is None: + if (eagle200_meter := self.eagle200_meter) is None: hub = aioeagle.EagleHub( aiohttp_client.async_get_clientsession(self.hass), self.cloud_id, diff --git a/homeassistant/components/seventeentrack/sensor.py b/homeassistant/components/seventeentrack/sensor.py index 9087cff8a97..87546d09004 100644 --- a/homeassistant/components/seventeentrack/sensor.py +++ b/homeassistant/components/seventeentrack/sensor.py @@ -180,8 +180,7 @@ class SeventeenTrackPackageSensor(SensorEntity): @property def name(self): """Return the name.""" - name = self._friendly_name - if not name: + if not (name := self._friendly_name): name = self._tracking_number return f"Seventeentrack Package: {name}" diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py index a3422e27d8d..ead0e6a0dce 100644 --- a/homeassistant/components/smarttub/binary_sensor.py +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -171,10 +171,7 @@ class SmartTubError(SmartTubEntity, BinarySensorEntity): @property def extra_state_attributes(self): """Return the state attributes.""" - - error = self.error - - if error is None: + if (error := self.error) is None: return {} return { diff --git a/homeassistant/components/snmp/sensor.py b/homeassistant/components/snmp/sensor.py index 09bfe3856cc..fb01be229ec 100644 --- a/homeassistant/components/snmp/sensor.py +++ b/homeassistant/components/snmp/sensor.py @@ -167,9 +167,8 @@ class SnmpSensor(SensorEntity): async def async_update(self): """Get the latest data and updates the states.""" await self.data.async_update() - value = self.data.value - if value is None: + if (value := self.data.value) is None: value = STATE_UNKNOWN elif self._value_template is not None: value = self._value_template.async_render_with_possible_json_value( diff --git a/homeassistant/components/tahoma/binary_sensor.py b/homeassistant/components/tahoma/binary_sensor.py index c0946013469..55be39377bf 100644 --- a/homeassistant/components/tahoma/binary_sensor.py +++ b/homeassistant/components/tahoma/binary_sensor.py @@ -60,8 +60,7 @@ class TahomaBinarySensor(TahomaDevice, BinarySensorEntity): def extra_state_attributes(self): """Return the device state attributes.""" attr = {} - super_attr = super().extra_state_attributes - if super_attr is not None: + if (super_attr := super().extra_state_attributes) is not None: attr.update(super_attr) if self._battery is not None: diff --git a/homeassistant/components/tahoma/cover.py b/homeassistant/components/tahoma/cover.py index a02f21fb5e1..c7bd1540769 100644 --- a/homeassistant/components/tahoma/cover.py +++ b/homeassistant/components/tahoma/cover.py @@ -197,8 +197,7 @@ class TahomaCover(TahomaDevice, CoverEntity): def extra_state_attributes(self): """Return the device state attributes.""" attr = {} - super_attr = super().extra_state_attributes - if super_attr is not None: + if (super_attr := super().extra_state_attributes) is not None: attr.update(super_attr) if "core:Memorized1PositionState" in self.tahoma_device.active_states: diff --git a/homeassistant/components/tahoma/lock.py b/homeassistant/components/tahoma/lock.py index 3d160cd95b3..dde1227621c 100644 --- a/homeassistant/components/tahoma/lock.py +++ b/homeassistant/components/tahoma/lock.py @@ -83,7 +83,6 @@ class TahomaLock(TahomaDevice, LockEntity): attr = { ATTR_BATTERY_LEVEL: self._battery_level, } - super_attr = super().extra_state_attributes - if super_attr is not None: + if (super_attr := super().extra_state_attributes) is not None: attr.update(super_attr) return attr diff --git a/homeassistant/components/tahoma/sensor.py b/homeassistant/components/tahoma/sensor.py index 35a51b03805..6a7847ec06e 100644 --- a/homeassistant/components/tahoma/sensor.py +++ b/homeassistant/components/tahoma/sensor.py @@ -113,8 +113,7 @@ class TahomaSensor(TahomaDevice, SensorEntity): def extra_state_attributes(self): """Return the device state attributes.""" attr = {} - super_attr = super().extra_state_attributes - if super_attr is not None: + if (super_attr := super().extra_state_attributes) is not None: attr.update(super_attr) if "core:RSSILevelState" in self.tahoma_device.active_states: diff --git a/homeassistant/components/tahoma/switch.py b/homeassistant/components/tahoma/switch.py index 2ea68b93e6b..e75c72e8d4b 100644 --- a/homeassistant/components/tahoma/switch.py +++ b/homeassistant/components/tahoma/switch.py @@ -108,8 +108,7 @@ class TahomaSwitch(TahomaDevice, SwitchEntity): def extra_state_attributes(self): """Return the device state attributes.""" attr = {} - super_attr = super().extra_state_attributes - if super_attr is not None: + if (super_attr := super().extra_state_attributes) is not None: attr.update(super_attr) if "core:RSSILevelState" in self.tahoma_device.active_states: diff --git a/homeassistant/components/tellstick/light.py b/homeassistant/components/tellstick/light.py index 15b15112d14..a355644da41 100644 --- a/homeassistant/components/tellstick/light.py +++ b/homeassistant/components/tellstick/light.py @@ -66,8 +66,7 @@ class TellstickLight(TellstickDevice, LightEntity): def _update_model(self, new_state, data): """Update the device entity state to match the arguments.""" if new_state: - brightness = data - if brightness is not None: + if (brightness := data) is not None: self._brightness = brightness # _brightness is not defined when called from super diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index c41bc55bcc8..b3f73cebc82 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -259,8 +259,7 @@ class TradfriLight(TradfriBaseDevice, LightEntity): transition_time = None # HSB can always be set, but color temp + brightness is bulb dependent - command = dimmer_command - if command is not None: + if (command := dimmer_command) is not None: command += color_command else: command = color_command diff --git a/homeassistant/components/trafikverket_train/sensor.py b/homeassistant/components/trafikverket_train/sensor.py index cd5cdf29521..d67fef5a0df 100644 --- a/homeassistant/components/trafikverket_train/sensor.py +++ b/homeassistant/components/trafikverket_train/sensor.py @@ -191,8 +191,7 @@ class TrainSensor(SensorEntity): @property def native_value(self): """Return the departure state.""" - state = self._state - if state is not None: + if (state := self._state) is not None: if state.time_at_location is not None: return state.time_at_location if state.estimated_time_at_location is not None: diff --git a/homeassistant/components/transmission/sensor.py b/homeassistant/components/transmission/sensor.py index e5f827d1e52..169aeed363b 100644 --- a/homeassistant/components/transmission/sensor.py +++ b/homeassistant/components/transmission/sensor.py @@ -101,8 +101,7 @@ class TransmissionSpeedSensor(TransmissionSensor): def update(self): """Get the latest data from Transmission and updates the state.""" - data = self._tm_client.api.data - if data: + if data := self._tm_client.api.data: mb_spd = ( float(data.downloadSpeed) if self._sub_type == "download" @@ -117,8 +116,7 @@ class TransmissionStatusSensor(TransmissionSensor): def update(self): """Get the latest data from Transmission and updates the state.""" - data = self._tm_client.api.data - if data: + if data := self._tm_client.api.data: upload = data.uploadSpeed download = data.downloadSpeed if upload > 0 and download > 0: diff --git a/homeassistant/components/travisci/sensor.py b/homeassistant/components/travisci/sensor.py index 427283260e0..826503391b5 100644 --- a/homeassistant/components/travisci/sensor.py +++ b/homeassistant/components/travisci/sensor.py @@ -172,8 +172,7 @@ class TravisCISensor(SensorEntity): self._build = self._data.build(repo.last_build_id) if self._build: - sensor_type = self.entity_description.key - if sensor_type == "state": + if (sensor_type := self.entity_description.key) == "state": branch_stats = self._data.branch(self._branch, self._repo_name) self._attr_native_value = branch_stats.state diff --git a/homeassistant/components/universal/media_player.py b/homeassistant/components/universal/media_player.py index d658a44a117..59cef93de49 100644 --- a/homeassistant/components/universal/media_player.py +++ b/homeassistant/components/universal/media_player.py @@ -256,8 +256,7 @@ class UniversalMediaPlayer(MediaPlayerEntity): ) return - active_child = self._child_state - if active_child is None: + if (active_child := self._child_state) is None: # No child to call service on return @@ -307,8 +306,7 @@ class UniversalMediaPlayer(MediaPlayerEntity): if (master_state == STATE_OFF) or (self._state_template is not None): return master_state - active_child = self._child_state - if active_child: + if active_child := self._child_state: return active_child.state return master_state if master_state else STATE_OFF diff --git a/homeassistant/components/vallox/sensor.py b/homeassistant/components/vallox/sensor.py index ff22c317bc1..7bf9dca700f 100644 --- a/homeassistant/components/vallox/sensor.py +++ b/homeassistant/components/vallox/sensor.py @@ -71,9 +71,7 @@ class ValloxSensor(SensorEntity): async def async_update(self) -> None: """Fetch state from the ventilation unit.""" - metric_key = self.entity_description.metric_key - - if metric_key is None: + if (metric_key := self.entity_description.metric_key) is None: self._attr_available = False _LOGGER.error("Error updating sensor. Empty metric key") return diff --git a/homeassistant/components/vera/__init__.py b/homeassistant/components/vera/__init__.py index 9a153841718..b6e13eb3832 100644 --- a/homeassistant/components/vera/__init__.py +++ b/homeassistant/components/vera/__init__.py @@ -270,8 +270,7 @@ class VeraDevice(Generic[DeviceType], Entity): attr[ATTR_ARMED] = "True" if armed else "False" if self.vera_device.is_trippable: - last_tripped = self.vera_device.last_trip - if last_tripped is not None: + if (last_tripped := self.vera_device.last_trip) is not None: utc_time = utc_from_timestamp(int(last_tripped)) attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat() else: @@ -279,12 +278,10 @@ class VeraDevice(Generic[DeviceType], Entity): tripped = self.vera_device.is_tripped attr[ATTR_TRIPPED] = "True" if tripped else "False" - power = self.vera_device.power - if power: + if power := self.vera_device.power: attr[ATTR_CURRENT_POWER_W] = convert(power, float, 0.0) - energy = self.vera_device.energy - if energy: + if energy := self.vera_device.energy: attr[ATTR_CURRENT_ENERGY_KWH] = convert(energy, float, 0.0) attr["Vera Device Id"] = self.vera_device.vera_device_id diff --git a/homeassistant/components/vera/climate.py b/homeassistant/components/vera/climate.py index cde36dcc623..69d5a3ccbfa 100644 --- a/homeassistant/components/vera/climate.py +++ b/homeassistant/components/vera/climate.py @@ -114,9 +114,9 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity): @property def current_power_w(self) -> float | None: """Return the current power usage in W.""" - power = self.vera_device.power - if power: + if power := self.vera_device.power: return convert(power, float, 0.0) + return None @property def temperature_unit(self) -> str: diff --git a/homeassistant/components/vera/switch.py b/homeassistant/components/vera/switch.py index 304441037ec..9f44bb28286 100644 --- a/homeassistant/components/vera/switch.py +++ b/homeassistant/components/vera/switch.py @@ -61,9 +61,9 @@ class VeraSwitch(VeraDevice[veraApi.VeraSwitch], SwitchEntity): @property def current_power_w(self) -> float | None: """Return the current power usage in W.""" - power = self.vera_device.power - if power: + if power := self.vera_device.power: return convert(power, float, 0.0) + return None @property def is_on(self) -> bool: diff --git a/homeassistant/components/vesync/fan.py b/homeassistant/components/vesync/fan.py index 6641f43d17b..c32ac6d2a25 100644 --- a/homeassistant/components/vesync/fan.py +++ b/homeassistant/components/vesync/fan.py @@ -76,10 +76,11 @@ class VeSyncFanHA(VeSyncDevice, FanEntity): @property def percentage(self): """Return the current speed.""" - if self.smartfan.mode == "manual": - current_level = self.smartfan.fan_level - if current_level is not None: - return ranged_value_to_percentage(SPEED_RANGE, current_level) + if ( + self.smartfan.mode == "manual" + and (current_level := self.smartfan.fan_level) is not None + ): + return ranged_value_to_percentage(SPEED_RANGE, current_level) return None @property diff --git a/homeassistant/components/weather/__init__.py b/homeassistant/components/weather/__init__.py index d4965be841d..81d245c19bb 100644 --- a/homeassistant/components/weather/__init__.py +++ b/homeassistant/components/weather/__init__.py @@ -185,28 +185,22 @@ class WeatherEntity(Entity): self.hass, self.temperature, self.temperature_unit, self.precision ) - humidity = self.humidity - if humidity is not None: + if (humidity := self.humidity) is not None: data[ATTR_WEATHER_HUMIDITY] = round(humidity) - ozone = self.ozone - if ozone is not None: + if (ozone := self.ozone) is not None: data[ATTR_WEATHER_OZONE] = ozone - pressure = self.pressure - if pressure is not None: + if (pressure := self.pressure) is not None: data[ATTR_WEATHER_PRESSURE] = pressure - wind_bearing = self.wind_bearing - if wind_bearing is not None: + if (wind_bearing := self.wind_bearing) is not None: data[ATTR_WEATHER_WIND_BEARING] = wind_bearing - wind_speed = self.wind_speed - if wind_speed is not None: + if (wind_speed := self.wind_speed) is not None: data[ATTR_WEATHER_WIND_SPEED] = wind_speed - visibility = self.visibility - if visibility is not None: + if (visibility := self.visibility) is not None: data[ATTR_WEATHER_VISIBILITY] = visibility if self.forecast is not None: diff --git a/homeassistant/components/wemo/entity.py b/homeassistant/components/wemo/entity.py index 62b23b78bd7..2811d371f6b 100644 --- a/homeassistant/components/wemo/entity.py +++ b/homeassistant/components/wemo/entity.py @@ -40,8 +40,7 @@ class WemoEntity(CoordinatorEntity): @property def name(self) -> str: """Return the name of the device if any.""" - suffix = self.name_suffix - if suffix: + if suffix := self.name_suffix: return f"{self.wemo.name} {suffix}" return self.wemo.name @@ -60,8 +59,7 @@ class WemoEntity(CoordinatorEntity): @property def unique_id(self) -> str: """Return the id of this WeMo device.""" - suffix = self.unique_id_suffix - if suffix: + if suffix := self.unique_id_suffix: return f"{self.wemo.serialnumber}_{suffix}" return self.wemo.serialnumber diff --git a/homeassistant/components/wiffi/__init__.py b/homeassistant/components/wiffi/__init__.py index e19fe227ddb..5f87141e423 100644 --- a/homeassistant/components/wiffi/__init__.py +++ b/homeassistant/components/wiffi/__init__.py @@ -103,8 +103,7 @@ class WiffiIntegrationApi: Remove listener for periodic callbacks. """ - remove_listener = self._periodic_callback - if remove_listener is not None: + if (remove_listener := self._periodic_callback) is not None: remove_listener() async def __call__(self, device, metrics): diff --git a/homeassistant/components/xbox/media_player.py b/homeassistant/components/xbox/media_player.py index be798cd999a..17390f81fad 100644 --- a/homeassistant/components/xbox/media_player.py +++ b/homeassistant/components/xbox/media_player.py @@ -127,8 +127,7 @@ class XboxMediaPlayer(CoordinatorEntity, MediaPlayerEntity): @property def media_title(self): """Title of current playing media.""" - app_details = self.data.app_details - if not app_details: + if not (app_details := self.data.app_details): return None return ( app_details.localized_properties[0].product_title @@ -138,8 +137,7 @@ class XboxMediaPlayer(CoordinatorEntity, MediaPlayerEntity): @property def media_image_url(self): """Image url of current playing media.""" - app_details = self.data.app_details - if not app_details: + if not (app_details := self.data.app_details): return None image = _find_media_image(app_details.localized_properties[0].images) diff --git a/homeassistant/components/yamaha_musiccast/media_player.py b/homeassistant/components/yamaha_musiccast/media_player.py index 5081a716357..758d39a8dfb 100644 --- a/homeassistant/components/yamaha_musiccast/media_player.py +++ b/homeassistant/components/yamaha_musiccast/media_player.py @@ -664,8 +664,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity): """Return all media players of the current group, if the media player is server.""" if self.is_client: # If we are a client we can still share group information, but we will take them from the server. - server = self.group_server - if server != self: + if (server := self.group_server) != self: return server.musiccast_group return [self] diff --git a/homeassistant/components/zha/climate.py b/homeassistant/components/zha/climate.py index e734c9cb415..b82cccd5324 100644 --- a/homeassistant/components/zha/climate.py +++ b/homeassistant/components/zha/climate.py @@ -580,8 +580,7 @@ class ZenWithinThermostat(Thermostat): def _rm_rs_action(self) -> str | None: """Return the current HVAC action based on running mode and running state.""" - running_state = self._thrm.running_state - if running_state is None: + if (running_state := self._thrm.running_state) is None: return None if running_state & (RunningState.HEAT | RunningState.HEAT_STAGE_2): return CURRENT_HVAC_HEAT diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index 18df552986d..8e8a92c099a 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -388,8 +388,7 @@ class SmartEnergyMetering(Sensor): attrs = {} if self._channel.device_type is not None: attrs["device_type"] = self._channel.device_type - status = self._channel.status - if status is not None: + if (status := self._channel.status) is not None: attrs["status"] = str(status)[len(status.__class__.__name__) + 1 :] return attrs @@ -578,8 +577,7 @@ class ZenHVACAction(ThermostatHVACAction): def _rm_rs_action(self) -> str | None: """Return the current HVAC action based on running mode and running state.""" - running_state = self._channel.running_state - if running_state is None: + if (running_state := self._channel.running_state) is None: return None rs_heat = ( diff --git a/homeassistant/components/zoneminder/sensor.py b/homeassistant/components/zoneminder/sensor.py index 3384bad758c..0eb3e9d63a2 100644 --- a/homeassistant/components/zoneminder/sensor.py +++ b/homeassistant/components/zoneminder/sensor.py @@ -111,8 +111,7 @@ class ZMSensorMonitors(SensorEntity): def update(self): """Update the sensor.""" - state = self._monitor.function - if not state: + if not (state := self._monitor.function): self._state = None else: self._state = state.value diff --git a/homeassistant/components/zwave/climate.py b/homeassistant/components/zwave/climate.py index a09f839e6c4..0519d42a59c 100644 --- a/homeassistant/components/zwave/climate.py +++ b/homeassistant/components/zwave/climate.py @@ -248,8 +248,7 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity): self._preset_list = [] self._preset_mapping = {} - mode_list = self._mode().data_items - if mode_list: + if mode_list := self._mode().data_items: for mode in mode_list: ha_mode = HVAC_STATE_MAPPINGS.get(str(mode).lower()) ha_preset = PRESET_MAPPINGS.get(str(mode).lower()) @@ -342,8 +341,7 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity): """Update fan mode.""" if self.values.fan_mode: self._current_fan_mode = self.values.fan_mode.data - fan_modes = self.values.fan_mode.data_items - if fan_modes: + if fan_modes := self.values.fan_mode.data_items: self._fan_modes = list(fan_modes) _LOGGER.debug("self._fan_modes=%s", self._fan_modes) diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index 05365b85645..2f9f0b52839 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -53,8 +53,7 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow): # Get current discovered entries. in_progress = self._async_in_progress() - has_devices = in_progress - if not has_devices: + if not (has_devices := in_progress): has_devices = await self.hass.async_add_job( # type: ignore self._discovery_function, self.hass )