Use assignment expressions 31 (#58715)

This commit is contained in:
Marc Mueller 2021-10-30 16:29:07 +02:00 committed by GitHub
parent f7dea3aa1d
commit a48ddcadd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 27 additions and 58 deletions

View file

@ -139,8 +139,7 @@ class EnOceanSensor(EnOceanEntity, RestoreEntity, SensorEntity):
if self._attr_native_value is not None:
return
state = await self.async_get_last_state()
if state is not None:
if (state := await self.async_get_last_state()) is not None:
self._attr_native_value = state.state
def value_changed(self, packet):

View file

@ -198,8 +198,7 @@ async def async_setup(hass, config):
_LOGGER.info("Start envisalink")
controller.start()
result = await sync_connect
if not result:
if not await sync_connect:
return False
# Load sub-components for Envisalink

View file

@ -137,8 +137,7 @@ class RuntimeEntryData:
async def async_load_from_store(self) -> tuple[list[EntityInfo], list[UserService]]:
"""Load the retained data from store and return de-serialized data."""
restored = await self.store.async_load()
if restored is None:
if (restored := await self.store.async_load()) is None:
return [], []
restored = cast("dict[str, Any]", restored)
self._storage_contents = restored.copy()

View file

@ -49,8 +49,7 @@ class SpeedtestSensor(RestoreEntity, SensorEntity):
)
)
state = await self.async_get_last_state()
if not state:
if not (state := await self.async_get_last_state()):
return
self._attr_native_value = state.state

View file

@ -171,8 +171,7 @@ class GenericHygrostat(HumidifierEntity, RestoreEntity):
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup)
old_state = await self.async_get_last_state()
if old_state is not None:
if (old_state := await self.async_get_last_state()) is not None:
if old_state.attributes.get(ATTR_MODE) == MODE_AWAY:
self._is_away = True
self._saved_target_humidity = self._target_humidity

View file

@ -144,8 +144,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity):
# Restore the last activity so we know
# how what to turn on if nothing
# is specified
last_state = await self.async_get_last_state()
if not last_state:
if not (last_state := await self.async_get_last_state()):
return
if ATTR_LAST_ACTIVITY not in last_state.attributes:
return

View file

@ -124,8 +124,7 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
async def async_added_to_hass(self):
"""Handle entity which will be added."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
try:
self._state = Decimal(state.state)
except (DecimalException, ValueError) as err:

View file

@ -60,8 +60,7 @@ class Iperf3Sensor(RestoreEntity, SensorEntity):
)
)
state = await self.async_get_last_state()
if not state:
if not (state := await self.async_get_last_state()):
return
self._attr_native_value = state.state

View file

@ -117,8 +117,7 @@ class ISYLightEntity(ISYNodeEntity, LightEntity, RestoreEntity):
await super().async_added_to_hass()
self._last_brightness = self.brightness or 255
last_state = await self.async_get_last_state()
if not last_state:
if not (last_state := await self.async_get_last_state()):
return
if (

View file

@ -231,8 +231,7 @@ class LimitlessLEDGroup(LightEntity, RestoreEntity):
async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
if last_state := await self.async_get_last_state():
self._is_on = last_state.state == STATE_ON
self._brightness = last_state.attributes.get("brightness")
self._temperature = last_state.attributes.get("color_temp")

View file

@ -235,9 +235,7 @@ class DashboardsCollection(collection.StorageCollection):
async def _async_load_data(self) -> dict | None:
"""Load the data."""
data = await self.store.async_load()
if data is None:
if (data := await self.store.async_load()) is None:
return cast(Optional[dict], data)
updated = False

View file

@ -70,9 +70,7 @@ class ResourceStorageCollection(collection.StorageCollection):
async def _async_load_data(self) -> dict | None:
"""Load the data."""
data = await self.store.async_load()
if data is not None:
if (data := await self.store.async_load()) is not None:
return cast(Optional[dict], data)
# Import it from config.

View file

@ -427,8 +427,7 @@ class ManualAlarm(alarm.AlarmControlPanelEntity, RestoreEntity):
async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
if (
state.state in (STATE_ALARM_PENDING, STATE_ALARM_ARMING)
and hasattr(state, "attributes")

View file

@ -130,9 +130,7 @@ class OwnTracksEntity(TrackerEntity, RestoreEntity):
if self._data:
return
state = await self.async_get_last_state()
if state is None:
if (state := await self.async_get_last_state()) is None:
return
attr = state.attributes

View file

@ -420,8 +420,7 @@ class Person(RestoreEntity):
async def async_added_to_hass(self):
"""Register device trackers."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._parse_source_state(state)
if self.hass.is_running:

