Use assignment expressions 32 (#58716)

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

View file

@ -240,9 +240,10 @@ class Counter(RestoreEntity):
await super().async_added_to_hass()
# __init__ will set self._state to self._initial, only override
# if needed.
if self._config[CONF_RESTORE]:
state = await self.async_get_last_state()
if state is not None:
if (
self._config[CONF_RESTORE]
and (state := await self.async_get_last_state()) is not None
):
self._state = self.compute_next_state(int(state.state))
self._config[CONF_INITIAL] = state.attributes.get(ATTR_INITIAL)
self._config[CONF_MAXIMUM] = state.attributes.get(ATTR_MAXIMUM)

View file

@ -117,8 +117,7 @@ class ElkArea(ElkAttachedEntity, AlarmControlPanelEntity, RestoreEntity):
self._element.add_callback(self._watch_area)
# We do not get changed_by back from resync.
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_CHANGED_BY_KEYPAD in last_state.attributes:

View file

@ -102,8 +102,7 @@ class IncidentsSensor(RestoreEntity, SensorEntity):
"""Run when 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._state = state.state
self._state_attributes = state.attributes
if "id" in self._state_attributes:

View file

@ -421,9 +421,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa:
_LOGGER.warning("Not connected with the supervisor / system too busy!")
store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
data = await store.async_load()
if data is None:
if (data := await store.async_load()) is None:
data = {}
refresh_token = None

View file

@ -82,8 +82,7 @@ class AccessoryAidStorage:
aidstore = get_aid_storage_filename_for_entry_id(self._entry)
self.store = Store(self.hass, AID_MANAGER_STORAGE_VERSION, aidstore)
raw_storage = await self.store.async_load()
if not raw_storage:
if not (raw_storage := await self.store.async_load()):
# There is no data about aid allocations yet
return

View file

@ -270,8 +270,7 @@ class InputDatetime(RestoreEntity):
default_value = py_datetime.datetime.today().strftime("%Y-%m-%d 00:00:00")
# Priority 2: Old state
old_state = await self.async_get_last_state()
if old_state is None:
if (old_state := await self.async_get_last_state()) is None:
self._current_datetime = dt_util.parse_datetime(default_value)
return

View file

@ -254,8 +254,7 @@ class BaseSwitch(BasePlatform, ToggleEntity, RestoreEntity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await self.async_base_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._attr_is_on = state.state == STATE_ON
async def async_turn(self, command: int) -> None:

View file

@ -41,8 +41,7 @@ class ModbusBinarySensor(BasePlatform, RestoreEntity, BinarySensorEntity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await self.async_base_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._attr_is_on = state.state == STATE_ON
async def async_update(self, now: datetime | None = None) -> None:

View file

@ -100,8 +100,7 @@ class ModbusCover(BasePlatform, CoverEntity, RestoreEntity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await self.async_base_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
convert = {
STATE_CLOSED: self._state_closed,
STATE_CLOSING: self._state_closing,

View file

@ -53,8 +53,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity):
async def async_added_to_hass(self) -> None:
"""Handle entity which will be added."""
await self.async_base_added_to_hass()
state = await self.async_get_last_state()
if state:
if state := await self.async_get_last_state():
self._attr_native_value = state.state
async def async_update(self, now: datetime | None = None) -> None:

View file

@ -190,9 +190,7 @@ class MqttNumber(MqttEntity, NumberEntity, RestoreEntity):
},
)
if self._optimistic:
last_state = await self.async_get_last_state()
if last_state:
if self._optimistic and (last_state := await self.async_get_last_state()):
self._current_number = last_state.state
@property

View file

@ -157,9 +157,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
},
)
if self._optimistic:
last_state = await self.async_get_last_state()
if last_state:
if self._optimistic and (last_state := await self.async_get_last_state()):
self._attr_current_option = last_state.state
async def async_select_option(self, option: str) -> None:

View file

@ -145,9 +145,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
},
)
if self._optimistic:
last_state = await self.async_get_last_state()
if last_state:
if self._optimistic and (last_state := await self.async_get_last_state()):
self._state = last_state.state == STATE_ON
@property

View file

@ -66,8 +66,7 @@ class ElecPriceSensor(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._pvpc_data.state = state.state
# Update 'state' value in hour changes

View file

@ -92,7 +92,6 @@ class SmartMeterTexasSensor(CoordinatorEntity, RestoreEntity, SensorEntity):
if self.coordinator.last_update_success:
return
last_state = await self.async_get_last_state()
if last_state:
if last_state := await self.async_get_last_state():
self._state = last_state.state
self._available = True

View file

@ -206,8 +206,7 @@ class ZhaEntity(BaseZhaEntity, RestoreEntity):
if not self.zha_device.is_mains_powered:
# mains powered devices will get real time state
last_state = await self.async_get_last_state()
if last_state:
if last_state := await self.async_get_last_state():
self.async_restore_last_state(last_state)
self.async_accept_signal(