From 2a8eaf0e0fc07eb617cdcdefbe7e4b94574b8391 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 17 Oct 2021 20:05:11 +0200 Subject: [PATCH] Use assignment expressions 06 (#57786) --- .../components/entur_public_transport/sensor.py | 3 +-- .../components/environment_canada/weather.py | 3 +-- homeassistant/components/esphome/light.py | 3 +-- homeassistant/components/fan/__init__.py | 9 +++------ homeassistant/components/fastdotcom/sensor.py | 3 +-- homeassistant/components/fibaro/__init__.py | 14 ++++++-------- homeassistant/components/fido/sensor.py | 3 +-- .../components/fireservicerota/sensor.py | 3 +-- .../components/fortios/device_tracker.py | 4 +--- homeassistant/components/frontend/__init__.py | 3 +-- homeassistant/components/gdacs/geo_location.py | 3 +-- homeassistant/components/glances/sensor.py | 3 +-- .../components/google_assistant/helpers.py | 4 +--- .../components/google_assistant/trait.py | 15 ++++----------- homeassistant/components/harmony/remote.py | 6 ++---- .../components/here_travel_time/sensor.py | 6 ++---- homeassistant/components/homeassistant/scene.py | 3 +-- .../components/homekit/type_thermostats.py | 3 +-- .../components/homekit_controller/air_quality.py | 3 +-- .../components/homematicip_cloud/binary_sensor.py | 4 ++-- .../homematicip_cloud/generic_entity.py | 6 ++---- 21 files changed, 35 insertions(+), 69 deletions(-) diff --git a/homeassistant/components/entur_public_transport/sensor.py b/homeassistant/components/entur_public_transport/sensor.py index 3256b26171b..776d1c17618 100644 --- a/homeassistant/components/entur_public_transport/sensor.py +++ b/homeassistant/components/entur_public_transport/sensor.py @@ -206,8 +206,7 @@ class EnturPublicTransportSensor(SensorEntity): self._attributes[CONF_LATITUDE] = data.latitude self._attributes[CONF_LONGITUDE] = data.longitude - calls = data.estimated_calls - if not calls: + if not (calls := data.estimated_calls): self._state = None return diff --git a/homeassistant/components/environment_canada/weather.py b/homeassistant/components/environment_canada/weather.py index 281cf117426..e54e9f8767d 100644 --- a/homeassistant/components/environment_canada/weather.py +++ b/homeassistant/components/environment_canada/weather.py @@ -202,8 +202,7 @@ def get_forecast(ec_data, forecast_type): forecast_array = [] if forecast_type == "daily": - half_days = ec_data.daily_forecasts - if not half_days: + if not (half_days := ec_data.daily_forecasts): return None today = { diff --git a/homeassistant/components/esphome/light.py b/homeassistant/components/esphome/light.py index b8fe4bd74c7..eb5e258f079 100644 --- a/homeassistant/components/esphome/light.py +++ b/homeassistant/components/esphome/light.py @@ -281,8 +281,7 @@ class EsphomeLight(EsphomeEntity[LightInfo, LightState], LightEntity): def color_mode(self) -> str | None: """Return the color mode of the light.""" if not self._supports_color_mode: - supported = self.supported_color_modes - if not supported: + if not (supported := self.supported_color_modes): return None return next(iter(supported)) diff --git a/homeassistant/components/fan/__init__.py b/homeassistant/components/fan/__init__.py index a05505e8112..3a5b9bcda67 100644 --- a/homeassistant/components/fan/__init__.py +++ b/homeassistant/components/fan/__init__.py @@ -458,13 +458,10 @@ class FanEntity(ToggleEntity): @property def speed(self) -> str | None: """Return the current speed.""" - if self._implemented_preset_mode: - preset_mode = self.preset_mode - if preset_mode: - return preset_mode + if self._implemented_preset_mode and (preset_mode := self.preset_mode): + return preset_mode if self._implemented_percentage: - percentage = self.percentage - if percentage is None: + if (percentage := self.percentage) is None: return None return self.percentage_to_speed(percentage) return None diff --git a/homeassistant/components/fastdotcom/sensor.py b/homeassistant/components/fastdotcom/sensor.py index fa1f18815f1..2a82cee7cea 100644 --- a/homeassistant/components/fastdotcom/sensor.py +++ b/homeassistant/components/fastdotcom/sensor.py @@ -56,8 +56,7 @@ class SpeedtestSensor(RestoreEntity, SensorEntity): def update(self) -> None: """Get the latest data and update the states.""" - data = self._speedtest_data.data # type: ignore[attr-defined] - if data is None: + if (data := self._speedtest_data.data) is None: # type: ignore[attr-defined] return self._attr_native_value = data["download"] diff --git a/homeassistant/components/fibaro/__init__.py b/homeassistant/components/fibaro/__init__.py index 964c1112840..cff4e153d98 100644 --- a/homeassistant/components/fibaro/__init__.py +++ b/homeassistant/components/fibaro/__init__.py @@ -307,8 +307,7 @@ class FibaroController: device.device_config = self._device_config.get(device.ha_id, {}) else: device.mapped_type = None - dtype = device.mapped_type - if dtype is None: + if (dtype := device.mapped_type) is None: continue device.unique_id_str = f"{self.hub_serial}.{device.id}" self._device_map[device.id] = device @@ -472,12 +471,11 @@ class FibaroDevice(Entity): @property def current_power_w(self): """Return the current power usage in W.""" - if "power" in self.fibaro_device.properties: - power = self.fibaro_device.properties.power - if power: - return convert(power, float, 0.0) - else: - return None + if "power" in self.fibaro_device.properties and ( + power := self.fibaro_device.properties.power + ): + return convert(power, float, 0.0) + return None @property def current_binary_state(self): diff --git a/homeassistant/components/fido/sensor.py b/homeassistant/components/fido/sensor.py index 6964abd9b3d..0a06c7a2b07 100644 --- a/homeassistant/components/fido/sensor.py +++ b/homeassistant/components/fido/sensor.py @@ -218,8 +218,7 @@ class FidoSensor(SensorEntity): async def async_update(self): """Get the latest data from Fido and update the state.""" await self.fido_data.async_update() - sensor_type = self.entity_description.key - if sensor_type == "balance": + if (sensor_type := self.entity_description.key) == "balance": if self.fido_data.data.get(sensor_type) is not None: self._attr_native_value = round(self.fido_data.data[sensor_type], 2) else: diff --git a/homeassistant/components/fireservicerota/sensor.py b/homeassistant/components/fireservicerota/sensor.py index ec446621212..a87b1609ec9 100644 --- a/homeassistant/components/fireservicerota/sensor.py +++ b/homeassistant/components/fireservicerota/sensor.py @@ -67,9 +67,8 @@ class IncidentsSensor(RestoreEntity, SensorEntity): def extra_state_attributes(self) -> object: """Return available attributes for sensor.""" attr = {} - data = self._state_attributes - if not data: + if not (data := self._state_attributes): return attr for value in ( diff --git a/homeassistant/components/fortios/device_tracker.py b/homeassistant/components/fortios/device_tracker.py index 1b9134bee44..a1507af99dc 100644 --- a/homeassistant/components/fortios/device_tracker.py +++ b/homeassistant/components/fortios/device_tracker.py @@ -102,9 +102,7 @@ class FortiOSDeviceScanner(DeviceScanner): device = device.lower() - data = self._clients_json - - if data == 0: + if (data := self._clients_json) == 0: _LOGGER.error("No json results to get device names") return None diff --git a/homeassistant/components/frontend/__init__.py b/homeassistant/components/frontend/__init__.py index 8b92745f4d4..005384b6beb 100644 --- a/homeassistant/components/frontend/__init__.py +++ b/homeassistant/components/frontend/__init__.py @@ -567,8 +567,7 @@ class IndexView(web_urldispatcher.AbstractResource): def get_template(self) -> jinja2.Template: """Get template.""" - tpl = self._template_cache - if tpl is None: + if (tpl := self._template_cache) is None: with (_frontend_root(self.repo_path) / "index.html").open( encoding="utf8" ) as file: diff --git a/homeassistant/components/gdacs/geo_location.py b/homeassistant/components/gdacs/geo_location.py index 7e3dc7484bb..fb0782de525 100644 --- a/homeassistant/components/gdacs/geo_location.py +++ b/homeassistant/components/gdacs/geo_location.py @@ -138,8 +138,7 @@ class GdacsEvent(GeolocationEvent): def _update_from_feed(self, feed_entry): """Update the internal state from the provided feed entry.""" - event_name = feed_entry.event_name - if not event_name: + if not (event_name := feed_entry.event_name): # Earthquakes usually don't have an event name. event_name = f"{feed_entry.country} ({feed_entry.event_id})" self._title = f"{feed_entry.event_type}: {event_name}" diff --git a/homeassistant/components/glances/sensor.py b/homeassistant/components/glances/sensor.py index 92173f9d143..b0960b3531a 100644 --- a/homeassistant/components/glances/sensor.py +++ b/homeassistant/components/glances/sensor.py @@ -113,8 +113,7 @@ class GlancesSensor(SensorEntity): async def async_update(self): # noqa: C901 """Get the latest data from REST API.""" - value = self.glances_data.api.data - if value is None: + if (value := self.glances_data.api.data) is None: return if self.entity_description.type == "fs": diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 534fdcfac1e..67f4e0c60df 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -262,9 +262,7 @@ class AbstractConfig(ABC): @callback def async_enable_local_sdk(self): """Enable the local SDK.""" - webhook_id = self.local_sdk_webhook_id - - if webhook_id is None: + if (webhook_id := self.local_sdk_webhook_id) is None: return try: diff --git a/homeassistant/components/google_assistant/trait.py b/homeassistant/components/google_assistant/trait.py index 0301769aea7..9f79f0f7d9b 100644 --- a/homeassistant/components/google_assistant/trait.py +++ b/homeassistant/components/google_assistant/trait.py @@ -255,9 +255,7 @@ class BrightnessTrait(_Trait): async def execute(self, command, data, params, challenge): """Execute a brightness command.""" - domain = self.state.domain - - if domain == light.DOMAIN: + if self.state.domain == light.DOMAIN: await self.hass.services.async_call( light.DOMAIN, light.SERVICE_TURN_ON, @@ -348,9 +346,7 @@ class OnOffTrait(_Trait): async def execute(self, command, data, params, challenge): """Execute an OnOff command.""" - domain = self.state.domain - - if domain == group.DOMAIN: + if (domain := self.state.domain) == group.DOMAIN: service_domain = HA_DOMAIN service = SERVICE_TURN_ON if params["on"] else SERVICE_TURN_OFF @@ -1156,9 +1152,7 @@ class HumiditySettingTrait(_Trait): async def execute(self, command, data, params, challenge): """Execute a humidity command.""" - domain = self.state.domain - - if domain == sensor.DOMAIN: + if self.state.domain == sensor.DOMAIN: raise SmartHomeError( ERR_NOT_SUPPORTED, "Execute is not supported by sensor" ) @@ -1449,8 +1443,7 @@ class FanSpeedTrait(_Trait): async def execute_reverse(self, data, params): """Execute a Reverse command.""" - domain = self.state.domain - if domain == fan.DOMAIN: + if self.state.domain == fan.DOMAIN: if self.state.attributes.get(fan.ATTR_DIRECTION) == fan.DIRECTION_FORWARD: direction = fan.DIRECTION_REVERSE else: diff --git a/homeassistant/components/harmony/remote.py b/homeassistant/components/harmony/remote.py index 806b638aee8..6a13093ec25 100644 --- a/homeassistant/components/harmony/remote.py +++ b/homeassistant/components/harmony/remote.py @@ -212,8 +212,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity): if self._last_activity: activity = self._last_activity else: - all_activities = self._data.activity_names - if all_activities: + if all_activities := self._data.activity_names: activity = all_activities[0] if activity: @@ -257,8 +256,7 @@ class HarmonyRemote(HarmonyEntity, remote.RemoteEntity, RestoreEntity): _LOGGER.debug( "%s: Writing hub configuration to file: %s", self.name, self._config_path ) - json_config = self._data.json_config - if json_config is None: + if (json_config := self._data.json_config) is None: _LOGGER.warning("%s: No configuration received from hub", self.name) return diff --git a/homeassistant/components/here_travel_time/sensor.py b/homeassistant/components/here_travel_time/sensor.py index 7606a2772d6..a47e28179b3 100644 --- a/homeassistant/components/here_travel_time/sensor.py +++ b/homeassistant/components/here_travel_time/sensor.py @@ -416,11 +416,9 @@ class HERETravelTimeData: # Convert location to HERE friendly location destination = self.destination.split(",") origin = self.origin.split(",") - arrival = self.arrival - if arrival is not None: + if (arrival := self.arrival) is not None: arrival = convert_time_to_isodate(arrival) - departure = self.departure - if departure is not None: + if (departure := self.departure) is not None: departure = convert_time_to_isodate(departure) if departure is None and arrival is None: diff --git a/homeassistant/components/homeassistant/scene.py b/homeassistant/components/homeassistant/scene.py index 9ae271baa72..e5b37dd3f01 100644 --- a/homeassistant/components/homeassistant/scene.py +++ b/homeassistant/components/homeassistant/scene.py @@ -311,8 +311,7 @@ class HomeAssistantScene(Scene): def extra_state_attributes(self): """Return the scene state attributes.""" attributes = {ATTR_ENTITY_ID: list(self.scene_config.states)} - unique_id = self.unique_id - if unique_id is not None: + if (unique_id := self.unique_id) is not None: attributes[CONF_ID] = unique_id return attributes diff --git a/homeassistant/components/homekit/type_thermostats.py b/homeassistant/components/homekit/type_thermostats.py index 95c5f87b6c2..c75cc95b169 100644 --- a/homeassistant/components/homekit/type_thermostats.py +++ b/homeassistant/components/homekit/type_thermostats.py @@ -609,8 +609,7 @@ class WaterHeater(HomeAccessory): self.char_display_units.set_value(unit) # Update target operation mode - operation_mode = new_state.state - if operation_mode: + if new_state.state: self.char_target_heat_cool.set_value(1) # Heat diff --git a/homeassistant/components/homekit_controller/air_quality.py b/homeassistant/components/homekit_controller/air_quality.py index b4ca2f4918a..df5a89f179e 100644 --- a/homeassistant/components/homekit_controller/air_quality.py +++ b/homeassistant/components/homekit_controller/air_quality.py @@ -91,8 +91,7 @@ class HomeAirQualitySensor(HomeKitEntity, AirQualityEntity): """Return the device state attributes.""" data = {"air_quality_text": self.air_quality_text} - voc = self.volatile_organic_compounds - if voc: + if voc := self.volatile_organic_compounds: data["volatile_organic_compounds"] = voc return data diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index 80dfa8316d0..3cf8fc72c56 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -544,8 +544,8 @@ class HomematicipSecuritySensorGroup( @property def is_on(self) -> bool: """Return true if safety issue detected.""" - parent_is_on = super().is_on - if parent_is_on: + if super().is_on: + # parent is on return True if ( diff --git a/homeassistant/components/homematicip_cloud/generic_entity.py b/homeassistant/components/homematicip_cloud/generic_entity.py index f1edff1854b..976650aa46b 100644 --- a/homeassistant/components/homematicip_cloud/generic_entity.py +++ b/homeassistant/components/homematicip_cloud/generic_entity.py @@ -153,8 +153,7 @@ class HomematicipGenericEntity(Entity): if not self.registry_entry: return - device_id = self.registry_entry.device_id - if device_id: + if device_id := self.registry_entry.device_id: # Remove from device registry. device_registry = await dr.async_get_registry(self.hass) if device_id in device_registry.devices: @@ -163,8 +162,7 @@ class HomematicipGenericEntity(Entity): else: # Remove from entity registry. # Only relevant for entities that do not belong to a device. - entity_id = self.registry_entry.entity_id - if entity_id: + if entity_id := self.registry_entry.entity_id: entity_registry = await er.async_get_registry(self.hass) if entity_id in entity_registry.entities: entity_registry.async_remove(entity_id)