Use assignment expressions 15 (#57961)

pull/57967/head
Marc Mueller 2021-10-18 14:01:23 +02:00 committed by GitHub
parent a421c524c1
commit 1e98761f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 29 additions and 66 deletions

View File

@ -46,9 +46,7 @@ CONFIG_SCHEMA = vol.Schema(
async def async_setup(hass, config):
"""Set up the emulated roku component."""
conf = config.get(DOMAIN)
if conf is None:
if (conf := config.get(DOMAIN)) is None:
return True
existing_servers = configured_servers(hass)

View File

@ -161,8 +161,7 @@ class EphEmberThermostat(ClimateEntity):
def set_temperature(self, **kwargs):
"""Set new target temperature."""
temperature = kwargs.get(ATTR_TEMPERATURE)
if temperature is None:
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return
if self._hot_water:

View File

@ -64,9 +64,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"Detection: %s %s - %s", ble_device.name, ble_device, advertisement_data
)
data = state.devices.get(ble_device.address)
if data:
if data := state.devices.get(ble_device.address):
data.device.detection_callback(ble_device, advertisement_data)
data.coordinator.async_set_updated_data(data.device.state)
else:

View File

@ -30,9 +30,7 @@ def resolve_location(hass, logger, loc):
def get_location_from_entity(hass, logger, entity_id):
"""Get the location from the entity state or attributes."""
entity = hass.states.get(entity_id)
if entity is None:
if (entity := hass.states.get(entity_id)) is None:
logger.error("Unable to find entity %s", entity_id)
return None

View File

@ -149,8 +149,7 @@ class GraphiteFeeder(threading.Thread):
def run(self):
"""Run the process to export the data."""
while True:
event = self._queue.get()
if event == self._quit_object:
if (event := self._queue.get()) == self._quit_object:
_LOGGER.debug("Event processing thread stopped")
self._queue.task_done()
return

View File

@ -227,8 +227,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity):
async def async_send_command(self, command, **kwargs):
"""Send a list of commands to one device."""
_LOGGER.debug("%s: Send Command", self.name)
device = kwargs.get(ATTR_DEVICE)
if device is None:
if (device := kwargs.get(ATTR_DEVICE)) is None:
_LOGGER.error("%s: Missing required argument: device", self.name)
return

View File

