Use assignment expressions 06 (#57786)

pull/56412/head
Marc Mueller 2021-10-17 20:05:11 +02:00 committed by GitHub
parent d5116810d4
commit 2a8eaf0e0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 35 additions and 69 deletions

View File

@ -206,8 +206,7 @@ class EnturPublicTransportSensor(SensorEntity):
self._attributes[CONF_LATITUDE] = data.latitude
self._attributes[CONF_LONGITUDE] = data.longitude
calls = data.estimated_calls
if not calls:
if not (calls := data.estimated_calls):
self._state = None
return

View File

@ -202,8 +202,7 @@ def get_forecast(ec_data, forecast_type):
forecast_array = []
if forecast_type == "daily":
half_days = ec_data.daily_forecasts
if not half_days:
if not (half_days := ec_data.daily_forecasts):
return None
today = {

View File

@ -281,8 +281,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity):
def color_mode(self) -> str | None:
"""Return the color mode of the light."""
if not self._supports_color_mode:
supported = self.supported_color_modes
if not supported:
if not (supported := self.supported_color_modes):
return None
return next(iter(supported))

View File

@ -458,13 +458,10 @@ class FanEntity(ToggleEntity):
@property
def speed(self) -> str | None:
"""Return the current speed."""
if self._implemented_preset_mode:
preset_mode = self.preset_mode
if preset_mode:
if self._implemented_preset_mode and (preset_mode := self.preset_mode):
return preset_mode
if self._implemented_percentage:
percentage = self.percentage
if percentage is None:
if (percentage := self.percentage) is None:
return None
return self.percentage_to_speed(percentage)
return None

View File

@ -56,8 +56,7 @@ class SpeedtestSensor(RestoreEntity, SensorEntity):
def update(self) -> None:
"""Get the latest data and update the states."""
data = self._speedtest_data.data # type: ignore[attr-defined]
if data is None:
if (data := self._speedtest_data.data) is None: # type: ignore[attr-defined]
return
self._attr_native_value = data["download"]

View File

@ -307,8 +307,7 @@ class FibaroController:
device.device_config = self._device_config.get(device.ha_id, {})
else:
device.mapped_type = None
dtype = device.mapped_type
if dtype is None:
if (dtype := device.mapped_type) is None:
continue
device.unique_id_str = f"{self.hub_serial}.{device.id}"
self._device_map[device.id] = device
@ -472,11 +471,10 @@ class FibaroDevice(Entity):
@property
def current_power_w(self):
"""Return the current power usage in W."""
if "power" in self.fibaro_device.properties:
power = self.fibaro_device.properties.power
if power:
if "power" in self.fibaro_device.properties and (
power := self.fibaro_device.properties.power
):
return convert(power, float, 0.0)
else:
return None
@property

View File

@ -218,8 +218,7 @@ class FidoSensor(SensorEntity):
async def async_update(self):
"""Get the latest data from Fido and update the state."""
await self.fido_data.async_update()
sensor_type = self.entity_description.key
if sensor_type == "balance":
if (sensor_type := self.entity_description.key) == "balance":
if self.fido_data.data.get(sensor_type) is not None:
self._attr_native_value = round(self.fido_data.data[sensor_type], 2)
else:

View File

@ -67,9 +67,8 @@ class IncidentsSensor(RestoreEntity, SensorEntity):
def extra_state_attributes(self) -> object:
"""Return available attributes for sensor."""
attr = {}
data = self._state_attributes
if not data:
if not (data := self._state_attributes):
return attr
for value in (

View File

@ -102,9 +102,7 @@ class FortiOSDeviceScanner(DeviceScanner):
device = device.lower()
data = self._clients_json
if data == 0:
if (data := self._clients_json) == 0:
_LOGGER.error("No json results to get device names")
return None

View File

@ -567,8 +567,7 @@ class IndexView(web_urldispatcher.AbstractResource):
def get_template(self) -> jinja2.Template:
"""Get template."""
tpl = self._template_cache
if tpl is None:
if (tpl := self._template_cache) is None:
with (_frontend_root(self.repo_path) / "index.html").open(
encoding="utf8"
) as file:

View File

@ -138,8 +138,7 @@ class GdacsEvent(GeolocationEvent):
def _update_from_feed(self, feed_entry):
"""Update the internal state from the provided feed entry."""
event_name = feed_entry.event_name
if not event_name:
if not (event_name := feed_entry.event_name):
# Earthquakes usually don't have an event name.
event_name = f"{feed_entry.country} ({feed_entry.event_id})"
self._title = f"{feed_entry.event_type}: {event_name}"

View File

@ -113,8 +113,7 @@ class GlancesSensor(SensorEntity):
async def async_update(self): # noqa: C901
"""Get the latest data from REST API."""
value = self.glances_data.api.data
if value is None:
if (value := self.glances_data.api.data) is None:
return
if self.entity_description.type == "fs":

View File

@ -262,9 +262,7 @@ class AbstractConfig(ABC):
@callback
def async_enable_local_sdk(self):
"""Enable the local SDK."""
webhook_id = self.local_sdk_webhook_id
if webhook_id is None:
if (webhook_id := self.local_sdk_webhook_id) is None:
return
try:

View File

@ -255,9 +255,7 @@ class BrightnessTrait(_Trait):
async def execute(self, command, data, params, challenge):
"""Execute a brightness command."""
domain = self.state.domain
if domain == light.DOMAIN:
if self.state.domain == light.DOMAIN:
await self.hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON,
@ -348,9 +346,7 @@ class OnOffTrait(_Trait):
async def execute(self, command, data, params, challenge):
"""Execute an OnOff command."""
domain = self.state.domain
if domain == group.DOMAIN:
if (domain := self.state.domain) == group.DOMAIN:
service_domain = HA_DOMAIN
service = SERVICE_TURN_ON if params["on"] else SERVICE_TURN_OFF
@ -1156,9 +1152,7 @@ class HumiditySettingTrait(_Trait):
async def execute(self, command, data, params, challenge):
"""Execute a humidity command."""
domain = self.state.domain
if domain == sensor.DOMAIN:
if self.state.domain == sensor.DOMAIN:
raise SmartHomeError(
ERR_NOT_SUPPORTED, "Execute is not supported by sensor"
)
@ -1449,8 +1443,7 @@ class FanSpeedTrait(_Trait):
async def execute_reverse(self, data, params):
"""Execute a Reverse command."""
domain = self.state.domain
if domain == fan.DOMAIN:
if self.state.domain == fan.DOMAIN:
if self.state.attributes.get(fan.ATTR_DIRECTION) == fan.DIRECTION_FORWARD:
direction = fan.DIRECTION_REVERSE
else:

View File

@ -212,8 +212,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity):
if self._last_activity:
activity = self._last_activity
else:
all_activities = self._data.activity_names
if all_activities:
if all_activities := self._data.activity_names:
activity = all_activities[0]
if activity:
@ -257,8 +256,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity):
_LOGGER.debug(
"%s: Writing hub configuration to file: %s", self.name, self._config_path
)
json_config = self._data.json_config
if json_config is None:
if (json_config := self._data.json_config) is None:
_LOGGER.warning("%s: No configuration received from hub", self.name)
return

