From 99903859264b062895af8a3d50c2618583479c88 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 22 Oct 2021 11:34:45 +0200 Subject: [PATCH] Use assignment expressions 25 (#58182) --- homeassistant/components/ambiclimate/climate.py | 3 +-- .../components/ambiclimate/config_flow.py | 3 +-- homeassistant/components/bond/light.py | 3 +-- homeassistant/components/brunt/cover.py | 3 +-- homeassistant/components/daikin/climate.py | 6 ++---- homeassistant/components/dyson/climate.py | 3 +-- .../components/egardia/alarm_control_panel.py | 3 +-- .../components/forecast_solar/__init__.py | 4 +--- .../components/forecast_solar/energy.py | 4 +--- homeassistant/components/hdmi_cec/__init__.py | 3 +-- homeassistant/components/iaqualink/light.py | 7 ++----- homeassistant/components/msteams/notify.py | 17 +++++++---------- homeassistant/components/neato/camera.py | 4 +--- .../components/openweathermap/__init__.py | 3 +-- homeassistant/components/plant/__init__.py | 3 +-- homeassistant/components/ps4/config_flow.py | 3 +-- homeassistant/components/ps4/media_player.py | 3 +-- homeassistant/components/tasmota/__init__.py | 3 +-- homeassistant/components/tasmota/light.py | 3 +-- homeassistant/components/tfiac/climate.py | 3 +-- homeassistant/components/withings/__init__.py | 2 +- 21 files changed, 29 insertions(+), 57 deletions(-) diff --git a/homeassistant/components/ambiclimate/climate.py b/homeassistant/components/ambiclimate/climate.py index aa4be202865..d30e48dbe59 100644 --- a/homeassistant/components/ambiclimate/climate.py +++ b/homeassistant/components/ambiclimate/climate.py @@ -157,8 +157,7 @@ class AmbiclimateEntity(ClimateEntity): async def async_set_temperature(self, **kwargs: Any) -> None: """Set new target temperature.""" - temperature = kwargs.get(ATTR_TEMPERATURE) - if temperature is None: + if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: return await self._heater.set_target_temperature(temperature) diff --git a/homeassistant/components/ambiclimate/config_flow.py b/homeassistant/components/ambiclimate/config_flow.py index 04d1b749d10..623e96a4a67 100644 --- a/homeassistant/components/ambiclimate/config_flow.py +++ b/homeassistant/components/ambiclimate/config_flow.py @@ -143,8 +143,7 @@ class AmbiclimateAuthCallbackView(HomeAssistantView): async def get(self, request: web.Request) -> str: """Receive authorization token.""" # pylint: disable=no-self-use - code = request.query.get("code") - if code is None: + if (code := request.query.get("code")) is None: return "No code" hass = request.app["hass"] hass.async_create_task( diff --git a/homeassistant/components/bond/light.py b/homeassistant/components/bond/light.py index 82bfa24e44d..dd4699ad006 100644 --- a/homeassistant/components/bond/light.py +++ b/homeassistant/components/bond/light.py @@ -273,8 +273,7 @@ class BondFireplace(BondEntity, LightEntity): """Turn the fireplace on.""" _LOGGER.debug("Fireplace async_turn_on called with: %s", kwargs) - brightness = kwargs.get(ATTR_BRIGHTNESS) - if brightness: + if brightness := kwargs.get(ATTR_BRIGHTNESS): flame = round((brightness * 100) / 255) await self._hub.bond.action(self._device.device_id, Action.set_flame(flame)) else: diff --git a/homeassistant/components/brunt/cover.py b/homeassistant/components/brunt/cover.py index 5c9d7b3d4a5..650ce9c05c6 100644 --- a/homeassistant/components/brunt/cover.py +++ b/homeassistant/components/brunt/cover.py @@ -43,8 +43,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): bapi = BruntAPI(username=username, password=password) try: - things = bapi.getThings()["things"] - if not things: + if not (things := bapi.getThings()["things"]): _LOGGER.error("No things present in account") else: add_entities( diff --git a/homeassistant/components/daikin/climate.py b/homeassistant/components/daikin/climate.py index b3e833bb64f..c8e962b9c76 100644 --- a/homeassistant/components/daikin/climate.py +++ b/homeassistant/components/daikin/climate.py @@ -136,12 +136,10 @@ class DaikinClimate(ClimateEntity): values = {} for attr in (ATTR_TEMPERATURE, ATTR_FAN_MODE, ATTR_SWING_MODE, ATTR_HVAC_MODE): - value = settings.get(attr) - if value is None: + if (value := settings.get(attr)) is None: continue - daikin_attr = HA_ATTR_TO_DAIKIN.get(attr) - if daikin_attr is not None: + if (daikin_attr := HA_ATTR_TO_DAIKIN.get(attr)) is not None: if attr == ATTR_HVAC_MODE: values[daikin_attr] = HA_STATE_TO_DAIKIN[value] elif value in self._list[attr]: diff --git a/homeassistant/components/dyson/climate.py b/homeassistant/components/dyson/climate.py index 4f4c4d7cbba..19993a6498c 100644 --- a/homeassistant/components/dyson/climate.py +++ b/homeassistant/components/dyson/climate.py @@ -142,8 +142,7 @@ class DysonClimateEntity(DysonEntity, ClimateEntity): def set_temperature(self, **kwargs): """Set new target temperature.""" - target_temp = kwargs.get(ATTR_TEMPERATURE) - if target_temp is None: + if (target_temp := kwargs.get(ATTR_TEMPERATURE)) is None: _LOGGER.error("Missing target temperature %s", kwargs) return target_temp = int(target_temp) diff --git a/homeassistant/components/egardia/alarm_control_panel.py b/homeassistant/components/egardia/alarm_control_panel.py index b133a96b820..e386ad6beac 100644 --- a/homeassistant/components/egardia/alarm_control_panel.py +++ b/homeassistant/components/egardia/alarm_control_panel.py @@ -97,8 +97,7 @@ class EgardiaAlarm(alarm.AlarmControlPanelEntity): def handle_status_event(self, event): """Handle the Egardia system status event.""" - statuscode = event.get("status") - if statuscode is not None: + if (statuscode := event.get("status")) is not None: status = self.lookupstatusfromcode(statuscode) self.parsestatus(status) self.schedule_update_ha_state() diff --git a/homeassistant/components/forecast_solar/__init__.py b/homeassistant/components/forecast_solar/__init__.py index 9638ea4e4dd..1fd77b9797c 100644 --- a/homeassistant/components/forecast_solar/__init__.py +++ b/homeassistant/components/forecast_solar/__init__.py @@ -25,11 +25,9 @@ PLATFORMS = ["sensor"] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Forecast.Solar from a config entry.""" - api_key = entry.options.get(CONF_API_KEY) # Our option flow may cause it to be an empty string, # this if statement is here to catch that. - if not api_key: - api_key = None + api_key = entry.options.get(CONF_API_KEY) or None session = async_get_clientsession(hass) forecast = ForecastSolar( diff --git a/homeassistant/components/forecast_solar/energy.py b/homeassistant/components/forecast_solar/energy.py index 6bf63910e5f..33537396330 100644 --- a/homeassistant/components/forecast_solar/energy.py +++ b/homeassistant/components/forecast_solar/energy.py @@ -10,9 +10,7 @@ async def async_get_solar_forecast( hass: HomeAssistant, config_entry_id: str ) -> dict[str, dict[str, float | int]] | None: """Get solar forecast for a config entry ID.""" - coordinator = hass.data[DOMAIN].get(config_entry_id) - - if coordinator is None: + if (coordinator := hass.data[DOMAIN].get(config_entry_id)) is None: return None return { diff --git a/homeassistant/components/hdmi_cec/__init__.py b/homeassistant/components/hdmi_cec/__init__.py index 87391634251..47bd8d160c6 100644 --- a/homeassistant/components/hdmi_cec/__init__.py +++ b/homeassistant/components/hdmi_cec/__init__.py @@ -297,8 +297,7 @@ def setup(hass: HomeAssistant, base_config: ConfigType) -> bool: # noqa: C901 def _select_device(call): """Select the active device.""" - addr = call.data[ATTR_DEVICE] - if not addr: + if not (addr := call.data[ATTR_DEVICE]): _LOGGER.error("Device not found: %s", call.data[ATTR_DEVICE]) return if addr in device_aliases: diff --git a/homeassistant/components/iaqualink/light.py b/homeassistant/components/iaqualink/light.py index 79030e1e3ca..17b686947ce 100644 --- a/homeassistant/components/iaqualink/light.py +++ b/homeassistant/components/iaqualink/light.py @@ -48,14 +48,11 @@ class HassAqualinkLight(AqualinkEntity, LightEntity): This handles brightness and light effects for lights that do support them. """ - brightness = kwargs.get(ATTR_BRIGHTNESS) - effect = kwargs.get(ATTR_EFFECT) - # For now I'm assuming lights support either effects or brightness. - if effect: + if effect := kwargs.get(ATTR_EFFECT): effect = AqualinkLightEffect[effect].value await self.dev.set_effect(effect) - elif brightness: + elif brightness := kwargs.get(ATTR_BRIGHTNESS): # Aqualink supports percentages in 25% increments. pct = int(round(brightness * 4.0 / 255)) * 25 await self.dev.set_brightness(pct) diff --git a/homeassistant/components/msteams/notify.py b/homeassistant/components/msteams/notify.py index eafac85c8c2..421b8093a8d 100644 --- a/homeassistant/components/msteams/notify.py +++ b/homeassistant/components/msteams/notify.py @@ -52,17 +52,14 @@ class MSTeamsNotificationService(BaseNotificationService): teams_message.text(message) - if data is not None: - file_url = data.get(ATTR_FILE_URL) + if data is not None and (file_url := data.get(ATTR_FILE_URL)) is not None: + if not file_url.startswith("http"): + _LOGGER.error("URL should start with http or https") + return - if file_url is not None: - if not file_url.startswith("http"): - _LOGGER.error("URL should start with http or https") - return - - message_section = pymsteams.cardsection() - message_section.addImage(file_url) - teams_message.addSection(message_section) + message_section = pymsteams.cardsection() + message_section.addImage(file_url) + teams_message.addSection(message_section) try: teams_message.send() except RuntimeError as err: diff --git a/homeassistant/components/neato/camera.py b/homeassistant/components/neato/camera.py index 392d586068d..ee70116a7d3 100644 --- a/homeassistant/components/neato/camera.py +++ b/homeassistant/components/neato/camera.py @@ -89,11 +89,9 @@ class NeatoCleaningMap(Camera): self._available = False return - image_url = None if self._mapdata: map_data: dict[str, Any] = self._mapdata[self._robot_serial]["maps"][0] - image_url = map_data["url"] - if image_url == self._image_url: + if (image_url := map_data["url"]) == self._image_url: _LOGGER.debug( "The map image_url for '%s' is the same as old", self.entity_id ) diff --git a/homeassistant/components/openweathermap/__init__.py b/homeassistant/components/openweathermap/__init__.py index d9643d6a4d3..58219ee70b3 100644 --- a/homeassistant/components/openweathermap/__init__.py +++ b/homeassistant/components/openweathermap/__init__.py @@ -71,8 +71,7 @@ async def async_migrate_entry(hass, entry): _LOGGER.debug("Migrating OpenWeatherMap entry from version %s", version) if version == 1: - mode = data[CONF_MODE] - if mode == FORECAST_MODE_FREE_DAILY: + if (mode := data[CONF_MODE]) == FORECAST_MODE_FREE_DAILY: mode = FORECAST_MODE_ONECALL_DAILY new_data = {**data, CONF_MODE: mode} diff --git a/homeassistant/components/plant/__init__.py b/homeassistant/components/plant/__init__.py index 290993959b3..5e734c7ba62 100644 --- a/homeassistant/components/plant/__init__.py +++ b/homeassistant/components/plant/__init__.py @@ -291,8 +291,7 @@ class Plant(Entity): ) for entity_id in self._sensormap: - state = self.hass.states.get(entity_id) - if state is not None: + if (state := self.hass.states.get(entity_id)) is not None: self.state_changed(entity_id, state) def _load_history_from_db(self): diff --git a/homeassistant/components/ps4/config_flow.py b/homeassistant/components/ps4/config_flow.py index 7424e0f5e1a..23ee1e061e3 100644 --- a/homeassistant/components/ps4/config_flow.py +++ b/homeassistant/components/ps4/config_flow.py @@ -88,8 +88,7 @@ class PlayStation4FlowHandler(config_entries.ConfigFlow, domain=DOMAIN): if user_input is not None: if user_input[CONF_MODE] == CONF_MANUAL: try: - device = user_input[CONF_IP_ADDRESS] - if device: + if device := user_input[CONF_IP_ADDRESS]: self.m_device = device except KeyError: errors[CONF_IP_ADDRESS] = "no_ipaddress" diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index 512894a9889..bdc8ba9714d 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -203,8 +203,7 @@ class PS4Device(MediaPlayerEntity): store = self._games[self._media_content_id] # If locked get attributes from file. - locked = store.get(ATTR_LOCKED) - if locked: + if store.get(ATTR_LOCKED): self._media_title = store.get(ATTR_MEDIA_TITLE) self._source = self._media_title self._media_image = store.get(ATTR_MEDIA_IMAGE_URL) diff --git a/homeassistant/components/tasmota/__init__.py b/homeassistant/components/tasmota/__init__.py index b863cda796d..fd156e20c3c 100644 --- a/homeassistant/components/tasmota/__init__.py +++ b/homeassistant/components/tasmota/__init__.py @@ -202,8 +202,7 @@ async def websocket_remove_device( device_id = msg["device_id"] dev_registry = await hass.helpers.device_registry.async_get_registry() - device = dev_registry.async_get(device_id) - if not device: + if not (device := dev_registry.async_get(device_id)): connection.send_error( msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found" ) diff --git a/homeassistant/components/tasmota/light.py b/homeassistant/components/tasmota/light.py index de25a25fd4f..c09b4c71948 100644 --- a/homeassistant/components/tasmota/light.py +++ b/homeassistant/components/tasmota/light.py @@ -162,8 +162,7 @@ class TasmotaLight( def state_updated(self, state: bool, **kwargs: Any) -> None: """Handle state updates.""" self._on_off_state = state - attributes = kwargs.get("attributes") - if attributes: + if attributes := kwargs.get("attributes"): if "brightness" in attributes: brightness = float(attributes["brightness"]) percent_bright = brightness / TASMOTA_BRIGHTNESS_MAX diff --git a/homeassistant/components/tfiac/climate.py b/homeassistant/components/tfiac/climate.py index 1e9bb86d8fd..7c350d48775 100644 --- a/homeassistant/components/tfiac/climate.py +++ b/homeassistant/components/tfiac/climate.py @@ -171,8 +171,7 @@ class TfiacClimate(ClimateEntity): async def async_set_temperature(self, **kwargs): """Set new target temperature.""" - temp = kwargs.get(ATTR_TEMPERATURE) - if temp is not None: + if (temp := kwargs.get(ATTR_TEMPERATURE)) is not None: await self._client.set_state(TARGET_TEMP, temp) async def async_set_hvac_mode(self, hvac_mode): diff --git a/homeassistant/components/withings/__init__.py b/homeassistant/components/withings/__init__.py index 2cf6d297f12..7132b3bff9d 100644 --- a/homeassistant/components/withings/__init__.py +++ b/homeassistant/components/withings/__init__.py @@ -65,7 +65,7 @@ CONFIG_SCHEMA = vol.Schema( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Withings component.""" conf = config.get(DOMAIN, {}) - if not conf: + if not (conf := config.get(DOMAIN, {})): return True # Make the config available to the oauth2 config flow.