Use assignment expressions 21 (#57970)

This commit is contained in:
Marc Mueller 2021-10-20 17:47:46 +02:00 committed by GitHub
parent d20936d175
commit ea7252e377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 28 additions and 56 deletions

View file

@ -91,8 +91,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME)
mode = config.get(CONF_MODE)
units = config.get(CONF_UNITS)
if not units:
if not (units := config.get(CONF_UNITS)):
units = "ca" if hass.config.units.is_metric else "us"
dark_sky = DarkSkyData(config.get(CONF_API_KEY), latitude, longitude, units)

View file

@ -178,8 +178,7 @@ class EDL21:
new_entities = []
for telegram in message_body.get("valList", []):
obis = telegram.get("objName")
if not obis:
if not (obis := telegram.get("objName")):
continue
if (electricity_id, obis) in self._registered_obis:
@ -187,8 +186,7 @@ class EDL21:
self._hass, SIGNAL_EDL21_TELEGRAM, electricity_id, telegram
)
else:
name = self._OBIS_NAMES.get(obis)
if name:
if name := self._OBIS_NAMES.get(obis):
if self._name:
name = f"{self._name}: {name}"
new_entities.append(

View file

@ -399,8 +399,7 @@ def _async_register_events_and_services(hass: HomeAssistant):
referenced = async_extract_referenced_entity_ids(hass, service)
dev_reg = device_registry.async_get(hass)
for device_id in referenced.referenced_devices:
dev_reg_ent = dev_reg.async_get(device_id)
if not dev_reg_ent:
if not (dev_reg_ent := dev_reg.async_get(device_id)):
raise HomeAssistantError(f"No device found for device id: {device_id}")
macs = [
cval
@ -697,8 +696,7 @@ class HomeKit:
if not self._filter(entity_id):
continue
ent_reg_ent = ent_reg.async_get(entity_id)
if ent_reg_ent:
if ent_reg_ent := ent_reg.async_get(entity_id):
await self._async_set_device_info_attributes(
ent_reg_ent, dev_reg, entity_id
)

View file

@ -413,8 +413,7 @@ class HomeAccessory(Accessory):
@ha_callback
def async_update_linked_battery_callback(self, event):
"""Handle linked battery sensor state change listener callback."""
new_state = event.data.get("new_state")
if new_state is None:
if (new_state := event.data.get("new_state")) is None:
return
if self.linked_battery_charging_sensor:
battery_charging_state = None
@ -425,8 +424,7 @@ class HomeAccessory(Accessory):
@ha_callback
def async_update_linked_battery_charging_callback(self, event):
"""Handle linked battery charging sensor state change listener callback."""
new_state = event.data.get("new_state")
if new_state is None:
if (new_state := event.data.get("new_state")) is None:
return
self.async_update_battery(None, new_state.state == STATE_ON)
@ -524,8 +522,7 @@ class HomeBridge(Bridge):
async def async_get_snapshot(self, info):
"""Get snapshot from accessory if supported."""
acc = self.accessories.get(info["aid"])
if acc is None:
if (acc := self.accessories.get(info["aid"])) is None:
raise ValueError("Requested snapshot for missing accessory")
if not hasattr(acc, "async_get_snapshot"):
raise ValueError(

View file

@ -314,8 +314,7 @@ class Camera(HomeAccessory, PyhapCamera):
async def _async_get_stream_source(self):
"""Find the camera stream source url."""
stream_source = self.config.get(CONF_STREAM_SOURCE)
if stream_source:
if stream_source := self.config.get(CONF_STREAM_SOURCE):
return stream_source
try:
stream_source = await self.hass.components.camera.async_get_stream_source(
@ -447,8 +446,7 @@ class Camera(HomeAccessory, PyhapCamera):
async def stop_stream(self, session_info):
"""Stop the stream for the given ``session_id``."""
session_id = session_info["id"]
stream = session_info.get("stream")
if not stream:
if not (stream := session_info.get("stream")):
_LOGGER.debug("No stream for session ID %s", session_id)
return

View file

@ -303,8 +303,7 @@ class TelevisionMediaPlayer(RemoteInputSelectAccessory):
def set_remote_key(self, value):
"""Send remote key value if call came from HomeKit."""
_LOGGER.debug("%s: Set remote key to %s", self.entity_id, value)
key_name = REMOTE_KEYS.get(value)
if key_name is None:
if (key_name := REMOTE_KEYS.get(value)) is None:
_LOGGER.warning("%s: Unhandled key press for %s", self.entity_id, value)
return

View file

@ -207,8 +207,7 @@ class ActivityRemote(RemoteInputSelectAccessory):
def set_remote_key(self, value):
"""Send remote key value if call came from HomeKit."""
_LOGGER.debug("%s: Set remote key to %s", self.entity_id, value)
key_name = REMOTE_KEYS.get(value)
if key_name is None:
if (key_name := REMOTE_KEYS.get(value)) is None:
_LOGGER.warning("%s: Unhandled key press for %s", self.entity_id, value)
return
self.hass.bus.async_fire(

View file

@ -462,8 +462,7 @@ class Thermostat(HomeAccessory):
)
# Set current operation mode for supported thermostats
hvac_action = new_state.attributes.get(ATTR_HVAC_ACTION)
if hvac_action:
if hvac_action := new_state.attributes.get(ATTR_HVAC_ACTION):
homekit_hvac_action = HC_HASS_TO_HOMEKIT_ACTION[hvac_action]
self.char_current_heat_cool.set_value(homekit_hvac_action)
@ -575,8 +574,7 @@ class WaterHeater(HomeAccessory):
def set_heat_cool(self, value):
"""Change operation mode to value if call came from HomeKit."""
_LOGGER.debug("%s: Set heat-cool to %d", self.entity_id, value)
hass_value = HC_HOMEKIT_TO_HASS[value]
if hass_value != HVAC_MODE_HEAT:
if HC_HOMEKIT_TO_HASS[value] != HVAC_MODE_HEAT:
self.char_target_heat_cool.set_value(1) # Heat
def set_target_temperature(self, value):
@ -615,14 +613,12 @@ class WaterHeater(HomeAccessory):
def _get_temperature_range_from_state(state, unit, default_min, default_max):
"""Calculate the temperature range from a state."""
min_temp = state.attributes.get(ATTR_MIN_TEMP)
if min_temp:
if min_temp := state.attributes.get(ATTR_MIN_TEMP):
min_temp = round(temperature_to_homekit(min_temp, unit) * 2) / 2
else:
min_temp = default_min
max_temp = state.attributes.get(ATTR_MAX_TEMP)
if max_temp:
if max_temp := state.attributes.get(ATTR_MAX_TEMP):
max_temp = round(temperature_to_homekit(max_temp, unit) * 2) / 2
else:
max_temp = default_max

View file

@ -55,8 +55,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
config.get(CONF_FOLDER),
)
value_template = config.get(CONF_VALUE_TEMPLATE)
if value_template is not None:
if (value_template := config.get(CONF_VALUE_TEMPLATE)) is not None:
value_template.hass = hass
sensor = EmailContentSensor(
hass,

View file

@ -450,8 +450,7 @@ class ManualMQTTAlarm(alarm.AlarmControlPanelEntity):
async def _async_state_changed_listener(self, event):
"""Publish state change to MQTT."""
new_state = event.data.get("new_state")
if new_state is None:
if (new_state := event.data.get("new_state")) is None:
return
mqtt.async_publish(
self.hass, self._state_topic, new_state.state, self._qos, True

View file

@ -187,8 +187,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
async def async_select_source(self, source):
"""Set the input source."""
source_id = _inverted(self._sources).get(source)
if source_id:
if source_id := _inverted(self._sources).get(source):
await self._tv.setSource(source_id)
await self._async_update_soon()
@ -316,8 +315,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
@property
def app_name(self):
"""Name of the current running app."""
app = self._tv.applications.get(self._tv.application_id)
if app:
if app := self._tv.applications.get(self._tv.application_id):
return app.get("label")
@property
@ -350,8 +348,7 @@ class PhilipsTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
else:
_LOGGER.error("Unable to find channel <%s>", media_id)
elif media_type == MEDIA_TYPE_APP:
app = self._tv.applications.get(media_id)
if app:
if app := self._tv.applications.get(media_id):
await self._tv.setApplication(app["intent"])
await self._async_update_soon()
else:

View file

@ -94,8 +94,7 @@ class SomfyClimate(SomfyEntity, ClimateEntity):
def set_temperature(self, **kwargs) -> None:
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return
self._climate.set_target(TargetMode.MANUAL, temperature, DurationType.NEXT_MODE)

View file

@ -427,8 +427,7 @@ class SpeechManager:
This method is a coroutine.
"""
filename = self.file_cache.get(key)
if not filename:
if not (filename := self.file_cache.get(key)):
raise HomeAssistantError(f"Key {key} not in file cache!")
voice_file = os.path.join(self.cache_dir, filename)

View file

@ -68,9 +68,7 @@ async def async_setup(hass: HomeAssistant, base_config: ConfigType) -> bool:
"""Set up for Vera controllers."""
hass.data[DOMAIN] = {}
config = base_config.get(DOMAIN)
if not config:
if not (config := base_config.get(DOMAIN)):
return True
hass.async_create_task(

View file

@ -117,8 +117,7 @@ class WemoLight(WemoEntity, LightEntity):
@property
def hs_color(self):
"""Return the hs color values of this light."""
xy_color = self.light.state.get("color_xy")
if xy_color:
if xy_color := self.light.state.get("color_xy"):
return color_util.color_xy_to_hs(*xy_color)
return None

View file

@ -35,7 +35,7 @@ async def async_setup_entry(
"""Initialize the integration."""
coordinator = hass.data[DOMAIN][entry.entry_id]
device = entry.data[CONF_DEVICE]
if device is None:
if (device := entry.data[CONF_DEVICE]) is None:
device = entry.entry_id
async_add_entities(

View file

@ -239,12 +239,10 @@ class ZhongHongClimate(ClimateEntity):
def set_temperature(self, **kwargs):
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is not None:
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is not None:
self._device.set_temperature(temperature)
operation_mode = kwargs.get(ATTR_HVAC_MODE)
if operation_mode is not None:
if (operation_mode := kwargs.get(ATTR_HVAC_MODE)) is not None:
self.set_hvac_mode(operation_mode)
def set_hvac_mode(self, hvac_mode):