Use assignment expressions 08 ()

pull/56412/head
Marc Mueller 2021-10-17 20:02:42 +02:00 committed by GitHub
parent 5048bad050
commit d5116810d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
32 changed files with 62 additions and 109 deletions

View File

@ -25,10 +25,9 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
for vm_id in node_config["vms"]:
coordinator = host_name_coordinators[node_name][vm_id]
coordinator_data = coordinator.data
# unfound vm case
if coordinator_data is None:
if (coordinator_data := coordinator.data) is None:
continue
vm_name = coordinator_data["name"]
@ -39,10 +38,9 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
for container_id in node_config["containers"]:
coordinator = host_name_coordinators[node_name][container_id]
coordinator_data = coordinator.data
# unfound container case
if coordinator_data is None:
if (coordinator_data := coordinator.data) is None:
continue
container_name = coordinator_data["name"]
@ -88,9 +86,7 @@ class ProxmoxBinarySensor(ProxmoxEntity, BinarySensorEntity):
@property
def is_on(self):
"""Return the state of the binary sensor."""
data = self.coordinator.data
if data is None:
if (data := self.coordinator.data) is None:
return None
return data["status"] == "running"

View File

@ -160,9 +160,7 @@ class PS4Device(MediaPlayerEntity):
def _parse_status(self):
"""Parse status."""
status = self._ps4.status
if status is not None:
if (status := self._ps4.status) is not None:
self._games = load_games(self.hass, self._unique_id)
if self._games:
self.get_source_list()
@ -384,13 +382,15 @@ class PS4Device(MediaPlayerEntity):
@property
def entity_picture(self):
"""Return picture."""
if self._state == STATE_PLAYING and self._media_content_id is not None:
image_hash = self.media_image_hash
if image_hash is not None:
return (
f"/api/media_player_proxy/{self.entity_id}?"
f"token={self.access_token}&cache={image_hash}"
)
if (
self._state == STATE_PLAYING
and self._media_content_id is not None
and (image_hash := self.media_image_hash) is not None
):
return (
f"/api/media_player_proxy/{self.entity_id}?"
f"token={self.access_token}&cache={image_hash}"
)
return MEDIA_IMAGE_DEFAULT
@property

View File

