From 7063c05127d40b3707d683b71e5ac5bd9bd86e84 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 30 Oct 2021 16:32:41 +0200 Subject: [PATCH] Use assignment expressions 33 (#58717) --- homeassistant/components/alexa/config.py | 3 +-- homeassistant/components/august/lock.py | 3 +-- homeassistant/components/automation/__init__.py | 6 ++---- homeassistant/components/camera/prefs.py | 4 +--- homeassistant/components/cloud/prefs.py | 4 +--- homeassistant/components/derivative/sensor.py | 3 +-- homeassistant/components/device_tracker/legacy.py | 3 +-- homeassistant/components/generic_thermostat/climate.py | 3 +-- homeassistant/components/geofency/device_tracker.py | 4 +--- homeassistant/components/google_assistant/helpers.py | 3 +-- homeassistant/components/gpslogger/device_tracker.py | 3 +-- homeassistant/components/group/__init__.py | 3 +-- homeassistant/components/homekit_controller/storage.py | 3 +-- homeassistant/components/mobile_app/__init__.py | 3 +-- homeassistant/components/mobile_app/device_tracker.py | 4 +--- homeassistant/components/mobile_app/entity.py | 3 +-- homeassistant/components/onboarding/__init__.py | 4 +--- homeassistant/components/rflink/__init__.py | 4 +--- homeassistant/components/rflink/cover.py | 4 +--- homeassistant/components/script/__init__.py | 3 +-- homeassistant/components/unifi/__init__.py | 4 +--- 21 files changed, 22 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/alexa/config.py b/homeassistant/components/alexa/config.py index cc5c604dc8c..739ce6be6a3 100644 --- a/homeassistant/components/alexa/config.py +++ b/homeassistant/components/alexa/config.py @@ -64,8 +64,7 @@ class AbstractConfig(ABC): async def async_disable_proactive_mode(self): """Disable proactive mode.""" - unsub_func = await self._unsub_proactive_report - if unsub_func: + if unsub_func := await self._unsub_proactive_report: unsub_func() self._unsub_proactive_report = None diff --git a/homeassistant/components/august/lock.py b/homeassistant/components/august/lock.py index 5f4fe85bc71..665b0036557 100644 --- a/homeassistant/components/august/lock.py +++ b/homeassistant/components/august/lock.py @@ -119,8 +119,7 @@ class AugustLock(AugustEntityMixin, RestoreEntity, LockEntity): """Restore ATTR_CHANGED_BY on startup since it is likely no longer in the activity log.""" 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 if ATTR_CHANGED_BY in last_state.attributes: diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 92fbd0e8b04..08764b35ea3 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -262,8 +262,7 @@ async def async_setup(hass, config): async def reload_service_handler(service_call): """Remove all automations 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 async_get_blueprints(hass).async_reset_cache() await _async_process_config(hass, conf, component) @@ -392,8 +391,7 @@ class AutomationEntity(ToggleEntity, RestoreEntity): ) self.action_script.update_logger(self._logger) - state = await self.async_get_last_state() - if state: + if state := await self.async_get_last_state(): enable_automation = state.state == STATE_ON last_triggered = state.attributes.get("last_triggered") if last_triggered is not None: diff --git a/homeassistant/components/camera/prefs.py b/homeassistant/components/camera/prefs.py index 36f3d60d0db..53a149ff7d8 100644 --- a/homeassistant/components/camera/prefs.py +++ b/homeassistant/components/camera/prefs.py @@ -40,9 +40,7 @@ class CameraPreferences: async def async_initialize(self) -> None: """Finish initializing the preferences.""" - prefs = await self._store.async_load() - - if prefs is None: + if (prefs := await self._store.async_load()) is None: prefs = {} self._prefs = prefs diff --git a/homeassistant/components/cloud/prefs.py b/homeassistant/components/cloud/prefs.py index a4c81bcc64f..816ebe26d24 100644 --- a/homeassistant/components/cloud/prefs.py +++ b/homeassistant/components/cloud/prefs.py @@ -54,9 +54,7 @@ class CloudPreferences: async def async_initialize(self): """Finish initializing the preferences.""" - prefs = await self._store.async_load() - - if prefs is None: + if (prefs := await self._store.async_load()) is None: prefs = self._empty_config("") self._prefs = prefs diff --git a/homeassistant/components/derivative/sensor.py b/homeassistant/components/derivative/sensor.py index 45f5db57a90..f6de217ff2b 100644 --- a/homeassistant/components/derivative/sensor.py +++ b/homeassistant/components/derivative/sensor.py @@ -122,8 +122,7 @@ class DerivativeSensor(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 is not None: + if (state := await self.async_get_last_state()) is not None: try: self._state = Decimal(state.state) except SyntaxError as err: diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index 94638c031a3..d81743c530a 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -788,8 +788,7 @@ class Device(RestoreEntity): async def async_added_to_hass(self) -> None: """Add an entity.""" await super().async_added_to_hass() - state = await self.async_get_last_state() - if not state: + if not (state := await self.async_get_last_state()): return self._state = state.state self.last_update_home = state.state == STATE_HOME diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index 4d52240535f..2c27d371c5e 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -240,8 +240,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup) # Check If we have an old state - 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 we have no initial temperature, restore if self._target_temp is None: # If we have a previously saved temperature diff --git a/homeassistant/components/geofency/device_tracker.py b/homeassistant/components/geofency/device_tracker.py index b2d26dcb2a5..2904d1a1b1d 100644 --- a/homeassistant/components/geofency/device_tracker.py +++ b/homeassistant/components/geofency/device_tracker.py @@ -106,9 +106,7 @@ class GeofencyEntity(TrackerEntity, RestoreEntity): if self._attributes: return - state = await self.async_get_last_state() - - if state is None: + if (state := await self.async_get_last_state()) is None: self._gps = (None, None) return diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 14667dbb303..76933cecd91 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -346,8 +346,7 @@ class GoogleConfigStore: async def async_load(self): """Store current configuration to disk.""" - data = await self._store.async_load() - if data: + if data := await self._store.async_load(): self._data = data diff --git a/homeassistant/components/gpslogger/device_tracker.py b/homeassistant/components/gpslogger/device_tracker.py index 8b0965cc434..18b5b7fa585 100644 --- a/homeassistant/components/gpslogger/device_tracker.py +++ b/homeassistant/components/gpslogger/device_tracker.py @@ -132,8 +132,7 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity): if self._location 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._location = (None, None) self._accuracy = None self._attributes = { diff --git a/homeassistant/components/group/__init__.py b/homeassistant/components/group/__init__.py index 523b45a94f7..e3816d52d60 100644 --- a/homeassistant/components/group/__init__.py +++ b/homeassistant/components/group/__init__.py @@ -224,8 +224,7 @@ async def async_setup(hass, config): """Remove all user-defined groups and load new ones from config.""" auto = list(filter(lambda e: not e.user_defined, component.entities)) - conf = await component.async_prepare_reload() - if conf is None: + if (conf := await component.async_prepare_reload()) is None: return await _async_process_config(hass, conf, component) diff --git a/homeassistant/components/homekit_controller/storage.py b/homeassistant/components/homekit_controller/storage.py index ffc5bdc2381..4d512fbbc5d 100644 --- a/homeassistant/components/homekit_controller/storage.py +++ b/homeassistant/components/homekit_controller/storage.py @@ -34,8 +34,7 @@ class EntityMapStorage: async def async_initialize(self): """Get the pairing cache data.""" - raw_storage = await self.store.async_load() - if not raw_storage: + if not (raw_storage := await self.store.async_load()): # There is no cached data about HomeKit devices yet return diff --git a/homeassistant/components/mobile_app/__init__.py b/homeassistant/components/mobile_app/__init__.py index 47e21d53515..a83931bab23 100644 --- a/homeassistant/components/mobile_app/__init__.py +++ b/homeassistant/components/mobile_app/__init__.py @@ -37,8 +37,7 @@ PLATFORMS = "sensor", "binary_sensor", "device_tracker" async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the mobile app component.""" store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY) - app_config = await store.async_load() - if app_config is None: + if (app_config := await store.async_load()) is None: app_config = { DATA_CONFIG_ENTRIES: {}, DATA_DELETED_IDS: [], diff --git a/homeassistant/components/mobile_app/device_tracker.py b/homeassistant/components/mobile_app/device_tracker.py index f4b5866eacd..28f5e13c4fd 100644 --- a/homeassistant/components/mobile_app/device_tracker.py +++ b/homeassistant/components/mobile_app/device_tracker.py @@ -119,9 +119,7 @@ class MobileAppEntity(TrackerEntity, RestoreEntity): if self._data 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._data = {} return diff --git a/homeassistant/components/mobile_app/entity.py b/homeassistant/components/mobile_app/entity.py index 0cdec984f55..03b6d95c2a2 100644 --- a/homeassistant/components/mobile_app/entity.py +++ b/homeassistant/components/mobile_app/entity.py @@ -44,9 +44,8 @@ class MobileAppEntity(RestoreEntity): self.hass, SIGNAL_SENSOR_UPDATE, self._handle_update ) ) - state = await self.async_get_last_state() - if state is None: + if (state := await self.async_get_last_state()) is None: return self.async_restore_last_state(state) diff --git a/homeassistant/components/onboarding/__init__.py b/homeassistant/components/onboarding/__init__.py index e383e4e32c4..c2ab9a495bf 100644 --- a/homeassistant/components/onboarding/__init__.py +++ b/homeassistant/components/onboarding/__init__.py @@ -50,9 +50,7 @@ def async_is_user_onboarded(hass): async def async_setup(hass, config): """Set up the onboarding component.""" store = OnboadingStorage(hass, STORAGE_VERSION, STORAGE_KEY, private=True) - data = await store.async_load() - - if data is None: + if (data := await store.async_load()) is None: data = {"done": []} if STEP_USER not in data["done"]: diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index 9cff8377c35..e0ce94cb723 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -580,9 +580,7 @@ class SwitchableRflinkDevice(RflinkCommand, RestoreEntity): async def async_added_to_hass(self): """Restore RFLink device state (ON/OFF).""" await super().async_added_to_hass() - - 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: self._state = old_state.state == STATE_ON def _handle_event(self, event): diff --git a/homeassistant/components/rflink/cover.py b/homeassistant/components/rflink/cover.py index 2e6837d21ea..b98fb0fe4a9 100644 --- a/homeassistant/components/rflink/cover.py +++ b/homeassistant/components/rflink/cover.py @@ -117,9 +117,7 @@ class RflinkCover(RflinkCommand, CoverEntity, RestoreEntity): async def async_added_to_hass(self): """Restore RFLink cover state (OPEN/CLOSE).""" await super().async_added_to_hass() - - 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: self._state = old_state.state == STATE_OPEN def _handle_event(self, event): diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index 14160268073..a8ffb271336 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -172,8 +172,7 @@ async def async_setup(hass, config): async def reload_service(service): """Call a service to reload scripts.""" - conf = await component.async_prepare_reload() - if conf is None: + if (conf := await component.async_prepare_reload()) is None: return await _async_process_config(hass, conf, component) diff --git a/homeassistant/components/unifi/__init__.py b/homeassistant/components/unifi/__init__.py index b935a7d01da..eb026643515 100644 --- a/homeassistant/components/unifi/__init__.py +++ b/homeassistant/components/unifi/__init__.py @@ -106,9 +106,7 @@ class UnifiWirelessClients: async def async_load(self): """Load data from file.""" - data = await self._store.async_load() - - if data is not None: + if (data := await self._store.async_load()) is not None: self.data = data @callback