From 5048bad050dff29b1d8a05a4dcc86bceb1d79ad5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 17 Oct 2021 19:56:00 +0200 Subject: [PATCH] Use assignment expressions 05 (#57785) --- homeassistant/components/alexa/capabilities.py | 6 ++---- homeassistant/components/alexa/handlers.py | 3 +-- .../components/amcrest/binary_sensor.py | 13 +++++-------- homeassistant/components/amcrest/sensor.py | 16 ++++++---------- homeassistant/components/aqualogic/sensor.py | 3 +-- homeassistant/components/aqualogic/switch.py | 9 +++------ .../components/arcam_fmj/media_player.py | 4 +--- homeassistant/components/arlo/camera.py | 3 +-- homeassistant/components/blebox/climate.py | 3 +-- homeassistant/components/blebox/light.py | 3 +-- homeassistant/components/calendar/__init__.py | 6 ++---- homeassistant/components/cloud/__init__.py | 4 +--- homeassistant/components/cloud/alexa_config.py | 4 +--- homeassistant/components/cloud/utils.py | 4 +--- .../config/auth_provider_homeassistant.py | 3 +-- homeassistant/components/coolmaster/climate.py | 3 +-- homeassistant/components/cover/__init__.py | 14 +++++--------- homeassistant/components/currencylayer/sensor.py | 3 +-- homeassistant/components/daikin/switch.py | 3 +-- 19 files changed, 36 insertions(+), 71 deletions(-) diff --git a/homeassistant/components/alexa/capabilities.py b/homeassistant/components/alexa/capabilities.py index 46f421963ca..dad6e00ff8e 100644 --- a/homeassistant/components/alexa/capabilities.py +++ b/homeassistant/components/alexa/capabilities.py @@ -182,8 +182,7 @@ class AlexaCapability: """Serialize according to the Discovery API.""" result = {"type": "AlexaInterface", "interface": self.name(), "version": "3"} - instance = self.instance - if instance is not None: + if (instance := self.instance) is not None: result["instance"] = instance properties_supported = self.properties_supported() @@ -264,8 +263,7 @@ class AlexaCapability: "timeOfSample": dt_util.utcnow().strftime(DATE_FORMAT), "uncertaintyInMilliseconds": 0, } - instance = self.instance - if instance is not None: + if (instance := self.instance) is not None: result["instance"] = instance yield result diff --git a/homeassistant/components/alexa/handlers.py b/homeassistant/components/alexa/handlers.py index 5a23f5d1bc2..21ad6648a5a 100644 --- a/homeassistant/components/alexa/handlers.py +++ b/homeassistant/components/alexa/handlers.py @@ -117,8 +117,7 @@ async def async_api_accept_grant(hass, config, directive, context): async def async_api_turn_on(hass, config, directive, context): """Process a turn on request.""" entity = directive.entity - domain = entity.domain - if domain == group.DOMAIN: + if (domain := entity.domain) == group.DOMAIN: domain = ha.DOMAIN service = SERVICE_TURN_ON diff --git a/homeassistant/components/amcrest/binary_sensor.py b/homeassistant/components/amcrest/binary_sensor.py index 8d2535a142b..4fc810fd773 100644 --- a/homeassistant/components/amcrest/binary_sensor.py +++ b/homeassistant/components/amcrest/binary_sensor.py @@ -215,8 +215,7 @@ class AmcrestBinarySensor(BinarySensorEntity): log_update_error(_LOGGER, "update", self.name, "binary sensor", error) return - event_code = self.entity_description.event_code - if event_code is None: + if (event_code := self.entity_description.event_code) is None: _LOGGER.error("Binary sensor %s event code not set", self.name) return @@ -228,12 +227,10 @@ class AmcrestBinarySensor(BinarySensorEntity): def _update_unique_id(self) -> None: """Set the unique id.""" - if self._attr_unique_id is None: - serial_number = self._api.serial_number - if serial_number: - self._attr_unique_id = ( - f"{serial_number}-{self.entity_description.key}-{self._channel}" - ) + if self._attr_unique_id is None and (serial_number := self._api.serial_number): + self._attr_unique_id = ( + f"{serial_number}-{self.entity_description.key}-{self._channel}" + ) async def async_on_demand_update(self) -> None: """Update state.""" diff --git a/homeassistant/components/amcrest/sensor.py b/homeassistant/components/amcrest/sensor.py index f2048654da6..c262e16ec7c 100644 --- a/homeassistant/components/amcrest/sensor.py +++ b/homeassistant/components/amcrest/sensor.py @@ -96,18 +96,14 @@ class AmcrestSensor(SensorEntity): _LOGGER.debug("Updating %s sensor", self.name) sensor_type = self.entity_description.key - if self._attr_unique_id is None: - serial_number = self._api.serial_number - if serial_number: - self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}" + if self._attr_unique_id is None and (serial_number := self._api.serial_number): + self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}" try: - if self._attr_unique_id is None: - serial_number = self._api.serial_number - if serial_number: - self._attr_unique_id = ( - f"{serial_number}-{sensor_type}-{self._channel}" - ) + if self._attr_unique_id is None and ( + serial_number := self._api.serial_number + ): + self._attr_unique_id = f"{serial_number}-{sensor_type}-{self._channel}" if sensor_type == SENSOR_PTZ_PRESET: self._attr_native_value = self._api.ptz_presets_count diff --git a/homeassistant/components/aqualogic/sensor.py b/homeassistant/components/aqualogic/sensor.py index 4c46a7aa5eb..c1823ca2bb5 100644 --- a/homeassistant/components/aqualogic/sensor.py +++ b/homeassistant/components/aqualogic/sensor.py @@ -147,8 +147,7 @@ class AquaLogicSensor(SensorEntity): @callback def async_update_callback(self): """Update callback.""" - panel = self._processor.panel - if panel is not None: + if (panel := self._processor.panel) is not None: if panel.is_metric: self._attr_native_unit_of_measurement = ( self.entity_description.unit_metric diff --git a/homeassistant/components/aqualogic/switch.py b/homeassistant/components/aqualogic/switch.py index c05bacc5f03..157688c7576 100644 --- a/homeassistant/components/aqualogic/switch.py +++ b/homeassistant/components/aqualogic/switch.py @@ -66,23 +66,20 @@ class AquaLogicSwitch(SwitchEntity): @property def is_on(self): """Return true if device is on.""" - panel = self._processor.panel - if panel is None: + if (panel := self._processor.panel) is None: return False state = panel.get_state(self._state_name) return state def turn_on(self, **kwargs): """Turn the device on.""" - panel = self._processor.panel - if panel is None: + if (panel := self._processor.panel) is None: return panel.set_state(self._state_name, True) def turn_off(self, **kwargs): """Turn the device off.""" - panel = self._processor.panel - if panel is None: + if (panel := self._processor.panel) is None: return panel.set_state(self._state_name, False) diff --git a/homeassistant/components/arcam_fmj/media_player.py b/homeassistant/components/arcam_fmj/media_player.py index e9e5e29a1c7..115675dfb89 100644 --- a/homeassistant/components/arcam_fmj/media_player.py +++ b/homeassistant/components/arcam_fmj/media_player.py @@ -375,9 +375,7 @@ class ArcamFmj(MediaPlayerEntity): if source is None: return None - channel = self.media_channel - - if channel: + if channel := self.media_channel: value = f"{source.name} - {channel}" else: value = source.name diff --git a/homeassistant/components/arlo/camera.py b/homeassistant/components/arlo/camera.py index 6b14f0cee0c..146970a8610 100644 --- a/homeassistant/components/arlo/camera.py +++ b/homeassistant/components/arlo/camera.py @@ -145,13 +145,12 @@ class ArloCam(Camera): def set_base_station_mode(self, mode): """Set the mode in the base station.""" # Get the list of base stations identified by library - base_stations = self.hass.data[DATA_ARLO].base_stations # Some Arlo cameras does not have base station # So check if there is base station detected first # if yes, then choose the primary base station # Set the mode on the chosen base station - if base_stations: + if base_stations := self.hass.data[DATA_ARLO].base_stations: primary_base_station = base_stations[0] primary_base_station.mode = mode diff --git a/homeassistant/components/blebox/climate.py b/homeassistant/components/blebox/climate.py index 59e64b772ef..5dec5725607 100644 --- a/homeassistant/components/blebox/climate.py +++ b/homeassistant/components/blebox/climate.py @@ -40,8 +40,7 @@ class BleBoxClimateEntity(BleBoxEntity, ClimateEntity): @property def hvac_action(self): """Return the actual current HVAC action.""" - is_on = self._feature.is_on - if not is_on: + if not (is_on := self._feature.is_on): return None if is_on is None else CURRENT_HVAC_OFF # NOTE: In practice, there's no need to handle case when is_heating is None diff --git a/homeassistant/components/blebox/light.py b/homeassistant/components/blebox/light.py index b03cc16112c..efbdb038794 100644 --- a/homeassistant/components/blebox/light.py +++ b/homeassistant/components/blebox/light.py @@ -56,8 +56,7 @@ class BleBoxLightEntity(BleBoxEntity, LightEntity): @property def rgbw_color(self): """Return the hue and saturation.""" - rgbw_hex = self._feature.rgbw_hex - if rgbw_hex is None: + if (rgbw_hex := self._feature.rgbw_hex) is None: return None return tuple(rgb_hex_to_rgb_list(rgbw_hex)[0:4]) diff --git a/homeassistant/components/calendar/__init__.py b/homeassistant/components/calendar/__init__.py index 10580339329..1666a30a1f5 100644 --- a/homeassistant/components/calendar/__init__.py +++ b/homeassistant/components/calendar/__init__.py @@ -145,8 +145,7 @@ class CalendarEventDevice(Entity): @property def state_attributes(self): """Return the entity state attributes.""" - event = self.event - if event is None: + if (event := self.event) is None: return None event = normalize_event(event) @@ -162,8 +161,7 @@ class CalendarEventDevice(Entity): @property def state(self): """Return the state of the calendar event.""" - event = self.event - if event is None: + if (event := self.event) is None: return STATE_OFF event = normalize_event(event) diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index f1833899fec..f99ac4c7b0a 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -253,9 +253,7 @@ def _remote_handle_prefs_updated(cloud: Cloud) -> None: if prefs.remote_enabled == cur_pref: return - cur_pref = prefs.remote_enabled - - if cur_pref: + if cur_pref := prefs.remote_enabled: await cloud.remote.connect() else: await cloud.remote.disconnect() diff --git a/homeassistant/components/cloud/alexa_config.py b/homeassistant/components/cloud/alexa_config.py index c14b4d7a4e7..f578a114dfc 100644 --- a/homeassistant/components/cloud/alexa_config.py +++ b/homeassistant/components/cloud/alexa_config.py @@ -143,10 +143,8 @@ class AlexaConfig(alexa_config.AbstractConfig): else: auxiliary_entity = False - default_expose = self._prefs.alexa_default_expose - # Backwards compat - if default_expose is None: + if (default_expose := self._prefs.alexa_default_expose) is None: return not auxiliary_entity return not auxiliary_entity and split_entity_id(entity_id)[0] in default_expose diff --git a/homeassistant/components/cloud/utils.py b/homeassistant/components/cloud/utils.py index 57f84b057f7..715a8119af9 100644 --- a/homeassistant/components/cloud/utils.py +++ b/homeassistant/components/cloud/utils.py @@ -8,9 +8,7 @@ from aiohttp import payload, web def aiohttp_serialize_response(response: web.Response) -> dict[str, Any]: """Serialize an aiohttp response to a dictionary.""" - body = response.body - - if body is None: + if (body := response.body) is None: pass elif isinstance(body, payload.StringPayload): # pylint: disable=protected-access diff --git a/homeassistant/components/config/auth_provider_homeassistant.py b/homeassistant/components/config/auth_provider_homeassistant.py index a8421c4c0f6..78175678a58 100644 --- a/homeassistant/components/config/auth_provider_homeassistant.py +++ b/homeassistant/components/config/auth_provider_homeassistant.py @@ -103,8 +103,7 @@ async def websocket_delete(hass, connection, msg): @websocket_api.async_response async def websocket_change_password(hass, connection, msg): """Change current user password.""" - user = connection.user - if user is None: + if (user := connection.user) is None: connection.send_error(msg["id"], "user_not_found", "User not found") return diff --git a/homeassistant/components/coolmaster/climate.py b/homeassistant/components/coolmaster/climate.py index 7077854a768..0b717697a1a 100644 --- a/homeassistant/components/coolmaster/climate.py +++ b/homeassistant/components/coolmaster/climate.py @@ -120,8 +120,7 @@ class CoolmasterClimate(CoordinatorEntity, ClimateEntity): def hvac_mode(self): """Return hvac target hvac state.""" mode = self._unit.mode - is_on = self._unit.is_on - if not is_on: + if not self._unit.is_on: return HVAC_MODE_OFF return CM_TO_HA_STATE[mode] diff --git a/homeassistant/components/cover/__init__.py b/homeassistant/components/cover/__init__.py index 00fef5c6485..7a3061d24c8 100644 --- a/homeassistant/components/cover/__init__.py +++ b/homeassistant/components/cover/__init__.py @@ -212,9 +212,7 @@ class CoverEntity(Entity): if self.is_closing: return STATE_CLOSING - closed = self.is_closed - - if closed is None: + if (closed := self.is_closed) is None: return None return STATE_CLOSED if closed else STATE_OPEN @@ -225,13 +223,11 @@ class CoverEntity(Entity): """Return the state attributes.""" data = {} - current = self.current_cover_position - if current is not None: - data[ATTR_CURRENT_POSITION] = self.current_cover_position + if (current := self.current_cover_position) is not None: + data[ATTR_CURRENT_POSITION] = current - current_tilt = self.current_cover_tilt_position - if current_tilt is not None: - data[ATTR_CURRENT_TILT_POSITION] = self.current_cover_tilt_position + if (current_tilt := self.current_cover_tilt_position) is not None: + data[ATTR_CURRENT_TILT_POSITION] = current_tilt return data diff --git a/homeassistant/components/currencylayer/sensor.py b/homeassistant/components/currencylayer/sensor.py index fd3f3b2f8c5..2f0461cc8c4 100644 --- a/homeassistant/components/currencylayer/sensor.py +++ b/homeassistant/components/currencylayer/sensor.py @@ -92,8 +92,7 @@ class CurrencylayerSensor(SensorEntity): def update(self): """Update current date.""" self.rest.update() - value = self.rest.data - if value is not None: + if (value := self.rest.data) is not None: self._state = round(value[f"{self._base}{self._quote}"], 4) diff --git a/homeassistant/components/daikin/switch.py b/homeassistant/components/daikin/switch.py index 5e0e1b5761a..647ee0689e6 100644 --- a/homeassistant/components/daikin/switch.py +++ b/homeassistant/components/daikin/switch.py @@ -22,8 +22,7 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up Daikin climate based on config_entry.""" daikin_api = hass.data[DAIKIN_DOMAIN][entry.entry_id] switches = [] - zones = daikin_api.device.zones - if zones: + if zones := daikin_api.device.zones: switches.extend( [ DaikinZoneSwitch(daikin_api, zone_id)