diff --git a/homeassistant/components/counter/__init__.py b/homeassistant/components/counter/__init__.py index 75b2b4902cb..d035d658206 100644 --- a/homeassistant/components/counter/__init__.py +++ b/homeassistant/components/counter/__init__.py @@ -240,14 +240,15 @@ 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: - 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) - self._config[CONF_MINIMUM] = state.attributes.get(ATTR_MINIMUM) - self._config[CONF_STEP] = state.attributes.get(ATTR_STEP) + 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) + self._config[CONF_MINIMUM] = state.attributes.get(ATTR_MINIMUM) + self._config[CONF_STEP] = state.attributes.get(ATTR_STEP) @callback def async_decrement(self) -> None: diff --git a/homeassistant/components/elkm1/alarm_control_panel.py b/homeassistant/components/elkm1/alarm_control_panel.py index 5b3a20b3448..04881b9f085 100644 --- a/homeassistant/components/elkm1/alarm_control_panel.py +++ b/homeassistant/components/elkm1/alarm_control_panel.py @@ -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: diff --git a/homeassistant/components/fireservicerota/sensor.py b/homeassistant/components/fireservicerota/sensor.py index a87b1609ec9..f2e415daec9 100644 --- a/homeassistant/components/fireservicerota/sensor.py +++ b/homeassistant/components/fireservicerota/sensor.py @@ -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: diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 6ad1c67f1f3..ef273b388ca 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -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 diff --git a/homeassistant/components/homekit/aidmanager.py b/homeassistant/components/homekit/aidmanager.py index 0f5e29426a8..c7ddc29a788 100644 --- a/homeassistant/components/homekit/aidmanager.py +++ b/homeassistant/components/homekit/aidmanager.py @@ -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 diff --git a/homeassistant/components/input_datetime/__init__.py b/homeassistant/components/input_datetime/__init__.py index de767d69ba8..d1a89fca67b 100644 --- a/homeassistant/components/input_datetime/__init__.py +++ b/homeassistant/components/input_datetime/__init__.py @@ -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 diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index c962f298586..682b3430807 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -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: diff --git a/homeassistant/components/modbus/binary_sensor.py b/homeassistant/components/modbus/binary_sensor.py index 171486639f4..07756b0f207 100644 --- a/homeassistant/components/modbus/binary_sensor.py +++ b/homeassistant/components/modbus/binary_sensor.py @@ -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: diff --git a/homeassistant/components/modbus/cover.py b/homeassistant/components/modbus/cover.py index 152239e88c7..1a9a7a82e9c 100644 --- a/homeassistant/components/modbus/cover.py +++ b/homeassistant/components/modbus/cover.py @@ -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, diff --git a/homeassistant/components/modbus/sensor.py b/homeassistant/components/modbus/sensor.py index 6911098dd94..e4249594940 100644 --- a/homeassistant/components/modbus/sensor.py +++ b/homeassistant/components/modbus/sensor.py @@ -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: diff --git a/homeassistant/components/mqtt/number.py b/homeassistant/components/mqtt/number.py index e3ff03b2fbe..f39ca870505 100644 --- a/homeassistant/components/mqtt/number.py +++ b/homeassistant/components/mqtt/number.py @@ -190,10 +190,8 @@ class MqttNumber(MqttEntity, NumberEntity, RestoreEntity): }, ) - if self._optimistic: - last_state = await self.async_get_last_state() - if last_state: - self._current_number = last_state.state + if self._optimistic and (last_state := await self.async_get_last_state()): + self._current_number = last_state.state @property def min_value(self) -> float: diff --git a/homeassistant/components/mqtt/select.py b/homeassistant/components/mqtt/select.py index 6ef0dbc3776..3857184a330 100644 --- a/homeassistant/components/mqtt/select.py +++ b/homeassistant/components/mqtt/select.py @@ -157,10 +157,8 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity): }, ) - if self._optimistic: - last_state = await self.async_get_last_state() - if last_state: - self._attr_current_option = last_state.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: """Update the current value.""" diff --git a/homeassistant/components/mqtt/switch.py b/homeassistant/components/mqtt/switch.py index 13a241f7ab1..d3252525b76 100644 --- a/homeassistant/components/mqtt/switch.py +++ b/homeassistant/components/mqtt/switch.py @@ -145,10 +145,8 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity): }, ) - if self._optimistic: - last_state = await self.async_get_last_state() - if last_state: - self._state = last_state.state == STATE_ON + if self._optimistic and (last_state := await self.async_get_last_state()): + self._state = last_state.state == STATE_ON @property def is_on(self): diff --git a/homeassistant/components/pvpc_hourly_pricing/sensor.py b/homeassistant/components/pvpc_hourly_pricing/sensor.py index 9cc5603e35b..1000d23b5bf 100644 --- a/homeassistant/components/pvpc_hourly_pricing/sensor.py +++ b/homeassistant/components/pvpc_hourly_pricing/sensor.py @@ -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 diff --git a/homeassistant/components/smart_meter_texas/sensor.py b/homeassistant/components/smart_meter_texas/sensor.py index ed5c84f0bce..e897048660f 100644 --- a/homeassistant/components/smart_meter_texas/sensor.py +++ b/homeassistant/components/smart_meter_texas/sensor.py @@ -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 diff --git a/homeassistant/components/zha/entity.py b/homeassistant/components/zha/entity.py index 0ba75a99306..80697c704bf 100644 --- a/homeassistant/components/zha/entity.py +++ b/homeassistant/components/zha/entity.py @@ -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(