View file

@ -86,8 +86,7 @@ class PilightBaseDevice(RestoreEntity):
async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._is_on = state.state == STATE_ON
self._brightness = state.attributes.get("brightness")

View file

@ -52,8 +52,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def reload_service_handler(service):
"""Remove all user-defined groups and load new ones from config."""
conf = await component.async_prepare_reload()
if conf is None:
if (conf := await component.async_prepare_reload()) is None:
return
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS)
_async_setup_shared_data(hass)

View file

@ -117,8 +117,7 @@ class PwmSimpleLed(LightEntity, RestoreEntity):
async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
if last_state := await self.async_get_last_state():
self._is_on = last_state.state == STATE_ON
self._brightness = last_state.attributes.get(
"brightness", DEFAULT_BRIGHTNESS
@ -193,8 +192,7 @@ class PwmRgbLed(PwmSimpleLed):
async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
if last_state := await self.async_get_last_state():
self._color = last_state.attributes.get("hs_color", DEFAULT_COLOR)
@property

View file

@ -211,8 +211,7 @@ async def setup_smartapp_endpoint(hass: HomeAssistant):
# Get/create config to store a unique id for this hass instance.
store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
config = await store.async_load()
if not config:
if not (config := await store.async_load()):
# Create config
config = {
CONF_INSTANCE_ID: str(uuid4()),

View file

@ -100,6 +100,5 @@ class SpeedtestSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._state = state.state

View file

@ -117,8 +117,7 @@ class SwitchBotBotEntity(SwitchbotEntity, SwitchEntity, RestoreEntity):
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if not last_state:
if not (last_state := await self.async_get_last_state()):
return
self._attr_is_on = last_state.state == STATE_ON
self._last_run_success = last_state.attributes["last_run_success"]

View file

@ -148,8 +148,7 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
# restore state after startup
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._state = state.state == STATE_ON
# no need to listen for events

View file

@ -402,8 +402,7 @@ class TraccarEntity(TrackerEntity, RestoreEntity):
if self._latitude is not None or self._longitude is not None:
return
state = await self.async_get_last_state()
if state is None:
if (state := await self.async_get_last_state()) is None:
self._latitude = None
self._longitude = None
self._accuracy = None

View file

@ -297,8 +297,7 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
async_dispatcher_connect(self.hass, SIGNAL_RESET_METER, self.async_reset_meter)
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
try:
self._state = Decimal(state.state)
except InvalidOperation:

View file

@ -73,8 +73,7 @@ class WebSocketHandler:
# Exceptions if Socket disconnected or cancelled by connection handler
with suppress(RuntimeError, ConnectionResetError, *CANCELLATION_ERRORS):
while not self.wsock.closed:
message = await self._to_write.get()
if message is None:
if (message := await self._to_write.get()) is None:
break
self._logger.debug("Sending %s", message)

View file

@ -37,10 +37,8 @@ async def async_migrator(
async def old_conf_migrate_func(old_data)
"""
store_data = await store.async_load()
# If we already have store data we have already migrated in the past.
if store_data is not None:
if (store_data := await store.async_load()) is not None:
return store_data
def load_old_config():