mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Use assignment expressions 05 (#57785)
This commit is contained in:
parent
d09ee11c54
commit
5048bad050
@ -182,8 +182,7 @@ class AlexaCapability:
|
||||
"""Serialize according to the Discovery API."""
|
||||
result = {"type": "AlexaInterface", "interface": self.name(), "version": "3"}
|
||||
|
||||
instance = self.instance
|
||||
if instance is not None:
|
||||
if (instance := self.instance) is not None:
|
||||
result["instance"] = instance
|
||||
|
||||
properties_supported = self.properties_supported()
|
||||
@ -264,8 +263,7 @@ class AlexaCapability:
|
||||
"timeOfSample": dt_util.utcnow().strftime(DATE_FORMAT),
|
||||
"uncertaintyInMilliseconds": 0,
|
||||
}
|
||||
instance = self.instance
|
||||
if instance is not None:
|
||||
if (instance := self.instance) is not None:
|
||||
result["instance"] = instance
|
||||
|
||||
yield result
|
||||
|
@ -117,8 +117,7 @@ async def async_api_accept_grant(hass, config, directive, context):
|
||||
async def async_api_turn_on(hass, config, directive, context):
|
||||
"""Process a turn on request."""
|
||||
entity = directive.entity
|
||||
domain = entity.domain
|
||||
if domain == group.DOMAIN:
|
||||
if (domain := entity.domain) == group.DOMAIN:
|
||||
domain = ha.DOMAIN
|
||||
|
||||
service = SERVICE_TURN_ON
|
||||
|
@ -215,8 +215,7 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
log_update_error(_LOGGER, "update", self.name, "binary sensor", error)
|
||||
return
|
||||
|
||||
event_code = self.entity_description.event_code
|
||||
if event_code is None:
|
||||
if (event_code := self.entity_description.event_code) is None:
|
||||
_LOGGER.error("Binary sensor %s event code not set", self.name)
|
||||
return
|
||||
|
||||
@ -228,12 +227,10 @@ class AmcrestBinarySensor(BinarySensorEntity):
|
||||
|
||||
def _update_unique_id(self) -> None:
|
||||
"""Set the unique id."""
|
||||
if self._attr_unique_id is None:
|
||||
serial_number = self._api.serial_number
|
||||
if serial_number:
|
||||
self._attr_unique_id = (
|
||||
f"{serial_number}-{self.entity_description.key}-{self._channel}"
|
||||
)
|
||||
if self._attr_unique_id is None and (serial_number := self._api.serial_number):
|
||||
self._attr_unique_id = (
|
||||
f"{serial_number}-{self.entity_description.key}-{self._channel}"
|
||||
)
|
||||
|
||||
async def async_on_demand_update(self) -> None:
|
||||
"""Update state."""
|
||||
|
@ -96,18 +96,14 @@ class AmcrestSensor(SensorEntity):
|
||||
_LOGGER.debug("Updating %s sensor", self.name)
|
||||
|
||||
sensor_type = self.entity_description.key
|
||||
if self._attr_unique_id is None:
|
||||
serial_number = self._api.serial_number
|
||||
if serial_number:
|
||||
self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
|
||||
if self._attr_unique_id is None and (serial_number := self._api.serial_number):
|
||||
self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
|
||||
|
||||
try:
|
||||
if self._attr_unique_id is None:
|
||||
serial_number = self._api.serial_number
|
||||
if serial_number:
|
||||
self._attr_unique_id = (
|
||||
f"{serial_number}-{sensor_type}-{self._channel}"
|
||||
)
|
||||
if self._attr_unique_id is None and (
|
||||
serial_number := self._api.serial_number
|
||||
):
|
||||
self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}"
|
||||
|
||||
if sensor_type == SENSOR_PTZ_PRESET:
|
||||
self._attr_native_value = self._api.ptz_presets_count
|
||||
|
@ -147,8 +147,7 @@ class AquaLogicSensor(SensorEntity):
|
||||
@callback
|
||||
def async_update_callback(self):
|
||||
"""Update callback."""
|
||||
panel = self._processor.panel
|
||||
if panel is not None:
|
||||
if (panel := self._processor.panel) is not None:
|
||||
if panel.is_metric:
|
||||
self._attr_native_unit_of_measurement = (
|
||||
self.entity_description.unit_metric
|
||||
|
@ -66,23 +66,20 @@ class AquaLogicSwitch(SwitchEntity):
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
panel = self._processor.panel
|
||||
if panel is None:
|
||||
if (panel := self._processor.panel) is None:
|
||||
return False
|
||||
state = panel.get_state(self._state_name)
|
||||
return state
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
panel = self._processor.panel
|
||||
if panel is None:
|
||||
if (panel := self._processor.panel) is None:
|
||||
return
|
||||
panel.set_state(self._state_name, True)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
panel = self._processor.panel
|
||||
if panel is None:
|
||||
if (panel := self._processor.panel) is None:
|
||||
return
|
||||
panel.set_state(self._state_name, False)
|
||||
|
||||
|
@ -375,9 +375,7 @@ class ArcamFmj(MediaPlayerEntity):
|
||||
if source is None:
|
||||
return None
|
||||
|
||||
channel = self.media_channel
|
||||
|
||||
if channel:
|
||||
if channel := self.media_channel:
|
||||
value = f"{source.name} - {channel}"
|
||||
else:
|
||||
value = source.name
|
||||
|
@ -145,13 +145,12 @@ class ArloCam(Camera):
|
||||
def set_base_station_mode(self, mode):
|
||||
"""Set the mode in the base station."""
|
||||
# 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
|
||||
# So check if there is base station detected first
|
||||
# if yes, then choose the primary 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.mode = mode
|
||||
|
||||
|
@ -40,8 +40,7 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity):
|
||||
@property
|
||||
def hvac_action(self):
|
||||
"""Return the actual current HVAC action."""
|
||||
is_on = self._feature.is_on
|
||||
if not is_on:
|
||||
if not (is_on := self._feature.is_on):
|
||||
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
|
||||
|
@ -56,8 +56,7 @@ class BleBoxLightEntity(BleBoxEntity, LightEntity):
|
||||
@property
|
||||
def rgbw_color(self):
|
||||
"""Return the hue and saturation."""
|
||||
rgbw_hex = self._feature.rgbw_hex
|
||||
if rgbw_hex is None:
|
||||
if (rgbw_hex := self._feature.rgbw_hex) is None:
|
||||
return None
|
||||
|
||||
return tuple(rgb_hex_to_rgb_list(rgbw_hex)[0:4])
|
||||
|
@ -145,8 +145,7 @@ class CalendarEventDevice(Entity):
|
||||
@property
|
||||
def state_attributes(self):
|
||||
"""Return the entity state attributes."""
|
||||
event = self.event
|
||||
if event is None:
|
||||
if (event := self.event) is None:
|
||||
return None
|
||||
|
||||
event = normalize_event(event)
|
||||
@ -162,8 +161,7 @@ class CalendarEventDevice(Entity):
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the calendar event."""
|
||||
event = self.event
|
||||
if event is None:
|
||||
if (event := self.event) is None:
|
||||
return STATE_OFF
|
||||
|
||||
event = normalize_event(event)
|
||||
|
@ -253,9 +253,7 @@ def _remote_handle_prefs_updated(cloud: Cloud) -> None:
|
||||
if prefs.remote_enabled == cur_pref:
|
||||
return
|
||||
|
||||
cur_pref = prefs.remote_enabled
|
||||
|
||||
if cur_pref:
|
||||
if cur_pref := prefs.remote_enabled:
|
||||
await cloud.remote.connect()
|
||||
else:
|
||||
await cloud.remote.disconnect()
|
||||
|
@ -143,10 +143,8 @@ class AlexaConfig(alexa_config.AbstractConfig):
|
||||
else:
|
||||
auxiliary_entity = False
|
||||
|
||||
default_expose = self._prefs.alexa_default_expose
|
||||
|
||||
# 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 and split_entity_id(entity_id)[0] in default_expose
|
||||
|
@ -8,9 +8,7 @@ from aiohttp import payload, web
|
||||
|
||||
def aiohttp_serialize_response(response: web.Response) -> dict[str, Any]:
|
||||
"""Serialize an aiohttp response to a dictionary."""
|
||||
body = response.body
|
||||
|
||||
if body is None:
|
||||
if (body := response.body) is None:
|
||||
pass
|
||||
elif isinstance(body, payload.StringPayload):
|
||||
# pylint: disable=protected-access
|
||||
|
@ -103,8 +103,7 @@ async def websocket_delete(hass, connection, msg):
|
||||
@websocket_api.async_response
|
||||
async def websocket_change_password(hass, connection, msg):
|
||||
"""Change current user password."""
|
||||
user = connection.user
|
||||
if user is None:
|
||||
if (user := connection.user) is None:
|
||||
connection.send_error(msg["id"], "user_not_found", "User not found")
|
||||
return
|
||||
|
||||
|
@ -120,8 +120,7 @@ class CoolmasterClimate(CoordinatorEntity, ClimateEntity):
|
||||
def hvac_mode(self):
|
||||
"""Return hvac target hvac state."""
|
||||
mode = self._unit.mode
|
||||
is_on = self._unit.is_on
|
||||
if not is_on:
|
||||
if not self._unit.is_on:
|
||||
return HVAC_MODE_OFF
|
||||
|
||||
return CM_TO_HA_STATE[mode]
|
||||
|
@ -212,9 +212,7 @@ class CoverEntity(Entity):
|
||||
if self.is_closing:
|
||||
return STATE_CLOSING
|
||||
|
||||
closed = self.is_closed
|
||||
|
||||
if closed is None:
|
||||
if (closed := self.is_closed) is None:
|
||||
return None
|
||||
|
||||
return STATE_CLOSED if closed else STATE_OPEN
|
||||
@ -225,13 +223,11 @@ class CoverEntity(Entity):
|
||||
"""Return the state attributes."""
|
||||
data = {}
|
||||
|
||||
current = self.current_cover_position
|
||||
if current is not None:
|
||||
data[ATTR_CURRENT_POSITION] = self.current_cover_position
|
||||
if (current := self.current_cover_position) is not None:
|
||||
data[ATTR_CURRENT_POSITION] = current
|
||||
|
||||
current_tilt = self.current_cover_tilt_position
|
||||
if current_tilt is not None:
|
||||
data[ATTR_CURRENT_TILT_POSITION] = self.current_cover_tilt_position
|
||||
if (current_tilt := self.current_cover_tilt_position) is not None:
|
||||
data[ATTR_CURRENT_TILT_POSITION] = current_tilt
|
||||
|
||||
return data
|
||||
|
||||
|
@ -92,8 +92,7 @@ class CurrencylayerSensor(SensorEntity):
|
||||
def update(self):
|
||||
"""Update current date."""
|
||||
self.rest.update()
|
||||
value = self.rest.data
|
||||
if value is not None:
|
||||
if (value := self.rest.data) is not None:
|
||||
self._state = round(value[f"{self._base}{self._quote}"], 4)
|
||||
|
||||
|
||||
|
@ -22,8 +22,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Set up Daikin climate based on config_entry."""
|
||||
daikin_api = hass.data[DAIKIN_DOMAIN][entry.entry_id]
|
||||
switches = []
|
||||
zones = daikin_api.device.zones
|
||||
if zones:
|
||||
if zones := daikin_api.device.zones:
|
||||
switches.extend(
|
||||
[
|
||||
DaikinZoneSwitch(daikin_api, zone_id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user