From a48ddcadd4b4d04110b8a7edbb11b08d6b5f052f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 30 Oct 2021 16:29:07 +0200 Subject: [PATCH] Use assignment expressions 31 (#58715) --- homeassistant/components/enocean/sensor.py | 3 +-- homeassistant/components/envisalink/__init__.py | 3 +-- homeassistant/components/esphome/entry_data.py | 3 +-- homeassistant/components/fastdotcom/sensor.py | 3 +-- homeassistant/components/generic_hygrostat/humidifier.py | 3 +-- homeassistant/components/harmony/remote.py | 3 +-- homeassistant/components/integration/sensor.py | 3 +-- homeassistant/components/iperf3/sensor.py | 3 +-- homeassistant/components/isy994/light.py | 3 +-- homeassistant/components/limitlessled/light.py | 3 +-- homeassistant/components/lovelace/dashboard.py | 4 +--- homeassistant/components/lovelace/resources.py | 4 +--- homeassistant/components/manual/alarm_control_panel.py | 3 +-- homeassistant/components/owntracks/device_tracker.py | 4 +--- homeassistant/components/person/__init__.py | 3 +-- homeassistant/components/pilight/base_class.py | 3 +-- homeassistant/components/rest/__init__.py | 3 +-- homeassistant/components/rpi_gpio_pwm/light.py | 6 ++---- homeassistant/components/smartthings/smartapp.py | 3 +-- homeassistant/components/speedtestdotnet/sensor.py | 3 +-- homeassistant/components/switchbot/switch.py | 3 +-- homeassistant/components/template/switch.py | 3 +-- homeassistant/components/traccar/device_tracker.py | 3 +-- homeassistant/components/utility_meter/sensor.py | 3 +-- homeassistant/components/websocket_api/http.py | 3 +-- homeassistant/helpers/storage.py | 4 +--- 26 files changed, 27 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/enocean/sensor.py b/homeassistant/components/enocean/sensor.py index ca0f5e95109..9c4c9df73e9 100644 --- a/homeassistant/components/enocean/sensor.py +++ b/homeassistant/components/enocean/sensor.py @@ -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): diff --git a/homeassistant/components/envisalink/__init__.py b/homeassistant/components/envisalink/__init__.py index 75d4bff3dd1..d5a8b39e8f9 100644 --- a/homeassistant/components/envisalink/__init__.py +++ b/homeassistant/components/envisalink/__init__.py @@ -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 diff --git a/homeassistant/components/esphome/entry_data.py b/homeassistant/components/esphome/entry_data.py index 51fc18ee37e..847997731d4 100644 --- a/homeassistant/components/esphome/entry_data.py +++ b/homeassistant/components/esphome/entry_data.py @@ -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() diff --git a/homeassistant/components/fastdotcom/sensor.py b/homeassistant/components/fastdotcom/sensor.py index 2a82cee7cea..8363981b526 100644 --- a/homeassistant/components/fastdotcom/sensor.py +++ b/homeassistant/components/fastdotcom/sensor.py @@ -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 diff --git a/homeassistant/components/generic_hygrostat/humidifier.py b/homeassistant/components/generic_hygrostat/humidifier.py index 726b6e654e7..383674a7f75 100644 --- a/homeassistant/components/generic_hygrostat/humidifier.py +++ b/homeassistant/components/generic_hygrostat/humidifier.py @@ -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 diff --git a/homeassistant/components/harmony/remote.py b/homeassistant/components/harmony/remote.py index 3431eff7994..91022cf8d42 100644 --- a/homeassistant/components/harmony/remote.py +++ b/homeassistant/components/harmony/remote.py @@ -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 diff --git a/homeassistant/components/integration/sensor.py b/homeassistant/components/integration/sensor.py index a2fd77fb4e1..22e80e7879e 100644 --- a/homeassistant/components/integration/sensor.py +++ b/homeassistant/components/integration/sensor.py @@ -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: diff --git a/homeassistant/components/iperf3/sensor.py b/homeassistant/components/iperf3/sensor.py index dfc4abba707..4e102b69782 100644 --- a/homeassistant/components/iperf3/sensor.py +++ b/homeassistant/components/iperf3/sensor.py @@ -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 diff --git a/homeassistant/components/isy994/light.py b/homeassistant/components/isy994/light.py index 509fd259830..3d208d18fa9 100644 --- a/homeassistant/components/isy994/light.py +++ b/homeassistant/components/isy994/light.py @@ -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 ( diff --git a/homeassistant/components/limitlessled/light.py b/homeassistant/components/limitlessled/light.py index ac307f68d08..41b8f446541 100644 --- a/homeassistant/components/limitlessled/light.py +++ b/homeassistant/components/limitlessled/light.py @@ -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") diff --git a/homeassistant/components/lovelace/dashboard.py b/homeassistant/components/lovelace/dashboard.py index 3c9fb03d863..c02a65cc425 100644 --- a/homeassistant/components/lovelace/dashboard.py +++ b/homeassistant/components/lovelace/dashboard.py @@ -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 diff --git a/homeassistant/components/lovelace/resources.py b/homeassistant/components/lovelace/resources.py index 2a098361962..22297c54d6c 100644 --- a/homeassistant/components/lovelace/resources.py +++ b/homeassistant/components/lovelace/resources.py @@ -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. diff --git a/homeassistant/components/manual/alarm_control_panel.py b/homeassistant/components/manual/alarm_control_panel.py index d8b1ed088e3..39fc032b4b7 100644 --- a/homeassistant/components/manual/alarm_control_panel.py +++ b/homeassistant/components/manual/alarm_control_panel.py @@ -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") diff --git a/homeassistant/components/owntracks/device_tracker.py b/homeassistant/components/owntracks/device_tracker.py index 7ba9346013f..ca6f4f4a343 100644 --- a/homeassistant/components/owntracks/device_tracker.py +++ b/homeassistant/components/owntracks/device_tracker.py @@ -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 diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 4b7d6a54b1f..23b9bafcc47 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -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: diff --git a/homeassistant/components/pilight/base_class.py b/homeassistant/components/pilight/base_class.py index 95eb987875b..97ebaef0080 100644 --- a/homeassistant/components/pilight/base_class.py +++ b/homeassistant/components/pilight/base_class.py @@ -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") diff --git a/homeassistant/components/rest/__init__.py b/homeassistant/components/rest/__init__.py index f2cffebdfcb..ba101624673 100644 --- a/homeassistant/components/rest/__init__.py +++ b/homeassistant/components/rest/__init__.py @@ -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) diff --git a/homeassistant/components/rpi_gpio_pwm/light.py b/homeassistant/components/rpi_gpio_pwm/light.py index 0673047c6a7..efd3f03c9c0 100644 --- a/homeassistant/components/rpi_gpio_pwm/light.py +++ b/homeassistant/components/rpi_gpio_pwm/light.py @@ -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 diff --git a/homeassistant/components/smartthings/smartapp.py b/homeassistant/components/smartthings/smartapp.py index 2086d564753..8feb5f512d6 100644 --- a/homeassistant/components/smartthings/smartapp.py +++ b/homeassistant/components/smartthings/smartapp.py @@ -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()), diff --git a/homeassistant/components/speedtestdotnet/sensor.py b/homeassistant/components/speedtestdotnet/sensor.py index 06f180a570f..e6d2138bfc3 100644 --- a/homeassistant/components/speedtestdotnet/sensor.py +++ b/homeassistant/components/speedtestdotnet/sensor.py @@ -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 diff --git a/homeassistant/components/switchbot/switch.py b/homeassistant/components/switchbot/switch.py index 22e4bb33f1a..a2064eb16d6 100644 --- a/homeassistant/components/switchbot/switch.py +++ b/homeassistant/components/switchbot/switch.py @@ -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"] diff --git a/homeassistant/components/template/switch.py b/homeassistant/components/template/switch.py index 0e083df13f4..a654e59eaa2 100644 --- a/homeassistant/components/template/switch.py +++ b/homeassistant/components/template/switch.py @@ -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 diff --git a/homeassistant/components/traccar/device_tracker.py b/homeassistant/components/traccar/device_tracker.py index 16cd9ba94e5..d800123e3fa 100644 --- a/homeassistant/components/traccar/device_tracker.py +++ b/homeassistant/components/traccar/device_tracker.py @@ -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 diff --git a/homeassistant/components/utility_meter/sensor.py b/homeassistant/components/utility_meter/sensor.py index 64cde15aa4f..69770ec2445 100644 --- a/homeassistant/components/utility_meter/sensor.py +++ b/homeassistant/components/utility_meter/sensor.py @@ -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: diff --git a/homeassistant/components/websocket_api/http.py b/homeassistant/components/websocket_api/http.py index 8d75d50e59a..22fa9816f4e 100644 --- a/homeassistant/components/websocket_api/http.py +++ b/homeassistant/components/websocket_api/http.py @@ -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) diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 116c9186149..4d69d3be070 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -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():