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_LATITUDE] = data.latitude
self._attributes[CONF_LONGITUDE] = data.longitude self._attributes[CONF_LONGITUDE] = data.longitude
calls = data.estimated_calls if not (calls := data.estimated_calls):
if not calls:
self._state = None self._state = None
return return

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -138,8 +138,7 @@ class GdacsEvent(GeolocationEvent):
def _update_from_feed(self, feed_entry): def _update_from_feed(self, feed_entry):
"""Update the internal state from the provided feed entry.""" """Update the internal state from the provided feed entry."""
event_name = feed_entry.event_name if not (event_name := feed_entry.event_name):
if not event_name:
# Earthquakes usually don't have an event name. # Earthquakes usually don't have an event name.
event_name = f"{feed_entry.country} ({feed_entry.event_id})" event_name = f"{feed_entry.country} ({feed_entry.event_id})"
self._title = f"{feed_entry.event_type}: {event_name}" 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 async def async_update(self): # noqa: C901
"""Get the latest data from REST API.""" """Get the latest data from REST API."""
value = self.glances_data.api.data if (value := self.glances_data.api.data) is None:
if value is None:
return return
if self.entity_description.type == "fs": if self.entity_description.type == "fs":

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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