From 4513ee4ea5146d344d64138b01ddaba6a786ec5f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 20 Oct 2021 23:34:08 +0200 Subject: [PATCH] Use assignment expressions 12 (#57937) --- homeassistant/components/adax/climate.py | 3 +-- homeassistant/components/asuswrt/__init__.py | 3 +-- homeassistant/components/blueprint/models.py | 3 +-- homeassistant/components/enocean/light.py | 3 +-- homeassistant/components/geo_location/trigger.py | 3 +-- homeassistant/components/google_cloud/tts.py | 3 +-- homeassistant/components/isy994/climate.py | 9 +++------ homeassistant/components/isy994/sensor.py | 3 +-- homeassistant/components/octoprint/__init__.py | 3 +-- homeassistant/components/plaato/__init__.py | 3 +-- homeassistant/components/plex/config_flow.py | 3 +-- homeassistant/components/plex/server.py | 3 +-- homeassistant/components/prometheus/__init__.py | 9 +++------ homeassistant/components/remote/reproduce_state.py | 4 +--- homeassistant/components/shelly/__init__.py | 6 ++---- homeassistant/components/shopping_list/__init__.py | 9 +++------ homeassistant/components/sma/__init__.py | 3 +-- homeassistant/components/utility_meter/sensor.py | 3 +-- homeassistant/components/volumio/browse_media.py | 6 ++---- homeassistant/components/water_heater/reproduce_state.py | 4 +--- homeassistant/components/zabbix/sensor.py | 6 ++---- 21 files changed, 30 insertions(+), 62 deletions(-) diff --git a/homeassistant/components/adax/climate.py b/homeassistant/components/adax/climate.py index 7cc23c048fe..783c2a9f2f8 100644 --- a/homeassistant/components/adax/climate.py +++ b/homeassistant/components/adax/climate.py @@ -86,8 +86,7 @@ class AdaxDevice(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._adax_data_handler.set_room_target_temperature( self._device_id, temperature, True diff --git a/homeassistant/components/asuswrt/__init__.py b/homeassistant/components/asuswrt/__init__.py index 1305403e1d7..2d067d0e608 100644 --- a/homeassistant/components/asuswrt/__init__.py +++ b/homeassistant/components/asuswrt/__init__.py @@ -73,8 +73,7 @@ CONFIG_SCHEMA = vol.Schema( async def async_setup(hass, config): """Set up the AsusWrt integration.""" - conf = config.get(DOMAIN) - if conf is None: + if (conf := config.get(DOMAIN)) is None: return True # save the options from config yaml diff --git a/homeassistant/components/blueprint/models.py b/homeassistant/components/blueprint/models.py index 827d37843d9..a8146764710 100644 --- a/homeassistant/components/blueprint/models.py +++ b/homeassistant/components/blueprint/models.py @@ -255,8 +255,7 @@ class DomainBlueprints: def load_from_cache(): """Load blueprint from cache.""" - blueprint = self._blueprints[blueprint_path] - if blueprint is None: + if (blueprint := self._blueprints[blueprint_path]) is None: raise FailedToLoad( self.domain, blueprint_path, diff --git a/homeassistant/components/enocean/light.py b/homeassistant/components/enocean/light.py index b8ea753b8d0..d743b6c3346 100644 --- a/homeassistant/components/enocean/light.py +++ b/homeassistant/components/enocean/light.py @@ -73,8 +73,7 @@ class EnOceanLight(EnOceanEntity, LightEntity): def turn_on(self, **kwargs): """Turn the light source on or sets a specific dimmer value.""" - brightness = kwargs.get(ATTR_BRIGHTNESS) - if brightness is not None: + if (brightness := kwargs.get(ATTR_BRIGHTNESS)) is not None: self._brightness = brightness bval = math.floor(self._brightness / 256.0 * 100.0) diff --git a/homeassistant/components/geo_location/trigger.py b/homeassistant/components/geo_location/trigger.py index b77aecee14c..c030b3d3075 100644 --- a/homeassistant/components/geo_location/trigger.py +++ b/homeassistant/components/geo_location/trigger.py @@ -52,8 +52,7 @@ async def async_attach_trigger(hass, config, action, automation_info): if not source_match(from_state, source) and not source_match(to_state, source): return - zone_state = hass.states.get(zone_entity_id) - if zone_state is None: + if (zone_state := hass.states.get(zone_entity_id)) is None: _LOGGER.warning( "Unable to execute automation %s: Zone %s not found", automation_info["name"], diff --git a/homeassistant/components/google_cloud/tts.py b/homeassistant/components/google_cloud/tts.py index a1cbed2ee55..af4e6771795 100644 --- a/homeassistant/components/google_cloud/tts.py +++ b/homeassistant/components/google_cloud/tts.py @@ -150,8 +150,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( async def async_get_engine(hass, config, discovery_info=None): """Set up Google Cloud TTS component.""" - key_file = config.get(CONF_KEY_FILE) - if key_file: + if key_file := config.get(CONF_KEY_FILE): key_file = hass.config.path(key_file) if not os.path.isfile(key_file): _LOGGER.error("File %s doesn't exist", key_file) diff --git a/homeassistant/components/isy994/climate.py b/homeassistant/components/isy994/climate.py index 578fbe2bf21..df9598b00ee 100644 --- a/homeassistant/components/isy994/climate.py +++ b/homeassistant/components/isy994/climate.py @@ -106,8 +106,7 @@ class ISYThermostatEntity(ISYNodeEntity, ClimateEntity): @property def temperature_unit(self) -> str: """Return the unit of measurement.""" - uom = self._node.aux_properties.get(PROP_UOM) - if not uom: + if not (uom := self._node.aux_properties.get(PROP_UOM)): return self.hass.config.units.temperature_unit if uom.value == UOM_ISY_CELSIUS: return TEMP_CELSIUS @@ -117,16 +116,14 @@ class ISYThermostatEntity(ISYNodeEntity, ClimateEntity): @property def current_humidity(self) -> int | None: """Return the current humidity.""" - humidity = self._node.aux_properties.get(PROP_HUMIDITY) - if not humidity: + if not (humidity := self._node.aux_properties.get(PROP_HUMIDITY)): return None return int(humidity.value) @property def hvac_mode(self) -> str | None: """Return hvac operation ie. heat, cool mode.""" - hvac_mode = self._node.aux_properties.get(CMD_CLIMATE_MODE) - if not hvac_mode: + if not (hvac_mode := self._node.aux_properties.get(CMD_CLIMATE_MODE)): return None # Which state values used depends on the mode property's UOM: diff --git a/homeassistant/components/isy994/sensor.py b/homeassistant/components/isy994/sensor.py index c15da2af0da..6439a0d198f 100644 --- a/homeassistant/components/isy994/sensor.py +++ b/homeassistant/components/isy994/sensor.py @@ -57,8 +57,7 @@ class ISYSensorEntity(ISYNodeEntity, SensorEntity): return UOM_FRIENDLY_NAME.get(uom[0], uom[0]) # Special cases for ISY UOM index units: - isy_states = UOM_TO_STATES.get(uom) - if isy_states: + if isy_states := UOM_TO_STATES.get(uom): return isy_states if uom in (UOM_ON_OFF, UOM_INDEX): diff --git a/homeassistant/components/octoprint/__init__.py b/homeassistant/components/octoprint/__init__.py index 396a18318f2..31474611783 100644 --- a/homeassistant/components/octoprint/__init__.py +++ b/homeassistant/components/octoprint/__init__.py @@ -280,8 +280,7 @@ class OctoPrintAPI: def update(self, sensor_type, end_point, group, tool=None): """Return the value for sensor_type from the provided endpoint.""" - response = self.get(end_point) - if response is not None: + if (response := self.get(end_point)) is not None: return get_value_from_json(response, sensor_type, group, tool) return response diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index c965c632827..ce764c4fef5 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -85,9 +85,8 @@ WEBHOOK_SCHEMA = vol.Schema( async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Configure based on config entry.""" hass.data.setdefault(DOMAIN, {}) - use_webhook = entry.data[CONF_USE_WEBHOOK] - if use_webhook: + if entry.data[CONF_USE_WEBHOOK]: async_setup_webhook(hass, entry) else: await async_setup_coordinator(hass, entry) diff --git a/homeassistant/components/plex/config_flow.py b/homeassistant/components/plex/config_flow.py index cffb484ac5a..b2606c6eeaf 100644 --- a/homeassistant/components/plex/config_flow.py +++ b/homeassistant/components/plex/config_flow.py @@ -131,8 +131,7 @@ class PlexFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Begin manual configuration.""" if user_input is not None and errors is None: user_input.pop(CONF_URL, None) - host = user_input.get(CONF_HOST) - if host: + if host := user_input.get(CONF_HOST): port = user_input[CONF_PORT] prefix = "https" if user_input.get(CONF_SSL) else "http" user_input[CONF_URL] = f"{prefix}://{host}:{port}" diff --git a/homeassistant/components/plex/server.py b/homeassistant/components/plex/server.py index 12398edfd59..62fa095b50f 100644 --- a/homeassistant/components/plex/server.py +++ b/homeassistant/components/plex/server.py @@ -259,8 +259,7 @@ class PlexServer: """Process a session payload received from a websocket callback.""" session_payload = payload["PlaySessionStateNotification"][0] - state = session_payload["state"] - if state == "buffering": + if (state := session_payload["state"]) == "buffering": return session_key = int(session_payload["sessionKey"]) diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 39ac4c28415..7c531a292b1 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -148,8 +148,7 @@ class PrometheusMetrics: def handle_event(self, event): """Listen for new messages on the bus, and add them to Prometheus.""" - state = event.data.get("new_state") - if state is None: + if (state := event.data.get("new_state")) is None: return entity_id = state.entity_id @@ -318,8 +317,7 @@ class PrometheusMetrics: metric.labels(**self._labels(state)).set(value) def _handle_climate_temp(self, state, attr, metric_name, metric_description): - temp = state.attributes.get(attr) - if temp: + if temp := state.attributes.get(attr): if self._climate_units == TEMP_FAHRENHEIT: temp = fahrenheit_to_celsius(temp) metric = self._metric( @@ -355,8 +353,7 @@ class PrometheusMetrics: "Current temperature in degrees Celsius", ) - current_action = state.attributes.get(ATTR_HVAC_ACTION) - if current_action: + if current_action := state.attributes.get(ATTR_HVAC_ACTION): metric = self._metric( "climate_action", self.prometheus_cli.Gauge, diff --git a/homeassistant/components/remote/reproduce_state.py b/homeassistant/components/remote/reproduce_state.py index cc9685dee2f..064e4a9711a 100644 --- a/homeassistant/components/remote/reproduce_state.py +++ b/homeassistant/components/remote/reproduce_state.py @@ -30,9 +30,7 @@ async def _async_reproduce_state( reproduce_options: dict[str, Any] | None = None, ) -> None: """Reproduce a single state.""" - cur_state = hass.states.get(state.entity_id) - - if cur_state is None: + if (cur_state := hass.states.get(state.entity_id)) is None: _LOGGER.warning("Unable to find entity %s", state.entity_id) return diff --git a/homeassistant/components/shelly/__init__.py b/homeassistant/components/shelly/__init__.py index da1603e3201..f181817e6f1 100644 --- a/homeassistant/components/shelly/__init__.py +++ b/homeassistant/components/shelly/__init__.py @@ -79,8 +79,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Shelly component.""" hass.data[DOMAIN] = {DATA_CONFIG_ENTRY: {}} - conf = config.get(DOMAIN) - if conf is not None: + if (conf := config.get(DOMAIN)) is not None: hass.data[DOMAIN][CONF_COAP_PORT] = conf[CONF_COAP_PORT] return True @@ -233,9 +232,8 @@ class BlockDeviceWrapper(update_coordinator.DataUpdateCoordinator): ) -> None: """Initialize the Shelly device wrapper.""" self.device_id: str | None = None - sleep_period = entry.data["sleep_period"] - if sleep_period: + if sleep_period := entry.data["sleep_period"]: update_interval = SLEEP_PERIOD_MULTIPLIER * sleep_period else: update_interval = ( diff --git a/homeassistant/components/shopping_list/__init__.py b/homeassistant/components/shopping_list/__init__.py index a38720bef59..fd3072ff7f4 100644 --- a/homeassistant/components/shopping_list/__init__.py +++ b/homeassistant/components/shopping_list/__init__.py @@ -82,15 +82,13 @@ async def async_setup_entry(hass, config_entry): async def add_item_service(call): """Add an item with `name`.""" data = hass.data[DOMAIN] - name = call.data.get(ATTR_NAME) - if name is not None: + if (name := call.data.get(ATTR_NAME)) is not None: await data.async_add(name) async def complete_item_service(call): """Mark the item provided via `name` as completed.""" data = hass.data[DOMAIN] - name = call.data.get(ATTR_NAME) - if name is None: + if (name := call.data.get(ATTR_NAME)) is None: return try: item = [item for item in data.items if item["name"] == name][0] @@ -102,8 +100,7 @@ async def async_setup_entry(hass, config_entry): async def incomplete_item_service(call): """Mark the item provided via `name` as incomplete.""" data = hass.data[DOMAIN] - name = call.data.get(ATTR_NAME) - if name is None: + if (name := call.data.get(ATTR_NAME)) is None: return try: item = [item for item in data.items if item["name"] == name][0] diff --git a/homeassistant/components/sma/__init__.py b/homeassistant/components/sma/__init__.py index 2eb0e6760ed..1a48bee7797 100644 --- a/homeassistant/components/sma/__init__.py +++ b/homeassistant/components/sma/__init__.py @@ -61,8 +61,7 @@ def _parse_legacy_options( ) # Parsing of sensors configuration - config_sensors = entry.data.get(CONF_SENSORS) - if not config_sensors: + if not (config_sensors := entry.data.get(CONF_SENSORS)): return [] # Support import of legacy config that should have been removed from 0.99, but was still functional diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index 50185461030..29ea478f0b3 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -207,8 +207,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity): @callback def async_tariff_change(self, event): """Handle tariff changes.""" - new_state = event.data.get("new_state") - if new_state is None: + if (new_state := event.data.get("new_state")) is None: return self._change_status(new_state.state) diff --git a/homeassistant/components/volumio/browse_media.py b/homeassistant/components/volumio/browse_media.py index 25fe929aaf1..ad634392989 100644 --- a/homeassistant/components/volumio/browse_media.py +++ b/homeassistant/components/volumio/browse_media.py @@ -102,8 +102,7 @@ def _list_payload(item, children=None): def _raw_item_payload(entity, item, parent_item=None, title=None, info=None): if "type" in item: - thumbnail = item.get("albumart") - if thumbnail: + if thumbnail := item.get("albumart"): item_hash = str(hash(thumbnail)) entity.thumbnail_cache.setdefault(item_hash, thumbnail) thumbnail = entity.get_browse_image_url(MEDIA_TYPE_MUSIC, item_hash) @@ -156,8 +155,7 @@ async def browse_node(entity, media_library, media_content_type, media_content_i for item in first_list["items"] ] info = navigation.get("info") - title = first_list.get("title") - if not title: + if not (title := first_list.get("title")): if info: title = f"{info.get('album')} ({info.get('artist')})" else: diff --git a/homeassistant/components/water_heater/reproduce_state.py b/homeassistant/components/water_heater/reproduce_state.py index 513b365e67a..5fbe3f935f8 100644 --- a/homeassistant/components/water_heater/reproduce_state.py +++ b/homeassistant/components/water_heater/reproduce_state.py @@ -53,9 +53,7 @@ async def _async_reproduce_state( reproduce_options: dict[str, Any] | None = None, ) -> None: """Reproduce a single state.""" - cur_state = hass.states.get(state.entity_id) - - if cur_state is None: + if (cur_state := hass.states.get(state.entity_id)) is None: _LOGGER.warning("Unable to find entity %s", state.entity_id) return diff --git a/homeassistant/components/zabbix/sensor.py b/homeassistant/components/zabbix/sensor.py index ff2e2c4d9ba..1b3cf40eedd 100644 --- a/homeassistant/components/zabbix/sensor.py +++ b/homeassistant/components/zabbix/sensor.py @@ -34,16 +34,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Zabbix sensor platform.""" sensors = [] - zapi = hass.data[zabbix.DOMAIN] - if not zapi: + if not (zapi := hass.data[zabbix.DOMAIN]): _LOGGER.error("Zabbix integration hasn't been loaded? zapi is None") return False _LOGGER.info("Connected to Zabbix API Version %s", zapi.api_version()) - trigger_conf = config.get(_CONF_TRIGGERS) # The following code seems overly complex. Need to think about this... - if trigger_conf: + if trigger_conf := config.get(_CONF_TRIGGERS): hostids = trigger_conf.get(_CONF_HOSTIDS) individual = trigger_conf.get(_CONF_INDIVIDUAL) name = trigger_conf.get(CONF_NAME)