Use assignment expressions 05 (#57785)

This commit is contained in:
Marc Mueller 2021-10-17 19:56:00 +02:00 committed by GitHub
parent d09ee11c54
commit 5048bad050
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 36 additions and 71 deletions

View file

@ -182,8 +182,7 @@ class AlexaCapability:
"""Serialize according to the Discovery API.""" """Serialize according to the Discovery API."""
result = {"type": "AlexaInterface", "interface": self.name(), "version": "3"} result = {"type": "AlexaInterface", "interface": self.name(), "version": "3"}
instance = self.instance if (instance := self.instance) is not None:
if instance is not None:
result["instance"] = instance result["instance"] = instance
properties_supported = self.properties_supported() properties_supported = self.properties_supported()
@ -264,8 +263,7 @@ class AlexaCapability:
"timeOfSample": dt_util.utcnow().strftime(DATE_FORMAT), "timeOfSample": dt_util.utcnow().strftime(DATE_FORMAT),
"uncertaintyInMilliseconds": 0, "uncertaintyInMilliseconds": 0,
} }
instance = self.instance if (instance := self.instance) is not None:
if instance is not None:
result["instance"] = instance result["instance"] = instance
yield result yield result

View file

@ -117,8 +117,7 @@ async def async_api_accept_grant(hass, config, directive, context):
async def async_api_turn_on(hass, config, directive, context): async def async_api_turn_on(hass, config, directive, context):
"""Process a turn on request.""" """Process a turn on request."""
entity = directive.entity entity = directive.entity
domain = entity.domain if (domain := entity.domain) == group.DOMAIN:
if domain == group.DOMAIN:
domain = ha.DOMAIN domain = ha.DOMAIN
service = SERVICE_TURN_ON service = SERVICE_TURN_ON

View file

@ -215,8 +215,7 @@ class AmcrestBinarySensor(BinarySensorEntity):
log_update_error(_LOGGER, "update", self.name, "binary sensor", error) log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
return return
event_code = self.entity_description.event_code if (event_code := self.entity_description.event_code) is None:
if event_code is None:
_LOGGER.error("Binary sensor %s event code not set", self.name) _LOGGER.error("Binary sensor %s event code not set", self.name)
return return
@ -228,12 +227,10 @@ class AmcrestBinarySensor(BinarySensorEntity):
def _update_unique_id(self) -> None: def _update_unique_id(self) -> None:
"""Set the unique id.""" """Set the unique id."""
if self._attr_unique_id is None: if self._attr_unique_id is None and (serial_number := self._api.serial_number):
serial_number = self._api.serial_number self._attr_unique_id = (
if serial_number: f"{serial_number}-{self.entity_description.key}-{self._channel}"
self._attr_unique_id = ( )
f"{serial_number}-{self.entity_description.key}-{self._channel}"
)
async def async_on_demand_update(self) -> None: async def async_on_demand_update(self) -> None:
"""Update state.""" """Update state."""

View file

@ -96,18 +96,14 @@ class AmcrestSensor(SensorEntity):
_LOGGER.debug("Updating %s sensor", self.name) _LOGGER.debug("Updating %s sensor", self.name)
sensor_type = self.entity_description.key sensor_type = self.entity_description.key
if self._attr_unique_id is None: if self._attr_unique_id is None and (serial_number := self._api.serial_number):
serial_number = self._api.serial_number self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
if serial_number:
self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
try: try:
if self._attr_unique_id is None: if self._attr_unique_id is None and (
serial_number = self._api.serial_number serial_number := self._api.serial_number
if serial_number: ):
self._attr_unique_id = ( self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
f"{serial_number}-{sensor_type}-{self._channel}"
)
if sensor_type == SENSOR_PTZ_PRESET: if sensor_type == SENSOR_PTZ_PRESET:
self._attr_native_value = self._api.ptz_presets_count self._attr_native_value = self._api.ptz_presets_count

View file