@ -86,9 +86,7 @@ async def async_attach_trigger(
automation_info: AutomationTriggerInfo,
) -> CALLBACK_TYPE:
"""Attach a trigger."""
trigger_type = config[CONF_TYPE]
if trigger_type == "target_humidity_changed":
if config[CONF_TYPE] == "target_humidity_changed":
numeric_state_config = {
numeric_state_trigger.CONF_PLATFORM: "numeric_state",
numeric_state_trigger.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
@ -118,9 +116,7 @@ async def async_get_trigger_capabilities(
hass: HomeAssistant, config: ConfigType
) -> dict[str, vol.Schema]:
"""List trigger capabilities."""
trigger_type = config[CONF_TYPE]
if trigger_type == "target_humidity_changed":
if config[CONF_TYPE] == "target_humidity_changed":
return {
"extra_fields": vol.Schema(
{

View File

@ -28,9 +28,7 @@ async def _async_reproduce_states(
reproduce_options: dict[str, Any] | None = None,
) -> None:
"""Reproduce component states."""
cur_state = hass.states.get(state.entity_id)
if cur_state is None:
if (cur_state := hass.states.get(state.entity_id)) is None:
_LOGGER.warning("Unable to find entity %s", state.entity_id)
return

View File

@ -33,8 +33,7 @@ class MatrixNotificationService(BaseNotificationService):
"""Send the message to the Matrix server."""
target_rooms = kwargs.get(ATTR_TARGET) or [self._default_room]
service_data = {ATTR_TARGET: target_rooms, ATTR_MESSAGE: message}
data = kwargs.get(ATTR_DATA)
if data is not None:
if (data := kwargs.get(ATTR_DATA)) is not None:
service_data[ATTR_DATA] = data
return self.hass.services.call(
DOMAIN, SERVICE_SEND_MESSAGE, service_data=service_data

View File

@ -79,8 +79,7 @@ class OneWireHub:
for device_path in self.owproxy.dir(path):
device_family = self.owproxy.read(f"{device_path}family").decode()
device_type = self.owproxy.read(f"{device_path}type").decode()
device_branches = DEVICE_COUPLERS.get(device_family)
if device_branches:
if device_branches := DEVICE_COUPLERS.get(device_family):
for branch in device_branches:
devices += self._discover_devices_owserver(f"{device_path}{branch}")
else:

View File

@ -138,8 +138,7 @@ class OpenGarageCover(CoordinatorEntity, CoverEntity):
@callback
def _update_attr(self) -> None:
"""Update the state and attributes."""
status = self.coordinator.data
if status is None:
if (status := self.coordinator.data) is None:
_LOGGER.error("Unable to connect to OpenGarage device")
self._attr_available = False
return

View File

@ -109,15 +109,13 @@ async def async_get_or_create_registered_webhook_id_and_url(hass, entry):
updated_config = False
webhook_url = None
webhook_id = config.get(CONF_WEBHOOK_ID)
if not webhook_id:
if not (webhook_id := config.get(CONF_WEBHOOK_ID)):
webhook_id = hass.components.webhook.async_generate_id()
config[CONF_WEBHOOK_ID] = webhook_id
updated_config = True
if hass.components.cloud.async_active_subscription():
cloudhook_url = config.get(CONF_CLOUDHOOK_URL)
if not cloudhook_url:
if not (cloudhook_url := config.get(CONF_CLOUDHOOK_URL)):
cloudhook_url = await hass.components.cloud.async_create_cloudhook(
webhook_id
)

View File

@ -94,8 +94,7 @@ class SlideCover(CoverEntity):
@property
def current_cover_position(self):
"""Return the current position of cover shutter."""
pos = self._slide["pos"]
if pos is not None:
if (pos := self._slide["pos"]) is not None:
if (1 - pos) <= DEFAULT_OFFSET or pos <= DEFAULT_OFFSET:
pos = round(pos)
if not self._invert:

View File

@ -54,9 +54,7 @@ def setup(hass, config):
def statsd_event_listener(event):
"""Listen for new messages on the bus and sends them to StatsD."""
state = event.data.get("new_state")
if state is None:
if (state := event.data.get("new_state")) is None:
return
try:

View File

@ -348,8 +348,7 @@ class Stream:
raise HomeAssistantError(f"Can't write {video_path}, no access to path!")
# Add recorder
recorder = self.outputs().get(RECORDER_PROVIDER)
if recorder:
if recorder := self.outputs().get(RECORDER_PROVIDER):
assert isinstance(recorder, RecorderOutput)
raise HomeAssistantError(
f"Stream already recording to {recorder.video_path}!"

View File

@ -123,13 +123,10 @@ class TelldusLiveEntity(Entity):
"identifiers": {("tellduslive", self.device.device_id)},
"name": self.device.name,
}
model = device.get("model")
if model is not None:
if (model := device.get("model")) is not None:
device_info["model"] = model.title()
protocol = device.get("protocol")
if protocol is not None:
if (protocol := device.get("protocol")) is not None:
device_info["manufacturer"] = protocol.title()
client = device.get("client")
if client is not None:
if (client := device.get("client")) is not None:
device_info["via_device"] = ("tellduslive", client)
return device_info

View File

@ -61,8 +61,7 @@ class VelbusClimate(VelbusEntity, ClimateEntity):
def set_temperature(self, **kwargs):
"""Set new target temperatures."""
temp = kwargs.get(ATTR_TEMPERATURE)
if temp is None:
if (temp := kwargs.get(ATTR_TEMPERATURE)) is None:
return
self._channel.set_temp(temp)
self.schedule_update_ha_state()

View File

@ -60,8 +60,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"""Set up the ViaggiaTreno platform."""
train_id = config.get(CONF_TRAIN_ID)
station_id = config.get(CONF_STATION_ID)
name = config.get(CONF_NAME)
if not name:
if not (name := config.get(CONF_NAME)):
name = DEFAULT_NAME.format(train_id)
async_add_entities([ViaggiaTrenoSensor(train_id, station_id, name)])

View File

@ -179,8 +179,7 @@ class VlcDevice(MediaPlayerEntity):
if not self._media_title:
# Fall back to filename.
data_info = data.get("data")
if data_info:
if data_info := data.get("data"):
self._media_title = data_info["filename"]
except CommandError as err:
@ -282,8 +281,7 @@ class VlcDevice(MediaPlayerEntity):
async def async_media_pause(self) -> None:
"""Send pause command."""
status = await self._vlc.status()
current_state = status.state
if current_state != "paused":
if status.state != "paused":
# Make sure we're not already paused since VLCTelnet.pause() toggles
# pause.
await self._vlc.pause()

View File

@ -29,9 +29,7 @@ def resolve_location(hass, logger, loc):
def get_location_from_entity(hass, logger, entity_id):
"""Get the location from the entity_id."""
state = hass.states.get(entity_id)
if state is None:
if (state := hass.states.get(entity_id)) is None:
logger.error("Unable to find entity %s", entity_id)
return None

View File

@ -256,8 +256,7 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self.cloud_devices = {}
for device in devices_raw:
parent_id = device.get("parent_id")
if not parent_id:
if not device.get("parent_id"):
name = device["name"]
model = device["model"]
list_name = f"{name} - {model}"

View File

@ -229,8 +229,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
params = {
key: value for key, value in service.data.items() if key != ATTR_ENTITY_ID
}
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
if entity_ids := service.data.get(ATTR_ENTITY_ID):
filtered_entities = [
entity
for entity in hass.data[DATA_KEY].values()

View File

@ -194,8 +194,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
for key, value in service.data.items()
if key != ATTR_ENTITY_ID
}
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
if entity_ids := service.data.get(ATTR_ENTITY_ID):
target_devices = [
dev
for dev in hass.data[DATA_KEY].values()

View File

@ -414,8 +414,7 @@ async def async_setup_other_entry(hass, config_entry, async_add_entities):
for key, value in service.data.items()
if key != ATTR_ENTITY_ID
}
entity_ids = service.data.get(ATTR_ENTITY_ID)
if entity_ids:
if entity_ids := service.data.get(ATTR_ENTITY_ID):
devices = [
device
for device in hass.data[DATA_KEY].values()