From e0c0d008334b09bd1c34659e4c37a88598156d88 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 31 Oct 2021 18:56:25 +0100 Subject: [PATCH] Use assignment expressions 39 (#58829) --- .../components/ambiclimate/config_flow.py | 4 +--- homeassistant/components/aruba/device_tracker.py | 3 +-- .../components/cisco_ios/device_tracker.py | 4 +--- homeassistant/components/dlna_dmr/config_flow.py | 3 +-- homeassistant/components/doorbird/__init__.py | 4 +--- homeassistant/components/dsmr/sensor.py | 3 +-- .../components/homematicip_cloud/services.py | 15 +++++---------- .../components/huawei_lte/device_tracker.py | 6 ++---- homeassistant/components/knx/expose.py | 3 +-- homeassistant/components/lametric/__init__.py | 3 +-- homeassistant/components/linode/binary_sensor.py | 3 +-- homeassistant/components/linode/switch.py | 3 +-- .../components/mobile_app/device_action.py | 4 +--- homeassistant/components/netatmo/sensor.py | 4 +--- homeassistant/components/repetier/sensor.py | 12 ++++-------- homeassistant/components/ripple/sensor.py | 4 +--- .../components/sky_hub/device_tracker.py | 4 +--- .../components/thomson/device_tracker.py | 3 +-- homeassistant/components/watson_iot/__init__.py | 3 +-- homeassistant/components/whirlpool/climate.py | 3 +-- homeassistant/components/xbox_live/sensor.py | 3 +-- homeassistant/components/zwave_js/sensor.py | 3 +-- homeassistant/util/location.py | 4 +--- 23 files changed, 31 insertions(+), 70 deletions(-) diff --git a/homeassistant/components/ambiclimate/config_flow.py b/homeassistant/components/ambiclimate/config_flow.py index 623e96a4a67..b022b54c4fd 100644 --- a/homeassistant/components/ambiclimate/config_flow.py +++ b/homeassistant/components/ambiclimate/config_flow.py @@ -86,9 +86,7 @@ class AmbiclimateFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Received code for authentication.""" self._async_abort_entries_match() - token_info = await self._get_token_info(code) - - if token_info is None: + if await self._get_token_info(code) is None: return self.async_abort(reason="access_token") config = self.hass.data[DATA_AMBICLIMATE_IMPL].copy() diff --git a/homeassistant/components/aruba/device_tracker.py b/homeassistant/components/aruba/device_tracker.py index e9799411197..721585fa391 100644 --- a/homeassistant/components/aruba/device_tracker.py +++ b/homeassistant/components/aruba/device_tracker.py @@ -74,8 +74,7 @@ class ArubaDeviceScanner(DeviceScanner): if not self.success_init: return False - data = self.get_aruba_data() - if not data: + if not (data := self.get_aruba_data()): return False self.last_results = data.values() diff --git a/homeassistant/components/cisco_ios/device_tracker.py b/homeassistant/components/cisco_ios/device_tracker.py index b30e9dae1f3..9861f657ff6 100644 --- a/homeassistant/components/cisco_ios/device_tracker.py +++ b/homeassistant/components/cisco_ios/device_tracker.py @@ -65,9 +65,7 @@ class CiscoDeviceScanner(DeviceScanner): Returns boolean if scanning successful. """ - string_result = self._get_arp_data() - - if string_result: + if string_result := self._get_arp_data(): self.last_results = [] last_results = [] diff --git a/homeassistant/components/dlna_dmr/config_flow.py b/homeassistant/components/dlna_dmr/config_flow.py index bfee37722b8..bfc5cb27129 100644 --- a/homeassistant/components/dlna_dmr/config_flow.py +++ b/homeassistant/components/dlna_dmr/config_flow.py @@ -89,8 +89,7 @@ class DlnaDmrFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): await self._async_set_info_from_discovery(discovery) return self._create_entry() - discoveries = await self._async_get_discoveries() - if not discoveries: + if not (discoveries := await self._async_get_discoveries()): # Nothing found, maybe the user knows an URL to try return await self.async_step_manual() diff --git a/homeassistant/components/doorbird/__init__.py b/homeassistant/components/doorbird/__init__.py index 3bd82a7ff8e..7497304f9e1 100644 --- a/homeassistant/components/doorbird/__init__.py +++ b/homeassistant/components/doorbird/__init__.py @@ -269,9 +269,7 @@ class ConfiguredDoorBird: if not self.webhook_is_registered(url): self.device.change_favorite("http", f"Home Assistant ({event})", url) - fav_id = self.get_webhook_id(url) - - if not fav_id: + if not self.get_webhook_id(url): _LOGGER.warning( 'Could not find favorite for URL "%s". ' 'Skipping sensor "%s"', url, diff --git a/homeassistant/components/dsmr/sensor.py b/homeassistant/components/dsmr/sensor.py index 12b2a17016a..c016a25ad55 100644 --- a/homeassistant/components/dsmr/sensor.py +++ b/homeassistant/components/dsmr/sensor.py @@ -260,8 +260,7 @@ class DSMREntity(SensorEntity): @property def native_value(self) -> StateType: """Return the state of sensor, if available, translate if needed.""" - value = self.get_dsmr_object_attr("value") - if value is None: + if (value := self.get_dsmr_object_attr("value")) is None: return None if self.entity_description.key == obis_ref.ELECTRICITY_ACTIVE_TARIFF: diff --git a/homeassistant/components/homematicip_cloud/services.py b/homeassistant/components/homematicip_cloud/services.py index 45b47b40efa..88c14c648d8 100644 --- a/homeassistant/components/homematicip_cloud/services.py +++ b/homeassistant/components/homematicip_cloud/services.py @@ -210,8 +210,7 @@ async def _async_activate_eco_mode_with_duration( duration = service.data[ATTR_DURATION] if hapid := service.data.get(ATTR_ACCESSPOINT_ID): - home = _get_home(hass, hapid) - if home: + if home := _get_home(hass, hapid): await home.activate_absence_with_duration(duration) else: for hap in hass.data[HMIPC_DOMAIN].values(): @@ -225,8 +224,7 @@ async def _async_activate_eco_mode_with_period( endtime = service.data[ATTR_ENDTIME] if hapid := service.data.get(ATTR_ACCESSPOINT_ID): - home = _get_home(hass, hapid) - if home: + if home := _get_home(hass, hapid): await home.activate_absence_with_period(endtime) else: for hap in hass.data[HMIPC_DOMAIN].values(): @@ -239,8 +237,7 @@ async def _async_activate_vacation(hass: HomeAssistant, service: ServiceCall) -> temperature = service.data[ATTR_TEMPERATURE] if hapid := service.data.get(ATTR_ACCESSPOINT_ID): - home = _get_home(hass, hapid) - if home: + if home := _get_home(hass, hapid): await home.activate_vacation(endtime, temperature) else: for hap in hass.data[HMIPC_DOMAIN].values(): @@ -250,8 +247,7 @@ async def _async_activate_vacation(hass: HomeAssistant, service: ServiceCall) -> async def _async_deactivate_eco_mode(hass: HomeAssistant, service: ServiceCall) -> None: """Service to deactivate eco mode.""" if hapid := service.data.get(ATTR_ACCESSPOINT_ID): - home = _get_home(hass, hapid) - if home: + if home := _get_home(hass, hapid): await home.deactivate_absence() else: for hap in hass.data[HMIPC_DOMAIN].values(): @@ -261,8 +257,7 @@ async def _async_deactivate_eco_mode(hass: HomeAssistant, service: ServiceCall) async def _async_deactivate_vacation(hass: HomeAssistant, service: ServiceCall) -> None: """Service to deactivate vacation.""" if hapid := service.data.get(ATTR_ACCESSPOINT_ID): - home = _get_home(hass, hapid) - if home: + if home := _get_home(hass, hapid): await home.deactivate_vacation() else: for hap in hass.data[HMIPC_DOMAIN].values(): diff --git a/homeassistant/components/huawei_lte/device_tracker.py b/homeassistant/components/huawei_lte/device_tracker.py index 5c451f71545..7c3f3d16c92 100644 --- a/homeassistant/components/huawei_lte/device_tracker.py +++ b/homeassistant/components/huawei_lte/device_tracker.py @@ -133,8 +133,7 @@ def async_add_new_entities( tracked: set[str], ) -> None: """Add new entities that are not already being tracked.""" - hosts = _get_hosts(router) - if not hosts: + if not (hosts := _get_hosts(router)): return track_wired_clients = router.config_entry.options.get( @@ -225,8 +224,7 @@ class HuaweiLteScannerEntity(HuaweiLteBaseEntity, ScannerEntity): async def async_update(self) -> None: """Update state.""" - hosts = _get_hosts(self.router) - if hosts is None: + if (hosts := _get_hosts(self.router)) is None: self._available = False return self._available = True diff --git a/homeassistant/components/knx/expose.py b/homeassistant/components/knx/expose.py index b4b15c977fd..6fa5a3ba728 100644 --- a/homeassistant/components/knx/expose.py +++ b/homeassistant/components/knx/expose.py @@ -137,8 +137,7 @@ class KNXExposeSensor: async def _async_entity_changed(self, event: Event) -> None: """Handle entity change.""" new_state = event.data.get("new_state") - new_value = self._get_expose_value(new_state) - if new_value is None: + if (new_value := self._get_expose_value(new_state)) is None: return old_state = event.data.get("old_state") # don't use default value for comparison on first state change (old_state is None) diff --git a/homeassistant/components/lametric/__init__.py b/homeassistant/components/lametric/__init__.py index 797f0982f00..f51a34aafc1 100644 --- a/homeassistant/components/lametric/__init__.py +++ b/homeassistant/components/lametric/__init__.py @@ -34,8 +34,7 @@ def setup(hass, config): hlmn = HassLaMetricManager( client_id=conf[CONF_CLIENT_ID], client_secret=conf[CONF_CLIENT_SECRET] ) - devices = hlmn.manager.get_devices() - if not devices: + if not (devices := hlmn.manager.get_devices()): _LOGGER.error("No LaMetric devices found") return False diff --git a/homeassistant/components/linode/binary_sensor.py b/homeassistant/components/linode/binary_sensor.py index 6769d72594b..4f602f5b81a 100644 --- a/homeassistant/components/linode/binary_sensor.py +++ b/homeassistant/components/linode/binary_sensor.py @@ -38,8 +38,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): dev = [] for node in nodes: - node_id = linode.get_node_id(node) - if node_id is None: + if (node_id := linode.get_node_id(node)) is None: _LOGGER.error("Node %s is not available", node) return dev.append(LinodeBinarySensor(linode, node_id)) diff --git a/homeassistant/components/linode/switch.py b/homeassistant/components/linode/switch.py index 9002cb7bd11..a2a31ae62a8 100644 --- a/homeassistant/components/linode/switch.py +++ b/homeassistant/components/linode/switch.py @@ -35,8 +35,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): dev = [] for node in nodes: - node_id = linode.get_node_id(node) - if node_id is None: + if (node_id := linode.get_node_id(node)) is None: _LOGGER.error("Node %s is not available", node) return dev.append(LinodeSwitch(linode, node_id)) diff --git a/homeassistant/components/mobile_app/device_action.py b/homeassistant/components/mobile_app/device_action.py index 193c25e482c..3ad43098225 100644 --- a/homeassistant/components/mobile_app/device_action.py +++ b/homeassistant/components/mobile_app/device_action.py @@ -45,9 +45,7 @@ async def async_call_action_from_config( "Unable to resolve webhook ID from the device ID" ) - service_name = get_notify_service(hass, webhook_id) - - if service_name is None: + if (service_name := get_notify_service(hass, webhook_id)) is None: raise InvalidDeviceAutomationConfig( "Unable to find notify service for webhook ID" ) diff --git a/homeassistant/components/netatmo/sensor.py b/homeassistant/components/netatmo/sensor.py index 5e7d5ae7893..5b93bdec27f 100644 --- a/homeassistant/components/netatmo/sensor.py +++ b/homeassistant/components/netatmo/sensor.py @@ -490,9 +490,7 @@ class NetatmoSensor(NetatmoBase, SensorEntity): self._station_id = module_info.get("main_device", self._id) station = self._data.get_station(self._station_id) - device = self._data.get_module(self._id) - - if not device: + if not (device := self._data.get_module(self._id)): # Assume it's a station if module can't be found device = station diff --git a/homeassistant/components/repetier/sensor.py b/homeassistant/components/repetier/sensor.py index b21ff092c67..393d8a16ae3 100644 --- a/homeassistant/components/repetier/sensor.py +++ b/homeassistant/components/repetier/sensor.py @@ -106,8 +106,7 @@ class RepetierSensor(SensorEntity): def update(self): """Update the sensor.""" - data = self._get_data() - if data is None: + if (data := self._get_data()) is None: return state = data.pop("state") _LOGGER.debug("Printer %s State %s", self.name, state) @@ -127,8 +126,7 @@ class RepetierTempSensor(RepetierSensor): def update(self): """Update the sensor.""" - data = self._get_data() - if data is None: + if (data := self._get_data()) is None: return state = data.pop("state") temp_set = data["temp_set"] @@ -155,8 +153,7 @@ class RepetierJobEndSensor(RepetierSensor): def update(self): """Update the sensor.""" - data = self._get_data() - if data is None: + if (data := self._get_data()) is None: return job_name = data["job_name"] start = data["start"] @@ -180,8 +177,7 @@ class RepetierJobStartSensor(RepetierSensor): def update(self): """Update the sensor.""" - data = self._get_data() - if data is None: + if (data := self._get_data()) is None: return job_name = data["job_name"] start = data["start"] diff --git a/homeassistant/components/ripple/sensor.py b/homeassistant/components/ripple/sensor.py index 2746f5789cd..a99bb86c5ef 100644 --- a/homeassistant/components/ripple/sensor.py +++ b/homeassistant/components/ripple/sensor.py @@ -62,7 +62,5 @@ class RippleSensor(SensorEntity): def update(self): """Get the latest state of the sensor.""" - - balance = get_balance(self.address) - if balance is not None: + if (balance := get_balance(self.address)) is not None: self._state = balance diff --git a/homeassistant/components/sky_hub/device_tracker.py b/homeassistant/components/sky_hub/device_tracker.py index fda0b5e3774..8333e8e0cda 100644 --- a/homeassistant/components/sky_hub/device_tracker.py +++ b/homeassistant/components/sky_hub/device_tracker.py @@ -68,9 +68,7 @@ class SkyHubDeviceScanner(DeviceScanner): """Ensure the information from the Sky Hub is up to date.""" _LOGGER.debug("Scanning") - data = await self._hub.async_get_skyhub_data() - - if not data: + if not (data := await self._hub.async_get_skyhub_data()): return self.last_results = data diff --git a/homeassistant/components/thomson/device_tracker.py b/homeassistant/components/thomson/device_tracker.py index 98302ad396a..8c52a6669dc 100644 --- a/homeassistant/components/thomson/device_tracker.py +++ b/homeassistant/components/thomson/device_tracker.py @@ -78,8 +78,7 @@ class ThomsonDeviceScanner(DeviceScanner): return False _LOGGER.info("Checking ARP") - data = self.get_thomson_data() - if not data: + if not (data := self.get_thomson_data()): return False # Flag C stands for CONNECTED diff --git a/homeassistant/components/watson_iot/__init__.py b/homeassistant/components/watson_iot/__init__.py index 8b81a9d741b..cd3599683d0 100644 --- a/homeassistant/components/watson_iot/__init__.py +++ b/homeassistant/components/watson_iot/__init__.py @@ -217,8 +217,7 @@ class WatsonIOTThread(threading.Thread): def run(self): """Process incoming events.""" while not self.shutdown: - event = self.get_events_json() - if event: + if event := self.get_events_json(): self.write_to_watson(event) self.queue.task_done() diff --git a/homeassistant/components/whirlpool/climate.py b/homeassistant/components/whirlpool/climate.py index eb1b88dcc13..ceb68ec29eb 100644 --- a/homeassistant/components/whirlpool/climate.py +++ b/homeassistant/components/whirlpool/climate.py @@ -64,8 +64,7 @@ SUPPORTED_TARGET_TEMPERATURE_STEP = 1 async def async_setup_entry(hass, config_entry, async_add_entities): """Set up entry.""" auth: Auth = hass.data[DOMAIN][config_entry.entry_id][AUTH_INSTANCE_KEY] - said_list = auth.get_said_list() - if not said_list: + if not (said_list := auth.get_said_list()): _LOGGER.debug("No appliances found") return diff --git a/homeassistant/components/xbox_live/sensor.py b/homeassistant/components/xbox_live/sensor.py index c09b707cba0..bbd44498dab 100644 --- a/homeassistant/components/xbox_live/sensor.py +++ b/homeassistant/components/xbox_live/sensor.py @@ -47,8 +47,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): interval = config.get(CONF_SCAN_INTERVAL, interval) for xuid in users: - gamercard = get_user_gamercard(api, xuid) - if gamercard is None: + if (gamercard := get_user_gamercard(api, xuid)) is None: continue entities.append(XboxSensor(api, xuid, gamercard, interval)) diff --git a/homeassistant/components/zwave_js/sensor.py b/homeassistant/components/zwave_js/sensor.py index a3d06a21f89..d8cf32d0f88 100644 --- a/homeassistant/components/zwave_js/sensor.py +++ b/homeassistant/components/zwave_js/sensor.py @@ -335,8 +335,7 @@ class ZWaveMeterSensor(ZWaveNumericSensor): @property def extra_state_attributes(self) -> Mapping[str, int | str] | None: """Return extra state attributes.""" - meter_type = get_meter_type(self.info.primary_value) - if meter_type: + if meter_type := get_meter_type(self.info.primary_value): return { ATTR_METER_TYPE: meter_type.value, ATTR_METER_TYPE_NAME: meter_type.name, diff --git a/homeassistant/util/location.py b/homeassistant/util/location.py index abe8fedd21a..b967a6a0b1e 100644 --- a/homeassistant/util/location.py +++ b/homeassistant/util/location.py @@ -52,9 +52,7 @@ async def async_detect_location_info( session: aiohttp.ClientSession, ) -> LocationInfo | None: """Detect location information.""" - data = await _get_whoami(session) - - if data is None: + if (data := await _get_whoami(session)) is None: return None data["use_metric"] = data["country_code"] not in ("US", "MM", "LR")