From 4f20a8023b51ad8f66fc1c4332e00437d23e7750 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 19 Feb 2022 17:21:26 +0100 Subject: [PATCH] Use assignment expressions [A-I] (#66880) --- homeassistant/components/adax/climate.py | 3 +-- homeassistant/components/androidtv/__init__.py | 3 +-- .../components/aurora_abb_powerone/aurora_device.py | 3 +-- homeassistant/components/balboa/climate.py | 3 +-- homeassistant/components/broadlink/switch.py | 3 +-- homeassistant/components/edl21/sensor.py | 4 +--- homeassistant/components/flux_led/__init__.py | 3 +-- homeassistant/components/flux_led/config_flow.py | 3 +-- homeassistant/components/flux_led/discovery.py | 3 +-- homeassistant/components/fritz/switch.py | 4 +--- homeassistant/components/heos/__init__.py | 3 +-- homeassistant/components/hive/alarm_control_panel.py | 3 +-- homeassistant/components/homekit/util.py | 3 +-- .../components/homekit_controller/diagnostics.py | 3 +-- homeassistant/components/homematic/sensor.py | 3 +-- homeassistant/components/hue/device_trigger.py | 9 +++------ homeassistant/components/hue/migration.py | 3 +-- homeassistant/components/iaqualink/__init__.py | 4 +--- 18 files changed, 20 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/adax/climate.py b/homeassistant/components/adax/climate.py index 48cbc9b270c..ea8e3554746 100644 --- a/homeassistant/components/adax/climate.py +++ b/homeassistant/components/adax/climate.py @@ -146,8 +146,7 @@ class LocalAdaxDevice(ClimateEntity): async def async_set_temperature(self, **kwargs): """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_target_temperature(temperature) diff --git a/homeassistant/components/androidtv/__init__.py b/homeassistant/components/androidtv/__init__.py index 9b968385602..157b618a264 100644 --- a/homeassistant/components/androidtv/__init__.py +++ b/homeassistant/components/androidtv/__init__.py @@ -125,8 +125,7 @@ def _migrate_aftv_entity(hass, aftv, entry_unique_id): # entity already exist, nothing to do return - old_unique_id = aftv.device_properties.get(PROP_SERIALNO) - if not old_unique_id: + if not (old_unique_id := aftv.device_properties.get(PROP_SERIALNO)): # serial no not found, exit return diff --git a/homeassistant/components/aurora_abb_powerone/aurora_device.py b/homeassistant/components/aurora_abb_powerone/aurora_device.py index d9cfb744231..5a524851bdf 100644 --- a/homeassistant/components/aurora_abb_powerone/aurora_device.py +++ b/homeassistant/components/aurora_abb_powerone/aurora_device.py @@ -35,8 +35,7 @@ class AuroraEntity(Entity): @property def unique_id(self) -> str | None: """Return the unique id for this device.""" - serial = self._data.get(ATTR_SERIAL_NUMBER) - if serial is None: + if (serial := self._data.get(ATTR_SERIAL_NUMBER)) is None: return None return f"{serial}_{self.entity_description.key}" diff --git a/homeassistant/components/balboa/climate.py b/homeassistant/components/balboa/climate.py index 4145ea8d807..81016bbeb33 100644 --- a/homeassistant/components/balboa/climate.py +++ b/homeassistant/components/balboa/climate.py @@ -99,8 +99,7 @@ class BalboaSpaClimate(BalboaEntity, ClimateEntity): @property def hvac_action(self) -> str: """Return the current operation mode.""" - state = self._client.get_heatstate() - if state >= self._client.ON: + if self._client.get_heatstate() >= self._client.ON: return CURRENT_HVAC_HEAT return CURRENT_HVAC_IDLE diff --git a/homeassistant/components/broadlink/switch.py b/homeassistant/components/broadlink/switch.py index aa295fd0d99..6a015748bd0 100644 --- a/homeassistant/components/broadlink/switch.py +++ b/homeassistant/components/broadlink/switch.py @@ -78,9 +78,8 @@ async def async_setup_platform( """ mac_addr = config[CONF_MAC] host = config.get(CONF_HOST) - switches = config.get(CONF_SWITCHES) - if switches: + if switches := config.get(CONF_SWITCHES): platform_data = hass.data[DOMAIN].platforms.setdefault(Platform.SWITCH, {}) platform_data.setdefault(mac_addr, []).extend(switches) diff --git a/homeassistant/components/edl21/sensor.py b/homeassistant/components/edl21/sensor.py index f96f9d828bb..278ac004121 100644 --- a/homeassistant/components/edl21/sensor.py +++ b/homeassistant/components/edl21/sensor.py @@ -450,9 +450,7 @@ class EDL21Entity(SensorEntity): @property def native_unit_of_measurement(self): """Return the unit of measurement.""" - unit = self._telegram.get("unit") - - if unit is None: + if (unit := self._telegram.get("unit")) is None: return None return SENSOR_UNIT_MAPPING[unit] diff --git a/homeassistant/components/flux_led/__init__.py b/homeassistant/components/flux_led/__init__.py index ff1962aed1b..997f053aa3c 100644 --- a/homeassistant/components/flux_led/__init__.py +++ b/homeassistant/components/flux_led/__init__.py @@ -90,8 +90,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _async_migrate_unique_ids(hass: HomeAssistant, entry: ConfigEntry) -> None: """Migrate entities when the mac address gets discovered.""" - unique_id = entry.unique_id - if not unique_id: + if not (unique_id := entry.unique_id): return entry_id = entry.entry_id diff --git a/homeassistant/components/flux_led/config_flow.py b/homeassistant/components/flux_led/config_flow.py index ee8da5bd66a..5bdd18d1dbd 100644 --- a/homeassistant/components/flux_led/config_flow.py +++ b/homeassistant/components/flux_led/config_flow.py @@ -165,8 +165,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): except FLUX_LED_EXCEPTIONS: errors["base"] = "cannot_connect" else: - mac_address = device[ATTR_ID] - if mac_address is not None: + if (mac_address := device[ATTR_ID]) is not None: await self.async_set_unique_id( dr.format_mac(mac_address), raise_on_progress=False ) diff --git a/homeassistant/components/flux_led/discovery.py b/homeassistant/components/flux_led/discovery.py index cd0d9424ead..62b80243c8b 100644 --- a/homeassistant/components/flux_led/discovery.py +++ b/homeassistant/components/flux_led/discovery.py @@ -82,8 +82,7 @@ def async_build_cached_discovery(entry: ConfigEntry) -> FluxLEDDiscovery: @callback def async_name_from_discovery(device: FluxLEDDiscovery) -> str: """Convert a flux_led discovery to a human readable name.""" - mac_address = device[ATTR_ID] - if mac_address is None: + if (mac_address := device[ATTR_ID]) is None: return device[ATTR_IPADDR] short_mac = mac_address[-6:] if device[ATTR_MODEL_DESCRIPTION]: diff --git a/homeassistant/components/fritz/switch.py b/homeassistant/components/fritz/switch.py index 07eb2eb437f..3b01ce618b8 100644 --- a/homeassistant/components/fritz/switch.py +++ b/homeassistant/components/fritz/switch.py @@ -60,9 +60,7 @@ def deflection_entities_list( _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_DEFLECTION) return [] - deflection_list = avm_wrapper.get_ontel_deflections() - - if not deflection_list: + if not (deflection_list := avm_wrapper.get_ontel_deflections()): return [] items = xmltodict.parse(deflection_list["NewDeflectionList"])["List"]["Item"] diff --git a/homeassistant/components/heos/__init__.py b/homeassistant/components/heos/__init__.py index bbe611c1db9..dbd66e28307 100644 --- a/homeassistant/components/heos/__init__.py +++ b/homeassistant/components/heos/__init__.py @@ -331,8 +331,7 @@ class GroupManager: heos_const.EVENT_CONNECTED, SIGNAL_HEOS_PLAYER_ADDED, ): - groups = await self.async_get_group_membership() - if groups: + if groups := await self.async_get_group_membership(): self._group_membership = groups _LOGGER.debug("Groups updated due to change event") # Let players know to update diff --git a/homeassistant/components/hive/alarm_control_panel.py b/homeassistant/components/hive/alarm_control_panel.py index 4f0520eaa72..4a0ad577f90 100644 --- a/homeassistant/components/hive/alarm_control_panel.py +++ b/homeassistant/components/hive/alarm_control_panel.py @@ -36,8 +36,7 @@ async def async_setup_entry( """Set up Hive thermostat based on a config entry.""" hive = hass.data[DOMAIN][entry.entry_id] - devices = hive.session.deviceList.get("alarm_control_panel") - if devices: + if devices := hive.session.deviceList.get("alarm_control_panel"): async_add_entities( [HiveAlarmControlPanelEntity(hive, dev) for dev in devices], True ) diff --git a/homeassistant/components/homekit/util.py b/homeassistant/components/homekit/util.py index 3165280a37b..8c64b9b0443 100644 --- a/homeassistant/components/homekit/util.py +++ b/homeassistant/components/homekit/util.py @@ -424,8 +424,7 @@ def format_version(version): """Extract the version string in a format homekit can consume.""" split_ver = str(version).replace("-", ".") num_only = NUMBERS_ONLY_RE.sub("", split_ver) - match = VERSION_RE.search(num_only) - if match: + if match := VERSION_RE.search(num_only): return match.group(0) return None diff --git a/homeassistant/components/homekit_controller/diagnostics.py b/homeassistant/components/homekit_controller/diagnostics.py index fd404636a22..f83ce7604cf 100644 --- a/homeassistant/components/homekit_controller/diagnostics.py +++ b/homeassistant/components/homekit_controller/diagnostics.py @@ -122,8 +122,7 @@ def _async_get_diagnostics( devices = data["devices"] = [] for device_id in connection.devices.values(): - device = device_registry.async_get(device_id) - if not device: + if not (device := device_registry.async_get(device_id)): continue devices.append(_async_get_diagnostics_for_device(hass, device)) diff --git a/homeassistant/components/homematic/sensor.py b/homeassistant/components/homematic/sensor.py index c8dc86c3348..456a10b7630 100644 --- a/homeassistant/components/homematic/sensor.py +++ b/homeassistant/components/homematic/sensor.py @@ -272,8 +272,7 @@ def setup_platform( devices = [] for conf in discovery_info[ATTR_DISCOVER_DEVICES]: state = conf.get(ATTR_PARAM) - entity_desc = SENSOR_DESCRIPTIONS.get(state) - if entity_desc is None: + if (entity_desc := SENSOR_DESCRIPTIONS.get(state)) is None: name = conf.get(ATTR_NAME) _LOGGER.warning( "Sensor (%s) entity description is missing. Sensor state (%s) needs to be maintained", diff --git a/homeassistant/components/hue/device_trigger.py b/homeassistant/components/hue/device_trigger.py index ee0453c9da6..a4b545aa141 100644 --- a/homeassistant/components/hue/device_trigger.py +++ b/homeassistant/components/hue/device_trigger.py @@ -41,8 +41,7 @@ async def async_validate_trigger_config(hass: "HomeAssistant", config: ConfigTyp device_id = config[CONF_DEVICE_ID] # lookup device in HASS DeviceRegistry dev_reg: dr.DeviceRegistry = dr.async_get(hass) - device_entry = dev_reg.async_get(device_id) - if device_entry is None: + if (device_entry := dev_reg.async_get(device_id)) is None: raise InvalidDeviceAutomationConfig(f"Device ID {device_id} is not valid") for conf_entry_id in device_entry.config_entries: @@ -64,8 +63,7 @@ async def async_attach_trigger( device_id = config[CONF_DEVICE_ID] # lookup device in HASS DeviceRegistry dev_reg: dr.DeviceRegistry = dr.async_get(hass) - device_entry = dev_reg.async_get(device_id) - if device_entry is None: + if (device_entry := dev_reg.async_get(device_id)) is None: raise InvalidDeviceAutomationConfig(f"Device ID {device_id} is not valid") for conf_entry_id in device_entry.config_entries: @@ -90,8 +88,7 @@ async def async_get_triggers(hass: "HomeAssistant", device_id: str): return [] # lookup device in HASS DeviceRegistry dev_reg: dr.DeviceRegistry = dr.async_get(hass) - device_entry = dev_reg.async_get(device_id) - if device_entry is None: + if (device_entry := dev_reg.async_get(device_id)) is None: raise ValueError(f"Device ID {device_id} is not valid") # Iterate all config entries for this device diff --git a/homeassistant/components/hue/migration.py b/homeassistant/components/hue/migration.py index f779fccdb3b..1d56d493785 100644 --- a/homeassistant/components/hue/migration.py +++ b/homeassistant/components/hue/migration.py @@ -39,8 +39,7 @@ async def check_migration(hass: core.HomeAssistant, entry: ConfigEntry) -> None: data[CONF_API_KEY] = data.pop(CONF_USERNAME) hass.config_entries.async_update_entry(entry, data=data) - conf_api_version = entry.data.get(CONF_API_VERSION, 1) - if conf_api_version == 1: + if (conf_api_version := entry.data.get(CONF_API_VERSION, 1)) == 1: # a bridge might have upgraded firmware since last run so # we discover its capabilities at every startup websession = aiohttp_client.async_get_clientsession(hass) diff --git a/homeassistant/components/iaqualink/__init__.py b/homeassistant/components/iaqualink/__init__.py index 73aa6f18867..030bb8cdcc8 100644 --- a/homeassistant/components/iaqualink/__init__.py +++ b/homeassistant/components/iaqualink/__init__.py @@ -71,9 +71,7 @@ CONFIG_SCHEMA = vol.Schema( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Aqualink component.""" - conf = config.get(DOMAIN) - - if conf is not None: + if (conf := config.get(DOMAIN)) is not None: hass.async_create_task( hass.config_entries.flow.async_init( DOMAIN,