Use assignment expressions [A-I] (#66880)

This commit is contained in:
Marc Mueller 2022-02-19 17:21:26 +01:00 committed by GitHub
parent 6e49b0e122
commit 4f20a8023b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 20 additions and 43 deletions

View File

@ -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)

View File

@ -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

View File

@ -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}"

View File

@ -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

View File

@ -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)

View File

@ -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]

View File

@ -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

View File

@ -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
)

View File

@ -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]:

View File

@ -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"]

View File

@ -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

View File

@ -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
)

View File

@ -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

View File

@ -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))

View File

@ -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",

View File

@ -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

View File

@ -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)

View File

@ -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,