@ -147,8 +147,7 @@ class AquaLogicSensor(SensorEntity):
@callback @callback
def async_update_callback(self): def async_update_callback(self):
"""Update callback.""" """Update callback."""
panel = self._processor.panel if (panel := self._processor.panel) is not None:
if panel is not None:
if panel.is_metric: if panel.is_metric:
self._attr_native_unit_of_measurement = ( self._attr_native_unit_of_measurement = (
self.entity_description.unit_metric self.entity_description.unit_metric

View file

@ -66,23 +66,20 @@ class AquaLogicSwitch(SwitchEntity):
@property @property
def is_on(self): def is_on(self):
"""Return true if device is on.""" """Return true if device is on."""
panel = self._processor.panel if (panel := self._processor.panel) is None:
if panel is None:
return False return False
state = panel.get_state(self._state_name) state = panel.get_state(self._state_name)
return state return state
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
panel = self._processor.panel if (panel := self._processor.panel) is None:
if panel is None:
return return
panel.set_state(self._state_name, True) panel.set_state(self._state_name, True)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
panel = self._processor.panel if (panel := self._processor.panel) is None:
if panel is None:
return return
panel.set_state(self._state_name, False) panel.set_state(self._state_name, False)

View file

@ -375,9 +375,7 @@ class ArcamFmj(MediaPlayerEntity):
if source is None: if source is None:
return None return None
channel = self.media_channel if channel := self.media_channel:
if channel:
value = f"{source.name} - {channel}" value = f"{source.name} - {channel}"
else: else:
value = source.name value = source.name

View file

@ -145,13 +145,12 @@ class ArloCam(Camera):
def set_base_station_mode(self, mode): def set_base_station_mode(self, mode):
"""Set the mode in the base station.""" """Set the mode in the base station."""
# Get the list of base stations identified by library # Get the list of base stations identified by library
base_stations = self.hass.data[DATA_ARLO].base_stations
# Some Arlo cameras does not have base station # Some Arlo cameras does not have base station
# So check if there is base station detected first # So check if there is base station detected first
# if yes, then choose the primary base station # if yes, then choose the primary base station
# Set the mode on the chosen base station # Set the mode on the chosen base station
if base_stations: if base_stations := self.hass.data[DATA_ARLO].base_stations:
primary_base_station = base_stations[0] primary_base_station = base_stations[0]
primary_base_station.mode = mode primary_base_station.mode = mode

View file

@ -40,8 +40,7 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity):
@property @property
def hvac_action(self): def hvac_action(self):
"""Return the actual current HVAC action.""" """Return the actual current HVAC action."""
is_on = self._feature.is_on if not (is_on := self._feature.is_on):
if not is_on:
return None if is_on is None else CURRENT_HVAC_OFF return None if is_on is None else CURRENT_HVAC_OFF
# NOTE: In practice, there's no need to handle case when is_heating is None # NOTE: In practice, there's no need to handle case when is_heating is None

View file

@ -56,8 +56,7 @@ class BleBoxLightEntity(BleBoxEntity, LightEntity):
@property @property
def rgbw_color(self): def rgbw_color(self):
"""Return the hue and saturation.""" """Return the hue and saturation."""
rgbw_hex = self._feature.rgbw_hex if (rgbw_hex := self._feature.rgbw_hex) is None:
if rgbw_hex is None:
return None return None
return tuple(rgb_hex_to_rgb_list(rgbw_hex)[0:4]) return tuple(rgb_hex_to_rgb_list(rgbw_hex)[0:4])

View file

@ -145,8 +145,7 @@ class CalendarEventDevice(Entity):
@property @property
def state_attributes(self): def state_attributes(self):
"""Return the entity state attributes.""" """Return the entity state attributes."""
event = self.event if (event := self.event) is None:
if event is None:
return None return None
event = normalize_event(event) event = normalize_event(event)
@ -162,8 +161,7 @@ class CalendarEventDevice(Entity):
@property @property
def state(self): def state(self):
"""Return the state of the calendar event.""" """Return the state of the calendar event."""
event = self.event if (event := self.event) is None:
if event is None:
return STATE_OFF return STATE_OFF
event = normalize_event(event) event = normalize_event(event)

View file

@ -253,9 +253,7 @@ def _remote_handle_prefs_updated(cloud: Cloud) -> None:
if prefs.remote_enabled == cur_pref: if prefs.remote_enabled == cur_pref:
return return
cur_pref = prefs.remote_enabled if cur_pref := prefs.remote_enabled:
if cur_pref:
await cloud.remote.connect() await cloud.remote.connect()
else: else:
await cloud.remote.disconnect() await cloud.remote.disconnect()

View file

@ -143,10 +143,8 @@ class AlexaConfig(alexa_config.AbstractConfig):
else: else:
auxiliary_entity = False auxiliary_entity = False
default_expose = self._prefs.alexa_default_expose
# Backwards compat # Backwards compat
if default_expose is None: if (default_expose := self._prefs.alexa_default_expose) is None:
return not auxiliary_entity return not auxiliary_entity
return not auxiliary_entity and split_entity_id(entity_id)[0] in default_expose return not auxiliary_entity and split_entity_id(entity_id)[0] in default_expose

View file

@ -8,9 +8,7 @@ from aiohttp import payload, web
def aiohttp_serialize_response(response: web.Response) -> dict[str, Any]: def aiohttp_serialize_response(response: web.Response) -> dict[str, Any]:
"""Serialize an aiohttp response to a dictionary.""" """Serialize an aiohttp response to a dictionary."""
body = response.body if (body := response.body) is None:
if body is None:
pass pass
elif isinstance(body, payload.StringPayload): elif isinstance(body, payload.StringPayload):
# pylint: disable=protected-access # pylint: disable=protected-access

View file

@ -103,8 +103,7 @@ async def websocket_delete(hass, connection, msg):
@websocket_api.async_response @websocket_api.async_response
async def websocket_change_password(hass, connection, msg): async def websocket_change_password(hass, connection, msg):
"""Change current user password.""" """Change current user password."""
user = connection.user if (user := connection.user) is None:
if user is None:
connection.send_error(msg["id"], "user_not_found", "User not found") connection.send_error(msg["id"], "user_not_found", "User not found")
return return

View file

@ -120,8 +120,7 @@ class CoolmasterClimate(CoordinatorEntity, ClimateEntity):
def hvac_mode(self): def hvac_mode(self):
"""Return hvac target hvac state.""" """Return hvac target hvac state."""
mode = self._unit.mode mode = self._unit.mode
is_on = self._unit.is_on if not self._unit.is_on:
if not is_on:
return HVAC_MODE_OFF return HVAC_MODE_OFF
return CM_TO_HA_STATE[mode] return CM_TO_HA_STATE[mode]

View file

@ -212,9 +212,7 @@ class CoverEntity(Entity):
if self.is_closing: if self.is_closing:
return STATE_CLOSING return STATE_CLOSING
closed = self.is_closed if (closed := self.is_closed) is None:
if closed is None:
return None return None
return STATE_CLOSED if closed else STATE_OPEN return STATE_CLOSED if closed else STATE_OPEN
@ -225,13 +223,11 @@ class CoverEntity(Entity):
"""Return the state attributes.""" """Return the state attributes."""
data = {} data = {}
current = self.current_cover_position if (current := self.current_cover_position) is not None:
if current is not None: data[ATTR_CURRENT_POSITION] = current
data[ATTR_CURRENT_POSITION] = self.current_cover_position
current_tilt = self.current_cover_tilt_position if (current_tilt := self.current_cover_tilt_position) is not None:
if current_tilt is not None: data[ATTR_CURRENT_TILT_POSITION] = current_tilt
data[ATTR_CURRENT_TILT_POSITION] = self.current_cover_tilt_position
return data return data

View file

@ -92,8 +92,7 @@ class CurrencylayerSensor(SensorEntity):
def update(self): def update(self):
"""Update current date.""" """Update current date."""
self.rest.update() self.rest.update()
value = self.rest.data if (value := self.rest.data) is not None:
if value is not None:
self._state = round(value[f"{self._base}{self._quote}"], 4) self._state = round(value[f"{self._base}{self._quote}"], 4)

View file

@ -22,8 +22,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Daikin climate based on config_entry.""" """Set up Daikin climate based on config_entry."""
daikin_api = hass.data[DAIKIN_DOMAIN][entry.entry_id] daikin_api = hass.data[DAIKIN_DOMAIN][entry.entry_id]
switches = [] switches = []
zones = daikin_api.device.zones if zones := daikin_api.device.zones:
if zones:
switches.extend( switches.extend(
[ [
DaikinZoneSwitch(daikin_api, zone_id) DaikinZoneSwitch(daikin_api, zone_id)