Use assignment expressions 20 (#57969)

This commit is contained in:
Marc Mueller 2021-10-20 20:31:00 +02:00 committed by GitHub
parent 398061706c
commit 487fa0a905
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 29 additions and 52 deletions

View file

@ -598,8 +598,7 @@ class MQTT:
"""
self = hass.data[DATA_MQTT]
conf = hass.data.get(DATA_MQTT_CONFIG)
if conf is None:
if (conf := hass.data.get(DATA_MQTT_CONFIG)) is None:
conf = CONFIG_SCHEMA({DOMAIN: dict(entry.data)})[DOMAIN]
self.conf = _merge_config(entry, conf)
@ -622,8 +621,7 @@ class MQTT:
else:
proto = mqtt.MQTTv311
client_id = self.conf.get(CONF_CLIENT_ID)
if client_id is None:
if (client_id := self.conf.get(CONF_CLIENT_ID)) is None:
# PAHO MQTT relies on the MQTT server to generate random client IDs.
# However, that feature is not mandatory so we generate our own.
client_id = mqtt.base62(uuid.uuid4().int, padding=22)
@ -637,9 +635,7 @@ class MQTT:
if username is not None:
self._mqttc.username_pw_set(username, password)
certificate = self.conf.get(CONF_CERTIFICATE)
if certificate == "auto":
if (certificate := self.conf.get(CONF_CERTIFICATE)) == "auto":
certificate = certifi.where()
client_key = self.conf.get(CONF_CLIENT_KEY)
@ -1002,8 +998,7 @@ async def websocket_remove_device(hass, connection, msg):
device_id = msg["device_id"]
dev_registry = await hass.helpers.device_registry.async_get_registry()
device = dev_registry.async_get(device_id)
if not device:
if not (device := dev_registry.async_get(device_id)):
connection.send_error(
msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found"
)

View file

@ -205,8 +205,7 @@ class MqttAlarm(MqttEntity, alarm.AlarmControlPanelEntity):
@property
def code_format(self):
"""Return one or more digits/characters."""
code = self._config.get(CONF_CODE)
if code is None:
if (code := self._config.get(CONF_CODE)) is None:
return None
if code == REMOTE_CODE or (isinstance(code, str) and re.search("^\\d+$", code)):
return alarm.FORMAT_NUMBER

View file

@ -142,8 +142,7 @@ async def async_start( # noqa: C901
payload[key] = f"{value[:-1]}{base}"
if payload.get(CONF_AVAILABILITY):
for availability_conf in payload[CONF_AVAILABILITY]:
topic = availability_conf.get(CONF_TOPIC)
if topic:
if topic := availability_conf.get(CONF_TOPIC):
if topic[0] == TOPIC_BASE:
availability_conf[CONF_TOPIC] = f"{base}{topic[1:]}"
if topic[-1] == TOPIC_BASE:

View file

@ -826,8 +826,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
def render_rgbx(color, template, color_mode):
"""Render RGBx payload."""
tpl = self._command_templates[template]
if tpl:
if tpl := self._command_templates[template]:
keys = ["red", "green", "blue"]
if color_mode == COLOR_MODE_RGBW:
keys.append("white")