@ -136,9 +136,7 @@ class EagleDataCoordinator(DataUpdateCoordinator):
async def _async_update_data_200(self):
"""Get the latest data from the Eagle-200 device."""
eagle200_meter = self.eagle200_meter
if eagle200_meter is None:
if (eagle200_meter := self.eagle200_meter) is None:
hub = aioeagle.EagleHub(
aiohttp_client.async_get_clientsession(self.hass),
self.cloud_id,

View File

@ -180,8 +180,7 @@ class SeventeenTrackPackageSensor(SensorEntity):
@property
def name(self):
"""Return the name."""
name = self._friendly_name
if not name:
if not (name := self._friendly_name):
name = self._tracking_number
return f"Seventeentrack Package: {name}"

View File

@ -171,10 +171,7 @@ class SmartTubError(SmartTubEntity, BinarySensorEntity):
@property
def extra_state_attributes(self):
"""Return the state attributes."""
error = self.error
if error is None:
if (error := self.error) is None:
return {}
return {

View File

@ -167,9 +167,8 @@ class SnmpSensor(SensorEntity):
async def async_update(self):
"""Get the latest data and updates the states."""
await self.data.async_update()
value = self.data.value
if value is None:
if (value := self.data.value) is None:
value = STATE_UNKNOWN
elif self._value_template is not None:
value = self._value_template.async_render_with_possible_json_value(

View File

@ -60,8 +60,7 @@ class TahomaBinarySensor(TahomaDevice, BinarySensorEntity):
def extra_state_attributes(self):
"""Return the device state attributes."""
attr = {}
super_attr = super().extra_state_attributes
if super_attr is not None:
if (super_attr := super().extra_state_attributes) is not None:
attr.update(super_attr)
if self._battery is not None:

View File

@ -197,8 +197,7 @@ class TahomaCover(TahomaDevice, CoverEntity):
def extra_state_attributes(self):
"""Return the device state attributes."""
attr = {}
super_attr = super().extra_state_attributes
if super_attr is not None:
if (super_attr := super().extra_state_attributes) is not None:
attr.update(super_attr)
if "core:Memorized1PositionState" in self.tahoma_device.active_states:

View File

@ -83,7 +83,6 @@ class TahomaLock(TahomaDevice, LockEntity):
attr = {
ATTR_BATTERY_LEVEL: self._battery_level,
}
super_attr = super().extra_state_attributes
if super_attr is not None:
if (super_attr := super().extra_state_attributes) is not None:
attr.update(super_attr)
return attr

View File

@ -113,8 +113,7 @@ class TahomaSensor(TahomaDevice, SensorEntity):
def extra_state_attributes(self):
"""Return the device state attributes."""
attr = {}
super_attr = super().extra_state_attributes
if super_attr is not None:
if (super_attr := super().extra_state_attributes) is not None:
attr.update(super_attr)
if "core:RSSILevelState" in self.tahoma_device.active_states:

View File

@ -108,8 +108,7 @@ class TahomaSwitch(TahomaDevice, SwitchEntity):
def extra_state_attributes(self):
"""Return the device state attributes."""
attr = {}
super_attr = super().extra_state_attributes
if super_attr is not None:
if (super_attr := super().extra_state_attributes) is not None:
attr.update(super_attr)
if "core:RSSILevelState" in self.tahoma_device.active_states:

View File

@ -66,8 +66,7 @@ class TellstickLight(TellstickDevice, LightEntity):
def _update_model(self, new_state, data):
"""Update the device entity state to match the arguments."""
if new_state:
brightness = data
if brightness is not None:
if (brightness := data) is not None:
self._brightness = brightness
# _brightness is not defined when called from super

View File

@ -259,8 +259,7 @@ class TradfriLight(TradfriBaseDevice, LightEntity):
transition_time = None
# HSB can always be set, but color temp + brightness is bulb dependent
command = dimmer_command
if command is not None:
if (command := dimmer_command) is not None:
command += color_command
else:
command = color_command

View File

@ -191,8 +191,7 @@ class TrainSensor(SensorEntity):
@property
def native_value(self):
"""Return the departure state."""
state = self._state
if state is not None:
if (state := self._state) is not None:
if state.time_at_location is not None:
return state.time_at_location
if state.estimated_time_at_location is not None:

View File

@ -101,8 +101,7 @@ class TransmissionSpeedSensor(TransmissionSensor):
def update(self):
"""Get the latest data from Transmission and updates the state."""
data = self._tm_client.api.data
if data:
if data := self._tm_client.api.data:
mb_spd = (
float(data.downloadSpeed)
if self._sub_type == "download"
@ -117,8 +116,7 @@ class TransmissionStatusSensor(TransmissionSensor):
def update(self):
"""Get the latest data from Transmission and updates the state."""
data = self._tm_client.api.data
if data:
if data := self._tm_client.api.data:
upload = data.uploadSpeed
download = data.downloadSpeed
if upload > 0 and download > 0:

View File

@ -172,8 +172,7 @@ class TravisCISensor(SensorEntity):
self._build = self._data.build(repo.last_build_id)
if self._build:
sensor_type = self.entity_description.key
if sensor_type == "state":
if (sensor_type := self.entity_description.key) == "state":
branch_stats = self._data.branch(self._branch, self._repo_name)
self._attr_native_value = branch_stats.state

View File

@ -256,8 +256,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
)
return
active_child = self._child_state
if active_child is None:
if (active_child := self._child_state) is None:
# No child to call service on
return
@ -307,8 +306,7 @@ class UniversalMediaPlayer(MediaPlayerEntity):
if (master_state == STATE_OFF) or (self._state_template is not None):
return master_state
active_child = self._child_state
if active_child:
if active_child := self._child_state:
return active_child.state
return master_state if master_state else STATE_OFF

View File

@ -71,9 +71,7 @@ class ValloxSensor(SensorEntity):
async def async_update(self) -> None:
"""Fetch state from the ventilation unit."""
metric_key = self.entity_description.metric_key
if metric_key is None:
if (metric_key := self.entity_description.metric_key) is None:
self._attr_available = False
_LOGGER.error("Error updating sensor. Empty metric key")
return

View File

@ -270,8 +270,7 @@ class VeraDevice(Generic[DeviceType], Entity):
attr[ATTR_ARMED] = "True" if armed else "False"
if self.vera_device.is_trippable:
last_tripped = self.vera_device.last_trip
if last_tripped is not None:
if (last_tripped := self.vera_device.last_trip) is not None:
utc_time = utc_from_timestamp(int(last_tripped))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
@ -279,12 +278,10 @@ class VeraDevice(Generic[DeviceType], Entity):
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = "True" if tripped else "False"
power = self.vera_device.power
if power:
if power := self.vera_device.power:
attr[ATTR_CURRENT_POWER_W] = convert(power, float, 0.0)
energy = self.vera_device.energy
if energy:
if energy := self.vera_device.energy:
attr[ATTR_CURRENT_ENERGY_KWH] = convert(energy, float, 0.0)
attr["Vera Device Id"] = self.vera_device.vera_device_id

View File

@ -114,9 +114,9 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
@property
def current_power_w(self) -> float | None:
"""Return the current power usage in W."""
power = self.vera_device.power
if power:
if power := self.vera_device.power:
return convert(power, float, 0.0)
return None
@property
def temperature_unit(self) -> str:

View File

@ -61,9 +61,9 @@ class VeraSwitch(VeraDevice[veraApi.VeraSwitch], SwitchEntity):
@property
def current_power_w(self) -> float | None:
"""Return the current power usage in W."""
power = self.vera_device.power
if power:
if power := self.vera_device.power:
return convert(power, float, 0.0)
return None
@property
def is_on(self) -> bool:

View File

@ -76,10 +76,11 @@ class VeSyncFanHA(VeSyncDevice, FanEntity):
@property
def percentage(self):
"""Return the current speed."""
if self.smartfan.mode == "manual":
current_level = self.smartfan.fan_level
if current_level is not None:
return ranged_value_to_percentage(SPEED_RANGE, current_level)
if (
self.smartfan.mode == "manual"
and (current_level := self.smartfan.fan_level) is not None
):
return ranged_value_to_percentage(SPEED_RANGE, current_level)
return None
@property

View File

@ -185,28 +185,22 @@ class WeatherEntity(Entity):
self.hass, self.temperature, self.temperature_unit, self.precision
)
humidity = self.humidity
if humidity is not None:
if (humidity := self.humidity) is not None:
data[ATTR_WEATHER_HUMIDITY] = round(humidity)
ozone = self.ozone
if ozone is not None:
if (ozone := self.ozone) is not None:
data[ATTR_WEATHER_OZONE] = ozone
pressure = self.pressure
if pressure is not None:
if (pressure := self.pressure) is not None:
data[ATTR_WEATHER_PRESSURE] = pressure
wind_bearing = self.wind_bearing
if wind_bearing is not None:
if (wind_bearing := self.wind_bearing) is not None:
data[ATTR_WEATHER_WIND_BEARING] = wind_bearing
wind_speed = self.wind_speed
if wind_speed is not None:
if (wind_speed := self.wind_speed) is not None:
data[ATTR_WEATHER_WIND_SPEED] = wind_speed
visibility = self.visibility
if visibility is not None:
if (visibility := self.visibility) is not None:
data[ATTR_WEATHER_VISIBILITY] = visibility
if self.forecast is not None:

View File

@ -40,8 +40,7 @@ class WemoEntity(CoordinatorEntity):
@property
def name(self) -> str:
"""Return the name of the device if any."""
suffix = self.name_suffix
if suffix:
if suffix := self.name_suffix:
return f"{self.wemo.name} {suffix}"
return self.wemo.name
@ -60,8 +59,7 @@ class WemoEntity(CoordinatorEntity):
@property
def unique_id(self) -> str:
"""Return the id of this WeMo device."""
suffix = self.unique_id_suffix
if suffix:
if suffix := self.unique_id_suffix:
return f"{self.wemo.serialnumber}_{suffix}"
return self.wemo.serialnumber

View File

@ -103,8 +103,7 @@ class WiffiIntegrationApi:
Remove listener for periodic callbacks.
"""
remove_listener = self._periodic_callback
if remove_listener is not None:
if (remove_listener := self._periodic_callback) is not None:
remove_listener()
async def __call__(self, device, metrics):

View File

@ -127,8 +127,7 @@ class XboxMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
@property
def media_title(self):
"""Title of current playing media."""
app_details = self.data.app_details
if not app_details:
if not (app_details := self.data.app_details):
return None
return (
app_details.localized_properties[0].product_title
@ -138,8 +137,7 @@ class XboxMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
@property
def media_image_url(self):
"""Image url of current playing media."""
app_details = self.data.app_details
if not app_details:
if not (app_details := self.data.app_details):
return None
image = _find_media_image(app_details.localized_properties[0].images)

View File

@ -664,8 +664,7 @@ class MusicCastMediaPlayer(MusicCastDeviceEntity, MediaPlayerEntity):
"""Return all media players of the current group, if the media player is server."""
if self.is_client:
# If we are a client we can still share group information, but we will take them from the server.
server = self.group_server
if server != self:
if (server := self.group_server) != self:
return server.musiccast_group
return [self]

View File

@ -580,8 +580,7 @@ class ZenWithinThermostat(Thermostat):
def _rm_rs_action(self) -> str | None:
"""Return the current HVAC action based on running mode and running state."""
running_state = self._thrm.running_state
if running_state is None:
if (running_state := self._thrm.running_state) is None:
return None
if running_state & (RunningState.HEAT | RunningState.HEAT_STAGE_2):
return CURRENT_HVAC_HEAT

View File

@ -388,8 +388,7 @@ class SmartEnergyMetering(Sensor):
attrs = {}
if self._channel.device_type is not None:
attrs["device_type"] = self._channel.device_type
status = self._channel.status
if status is not None:
if (status := self._channel.status) is not None:
attrs["status"] = str(status)[len(status.__class__.__name__) + 1 :]
return attrs
@ -578,8 +577,7 @@ class ZenHVACAction(ThermostatHVACAction):
def _rm_rs_action(self) -> str | None:
"""Return the current HVAC action based on running mode and running state."""
running_state = self._channel.running_state
if running_state is None:
if (running_state := self._channel.running_state) is None:
return None
rs_heat = (

View File

@ -111,8 +111,7 @@ class ZMSensorMonitors(SensorEntity):
def update(self):
"""Update the sensor."""
state = self._monitor.function
if not state:
if not (state := self._monitor.function):
self._state = None
else:
self._state = state.value

View File

@ -248,8 +248,7 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity):
self._preset_list = []
self._preset_mapping = {}
mode_list = self._mode().data_items
if mode_list:
if mode_list := self._mode().data_items:
for mode in mode_list:
ha_mode = HVAC_STATE_MAPPINGS.get(str(mode).lower())
ha_preset = PRESET_MAPPINGS.get(str(mode).lower())
@ -342,8 +341,7 @@ class ZWaveClimateBase(ZWaveDeviceEntity, ClimateEntity):
"""Update fan mode."""
if self.values.fan_mode:
self._current_fan_mode = self.values.fan_mode.data
fan_modes = self.values.fan_mode.data_items
if fan_modes:
if fan_modes := self.values.fan_mode.data_items:
self._fan_modes = list(fan_modes)
_LOGGER.debug("self._fan_modes=%s", self._fan_modes)

View File

@ -53,8 +53,7 @@ class DiscoveryFlowHandler(config_entries.ConfigFlow):
# Get current discovered entries.
in_progress = self._async_in_progress()
has_devices = in_progress
if not has_devices:
if not (has_devices := in_progress):
has_devices = await self.hass.async_add_job( # type: ignore
self._discovery_function, self.hass
)