Use assignment expressions [other] (#66882)

This commit is contained in:
Marc Mueller 2022-02-19 17:22:51 +01:00 committed by GitHub
parent 4f20a8023b
commit 45d8d04c40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 15 additions and 35 deletions

View file

@ -47,14 +47,12 @@ class CameraMediaSource(MediaSource):
if not camera: if not camera:
raise Unresolvable(f"Could not resolve media item: {item.identifier}") raise Unresolvable(f"Could not resolve media item: {item.identifier}")
stream_type = camera.frontend_stream_type if (stream_type := camera.frontend_stream_type) is None:
if stream_type is None:
return PlayMedia( return PlayMedia(
f"/api/camera_proxy_stream/{camera.entity_id}", camera.content_type f"/api/camera_proxy_stream/{camera.entity_id}", camera.content_type
) )
if camera.frontend_stream_type != STREAM_TYPE_HLS: if stream_type != STREAM_TYPE_HLS:
raise Unresolvable("Camera does not support MJPEG or HLS streaming.") raise Unresolvable("Camera does not support MJPEG or HLS streaming.")
if "stream" not in self.hass.config.components: if "stream" not in self.hass.config.components:

View file

@ -317,8 +317,7 @@ async def config_entry_update(hass, connection, msg):
@websocket_api.async_response @websocket_api.async_response
async def config_entry_disable(hass, connection, msg): async def config_entry_disable(hass, connection, msg):
"""Disable config entry.""" """Disable config entry."""
disabled_by = msg["disabled_by"] if (disabled_by := msg["disabled_by"]) is not None:
if disabled_by is not None:
disabled_by = config_entries.ConfigEntryDisabler(disabled_by) disabled_by = config_entries.ConfigEntryDisabler(disabled_by)
result = False result = False

View file

@ -141,14 +141,11 @@ def _async_register_mac(
return return
ent_reg = er.async_get(hass) ent_reg = er.async_get(hass)
entity_id = ent_reg.async_get_entity_id(DOMAIN, *unique_id)
if entity_id is None: if (entity_id := ent_reg.async_get_entity_id(DOMAIN, *unique_id)) is None:
return return
entity_entry = ent_reg.async_get(entity_id) if (entity_entry := ent_reg.async_get(entity_id)) is None:
if entity_entry is None:
return return
# Make sure entity has a config entry and was disabled by the # Make sure entity has a config entry and was disabled by the

View file

@ -106,9 +106,8 @@ def handle_get(
): ):
"""List all possible diagnostic handlers.""" """List all possible diagnostic handlers."""
domain = msg["domain"] domain = msg["domain"]
info = hass.data[DOMAIN].get(domain)
if info is None: if (info := hass.data[DOMAIN].get(domain)) is None:
connection.send_error( connection.send_error(
msg["id"], websocket_api.ERR_NOT_FOUND, "Domain not supported" msg["id"], websocket_api.ERR_NOT_FOUND, "Domain not supported"
) )
@ -197,14 +196,11 @@ class DownloadDiagnosticsView(http.HomeAssistantView):
return web.Response(status=HTTPStatus.BAD_REQUEST) return web.Response(status=HTTPStatus.BAD_REQUEST)
hass = request.app["hass"] hass = request.app["hass"]
config_entry = hass.config_entries.async_get_entry(d_id)
if config_entry is None: if (config_entry := hass.config_entries.async_get_entry(d_id)) is None:
return web.Response(status=HTTPStatus.NOT_FOUND) return web.Response(status=HTTPStatus.NOT_FOUND)
info = hass.data[DOMAIN].get(config_entry.domain) if (info := hass.data[DOMAIN].get(config_entry.domain)) is None:
if info is None:
return web.Response(status=HTTPStatus.NOT_FOUND) return web.Response(status=HTTPStatus.NOT_FOUND)
filename = f"{config_entry.domain}-{config_entry.entry_id}" filename = f"{config_entry.domain}-{config_entry.entry_id}"
@ -226,9 +222,8 @@ class DownloadDiagnosticsView(http.HomeAssistantView):
dev_reg = async_get(hass) dev_reg = async_get(hass)
assert sub_id assert sub_id
device = dev_reg.async_get(sub_id)
if device is None: if (device := dev_reg.async_get(sub_id)) is None:
return web.Response(status=HTTPStatus.NOT_FOUND) return web.Response(status=HTTPStatus.NOT_FOUND)
filename += f"-{device.name}-{device.id}" filename += f"-{device.name}-{device.id}"

View file

@ -610,10 +610,8 @@ def _merge_config(entry, conf):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Load a config entry.""" """Load a config entry."""
conf = hass.data.get(DATA_MQTT_CONFIG)
# If user didn't have configuration.yaml config, generate defaults # If user didn't have configuration.yaml config, generate defaults
if conf is None: if (conf := hass.data.get(DATA_MQTT_CONFIG)) is None:
conf = CONFIG_SCHEMA({DOMAIN: dict(entry.data)})[DOMAIN] conf = CONFIG_SCHEMA({DOMAIN: dict(entry.data)})[DOMAIN]
elif any(key in conf for key in entry.data): elif any(key in conf for key in entry.data):
shared_keys = conf.keys() & entry.data.keys() shared_keys = conf.keys() & entry.data.keys()

View file

@ -839,8 +839,7 @@ def async_removed_from_device(
if "config_entries" not in event.data["changes"]: if "config_entries" not in event.data["changes"]:
return False return False
device_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
device_entry = device_registry.async_get(device_id) if not (device_entry := device_registry.async_get(device_id)):
if not device_entry:
# The device is already removed, do cleanup when we get "remove" event # The device is already removed, do cleanup when we get "remove" event
return False return False
if config_entry_id in device_entry.config_entries: if config_entry_id in device_entry.config_entries:

View file

@ -344,9 +344,7 @@ class SpeechManager:
This method is a coroutine. This method is a coroutine.
""" """
provider = self.providers.get(engine) if (provider := self.providers.get(engine)) is None:
if provider is None:
raise HomeAssistantError(f"Provider {engine} not found") raise HomeAssistantError(f"Provider {engine} not found")
msg_hash = hashlib.sha1(bytes(message, "utf-8")).hexdigest() msg_hash = hashlib.sha1(bytes(message, "utf-8")).hexdigest()

View file

@ -94,9 +94,7 @@ class TTSMediaSource(MediaSource):
) -> BrowseMediaSource: ) -> BrowseMediaSource:
"""Return provider item.""" """Return provider item."""
manager: SpeechManager = self.hass.data[DOMAIN] manager: SpeechManager = self.hass.data[DOMAIN]
provider = manager.providers.get(provider_domain) if (provider := manager.providers.get(provider_domain)) is None:
if provider is None:
raise BrowseError("Unknown provider") raise BrowseError("Unknown provider")
if params: if params:

View file

@ -79,8 +79,7 @@ async def async_attach_trigger(
): ):
return return
zone_state = hass.states.get(zone_entity_id) if not (zone_state := hass.states.get(zone_entity_id)):
if not zone_state:
_LOGGER.warning( _LOGGER.warning(
"Automation '%s' is referencing non-existing zone '%s' in a zone trigger", "Automation '%s' is referencing non-existing zone '%s' in a zone trigger",
automation_info["name"], automation_info["name"],

View file

@ -302,8 +302,7 @@ def async_is_device_config_entry_not_loaded(
) -> bool: ) -> bool:
"""Return whether device's config entries are not loaded.""" """Return whether device's config entries are not loaded."""
dev_reg = dr.async_get(hass) dev_reg = dr.async_get(hass)
device = dev_reg.async_get(device_id) if (device := dev_reg.async_get(device_id)) is None:
if device is None:
raise ValueError(f"Device {device_id} not found") raise ValueError(f"Device {device_id} not found")
return any( return any(
(entry := hass.config_entries.async_get_entry(entry_id)) (entry := hass.config_entries.async_get_entry(entry_id))