Use assignment expressions 07 ()

pull/45618/head^2
Marc Mueller 2021-10-17 20:24:34 +02:00 committed by GitHub
parent 2b72b7b7b9
commit 4f8148f9ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 39 additions and 73 deletions

View File

@ -84,8 +84,7 @@ class IhcLight(IHCDevice, LightEntity):
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs[ATTR_BRIGHTNESS]
else:
brightness = self._brightness
if brightness == 0:
if (brightness := self._brightness) == 0:
brightness = 255
if self._dimmable:

View File

@ -234,8 +234,7 @@ class InfluxSensor(SensorEntity):
def update(self):
"""Get the latest data from Influxdb and updates the states."""
self.data.update()
value = self.data.value
if value is None:
if (value := self.data.value) is None:
value = STATE_UNKNOWN
if self._value_template is not None:
value = self._value_template.render_with_possible_json_value(

View File

@ -65,8 +65,7 @@ class InsteonEntity(Entity):
def name(self):
"""Return the name of the node (used for Entity_ID)."""
# Set a base description
description = self._insteon_device.description
if description is None:
if (description := self._insteon_device.description) is None:
description = "Unknown Device"
# Get an extension label if there is one
extension = self._get_label()

View File

@ -36,9 +36,7 @@ async def async_setup_entry(
coordinator: IPPDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
# config flow sets this to either UUID, serial number or None
unique_id = entry.unique_id
if unique_id is None:
if (unique_id := entry.unique_id) is None:
unique_id = entry.entry_id
sensors = []

View File

@ -69,8 +69,7 @@ class ISYSensorEntity(ISYNodeEntity, SensorEntity):
@property
def native_value(self) -> str:
"""Get the state of the ISY994 sensor device."""
value = self._node.status
if value == ISY_VALUE_UNKNOWN:
if (value := self._node.status) == ISY_VALUE_UNKNOWN:
return None
# Get the translated ISY Unit of Measurement

View File

@ -316,8 +316,7 @@ class ControllerDevice(ClimateEntity):
"""Return current operation ie. heat, cool, idle."""
if not self._controller.is_on:
return HVAC_MODE_OFF
mode = self._controller.mode
if mode == Controller.Mode.FREE_AIR:
if (mode := self._controller.mode) == Controller.Mode.FREE_AIR:
return HVAC_MODE_FAN_ONLY
for (key, value) in self._state_to_pizone.items():
if value == mode:

View File

@ -238,8 +238,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
connection = data[DATA_CONNECTION]
kodi = data[DATA_KODI]
name = config_entry.data[CONF_NAME]
uid = config_entry.unique_id
if uid is None:
if (uid := config_entry.unique_id) is None:
uid = config_entry.entry_id
entity = KodiEntity(connection, kodi, name, uid)

View File

@ -462,8 +462,7 @@ class LIFXLight(LightEntity):
"manufacturer": "LIFX",
}
version = self.bulb.host_firmware_version
if version is not None:
if (version := self.bulb.host_firmware_version) is not None:
info["sw_version"] = version
product_map = aiolifx().products.product_map

View File

@ -675,9 +675,7 @@ class LightEntity(ToggleEntity):
@property
def _light_internal_color_mode(self) -> str:
"""Return the color mode of the light with backwards compatibility."""
color_mode = self.color_mode
if color_mode is None:
if (color_mode := self.color_mode) is None:
# Backwards compatibility for color_mode added in 2021.4
# Add warning in 2021.6, remove in 2021.10
supported = self._light_internal_supported_color_modes

View File

@ -177,8 +177,7 @@ class LockEntity(Entity):
return STATE_LOCKING
if self.is_unlocking:
return STATE_UNLOCKING
locked = self.is_locked
if locked is None:
if (locked := self.is_locked) is None:
return None
return STATE_LOCKED if locked else STATE_UNLOCKED

View File

@ -315,8 +315,7 @@ def humanify(hass, events, entity_attr_cache, context_lookup):
"entity_id": entity_id,
}
icon = event.attributes_icon
if icon:
if icon := event.attributes_icon:
data["icon"] = icon
if event.context_user_id:
@ -581,8 +580,7 @@ def _keep_event(hass, event, entities_filter):
if event.event_type in HOMEASSISTANT_EVENTS:
return entities_filter is None or entities_filter(HA_DOMAIN_ENTITY_ID)
entity_id = event.data_entity_id
if entity_id:
if entity_id := event.data_entity_id:
return entities_filter is None or entities_filter(entity_id)
if event.event_type in hass.data[DOMAIN]:
@ -615,10 +613,9 @@ def _augment_data_with_context(
return
event_type = context_event.event_type
context_entity_id = context_event.entity_id
# State change
if context_entity_id:
if context_entity_id := context_event.entity_id:
data["context_entity_id"] = context_entity_id
data["context_entity_id_name"] = _entity_name_from_event(
context_entity_id, context_event, entity_attr_cache

View File

@ -482,16 +482,14 @@ class MediaPlayerEntity(Entity):
if hasattr(self, "_attr_media_image_hash"):
return self._attr_media_image_hash
url = self.media_image_url
if url is not None:
if (url := self.media_image_url) is not None:
return hashlib.sha256(url.encode("utf-8")).hexdigest()[:16]
return None
async def async_get_media_image(self):
"""Fetch media image of current playing image."""
url = self.media_image_url
if url is None:
if (url := self.media_image_url) is None:
return None, None
return await self._async_fetch_image_from_cache(url)
@ -871,9 +869,7 @@ class MediaPlayerEntity(Entity):
@property
def media_image_local(self):
"""Return local url to media image."""
image_hash = self.media_image_hash
if image_hash is None:
if (image_hash := self.media_image_hash) is None:
return None
return (
@ -887,15 +883,15 @@ class MediaPlayerEntity(Entity):
supported_features = self.supported_features or 0
data = {}
if supported_features & SUPPORT_SELECT_SOURCE:
source_list = self.source_list
if source_list:
data[ATTR_INPUT_SOURCE_LIST] = source_list
if supported_features & SUPPORT_SELECT_SOURCE and (
source_list := self.source_list
):
data[ATTR_INPUT_SOURCE_LIST] = source_list
if supported_features & SUPPORT_SELECT_SOUND_MODE:
sound_mode_list = self.sound_mode_list
if sound_mode_list:
data[ATTR_SOUND_MODE_LIST] = sound_mode_list
if supported_features & SUPPORT_SELECT_SOUND_MODE and (
sound_mode_list := self.sound_mode_list
):
data[ATTR_SOUND_MODE_LIST] = sound_mode_list
return data

View File

@ -134,8 +134,7 @@ class MelCloudDevice:
"manufacturer": "Mitsubishi Electric",
"name": self.name,
}
unit_infos = self.device.units
if unit_infos is not None:
if (unit_infos := self.device.units) is not None:
_device_info["model"] = ", ".join(
[x["model"] for x in unit_infos if x["model"]]
)

View File

@ -143,8 +143,7 @@ class AtaDeviceClimate(MelCloudClimate):
"""Return the optional state attributes with device specific additions."""
attr = {}
vane_horizontal = self._device.vane_horizontal
if vane_horizontal:
if vane_horizontal := self._device.vane_horizontal:
attr.update(
{
ATTR_VANE_HORIZONTAL: vane_horizontal,
@ -152,8 +151,7 @@ class AtaDeviceClimate(MelCloudClimate):
}
)
vane_vertical = self._device.vane_vertical
if vane_vertical:
if vane_vertical := self._device.vane_vertical:
attr.update(
{
ATTR_VANE_VERTICAL: vane_vertical,

View File

@ -48,8 +48,7 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
try:
with timeout(10):
acquired_token = token
if acquired_token is None:
if (acquired_token := token) is None:
acquired_token = await pymelcloud.login(
username,
password,

View File

@ -656,8 +656,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
brightness = self._brightness
if brightness:
if brightness := self._brightness:
brightness = min(round(brightness), 255)
return brightness
@ -728,10 +727,8 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
@property
def white_value(self):
"""Return the white property."""
white_value = self._white_value
if white_value:
white_value = min(round(white_value), 255)
return white_value
if white_value := self._white_value:
return min(round(white_value), 255)
return None
@property

View File

@ -115,8 +115,7 @@ class MVGLiveSensor(SensorEntity):
@property
def extra_state_attributes(self):
"""Return the state attributes."""
dep = self.data.departures
if not dep:
if not (dep := self.data.departures):
return None
attr = dep[0] # next depature attributes
attr["departures"] = deepcopy(dep) # all departures dictionary

View File

@ -115,8 +115,7 @@ class SignalUpdateCallback:
if not event_message.resource_update_name:
return
device_id = event_message.resource_update_name
events = event_message.resource_update_events
if not events:
if not (events := event_message.resource_update_events):
return
_LOGGER.debug("Event Update %s", events.keys())
device_registry = await self._hass.helpers.device_registry.async_get_registry()

View File

@ -217,8 +217,7 @@ class NestCamera(Camera):
"""Return image from any active events happening."""
if CameraEventImageTrait.NAME not in self._device.traits:
return None
trait = self._device.active_event_trait
if not trait:
if not (trait := self._device.active_event_trait):
return None
# Reuse image bytes if they have already been fetched
if not isinstance(trait, EventImageGenerator):

View File

@ -153,8 +153,7 @@ class ThermostatEntity(ClimateEntity):
@property
def target_temperature(self) -> float | None:
"""Return the temperature currently set to be reached."""
trait = self._target_temperature_trait
if not trait:
if not (trait := self._target_temperature_trait):
return None
if self.hvac_mode == HVAC_MODE_HEAT:
return trait.heat_celsius
@ -167,8 +166,7 @@ class ThermostatEntity(ClimateEntity):
"""Return the upper bound target temperature."""
if self.hvac_mode != HVAC_MODE_HEAT_COOL:
return None
trait = self._target_temperature_trait
if not trait:
if not (trait := self._target_temperature_trait):
return None
return trait.cool_celsius
@ -177,8 +175,7 @@ class ThermostatEntity(ClimateEntity):
"""Return the lower bound target temperature."""
if self.hvac_mode != HVAC_MODE_HEAT_COOL:
return None
trait = self._target_temperature_trait
if not trait:
if not (trait := self._target_temperature_trait):
return None
return trait.heat_celsius

View File

@ -48,8 +48,7 @@ class NestDeviceInfo:
return trait.custom_name
# Build a name from the room/structure. Note: This room/structure name
# is not associated with a home assistant Area.
parent_relations = self._device.parent_relations
if parent_relations:
if parent_relations := self._device.parent_relations:
items = sorted(parent_relations.items())
names = [name for id, name in items]
return " ".join(names)

View File

@ -88,8 +88,7 @@ async def validate_input(hass: core.HomeAssistant, data):
data = PyNUTData(host, port, alias, username, password)
await hass.async_add_executor_job(data.update)
status = data.status
if not status:
if not (status := data.status):
raise CannotConnect
return {"ups_list": data.ups_list, "available_resources": status}