View File

@ -416,11 +416,9 @@ class HERETravelTimeData:
# Convert location to HERE friendly location
destination = self.destination.split(",")
origin = self.origin.split(",")
arrival = self.arrival
if arrival is not None:
if (arrival := self.arrival) is not None:
arrival = convert_time_to_isodate(arrival)
departure = self.departure
if departure is not None:
if (departure := self.departure) is not None:
departure = convert_time_to_isodate(departure)
if departure is None and arrival is None:

View File

@ -311,8 +311,7 @@ class HomeAssistantScene(Scene):
def extra_state_attributes(self):
"""Return the scene state attributes."""
attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)}
unique_id = self.unique_id
if unique_id is not None:
if (unique_id := self.unique_id) is not None:
attributes[CONF_ID] = unique_id
return attributes

View File

@ -609,8 +609,7 @@ class WaterHeater(HomeAccessory):
self.char_display_units.set_value(unit)
# Update target operation mode
operation_mode = new_state.state
if operation_mode:
if new_state.state:
self.char_target_heat_cool.set_value(1) # Heat

View File

@ -91,8 +91,7 @@ class HomeAirQualitySensor(HomeKitEntity, AirQualityEntity):
"""Return the device state attributes."""
data = {"air_quality_text": self.air_quality_text}
voc = self.volatile_organic_compounds
if voc:
if voc := self.volatile_organic_compounds:
data["volatile_organic_compounds"] = voc
return data

View File

@ -544,8 +544,8 @@ class HomematicipSecuritySensorGroup(
@property
def is_on(self) -> bool:
"""Return true if safety issue detected."""
parent_is_on = super().is_on
if parent_is_on:
if super().is_on:
# parent is on
return True
if (

View File

@ -153,8 +153,7 @@ class HomematicipGenericEntity(Entity):
if not self.registry_entry:
return
device_id = self.registry_entry.device_id
if device_id:
if device_id := self.registry_entry.device_id:
# Remove from device registry.
device_registry = await dr.async_get_registry(self.hass)
if device_id in device_registry.devices:
@ -163,8 +162,7 @@ class HomematicipGenericEntity(Entity):
else:
# Remove from entity registry.
# Only relevant for entities that do not belong to a device.
entity_id = self.registry_entry.entity_id
if entity_id:
if entity_id := self.registry_entry.entity_id:
entity_registry = await er.async_get_registry(self.hass)
if entity_id in entity_registry.entities:
entity_registry.async_remove(entity_id)