From 70469e0979ec6a8032389088c745484067e2d9c6 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 22 Oct 2021 11:13:05 +0200 Subject: [PATCH] Use assignment expressions 23 (#58180) --- homeassistant/components/apns/notify.py | 4 +--- .../components/asuswrt/config_flow.py | 3 +-- homeassistant/components/fints/sensor.py | 3 +-- .../components/homekit/aidmanager.py | 3 +-- .../components/homematicip_cloud/climate.py | 3 +-- .../components/homematicip_cloud/services.py | 20 ++++++------------- homeassistant/components/nest/__init__.py | 3 +-- .../components/nest/device_trigger.py | 16 +++++++-------- .../components/netgear/device_tracker.py | 3 +-- .../components/samsungtv/config_flow.py | 3 +-- homeassistant/components/sentry/__init__.py | 3 +-- homeassistant/components/spaceapi/__init__.py | 6 ++---- .../components/thinkingcleaner/sensor.py | 4 +--- .../components/thinkingcleaner/switch.py | 3 +-- homeassistant/components/trace/__init__.py | 3 +-- homeassistant/components/tuya/cover.py | 6 ++---- 16 files changed, 29 insertions(+), 57 deletions(-) diff --git a/homeassistant/components/apns/notify.py b/homeassistant/components/apns/notify.py index a87cae09b1a..e0287897e96 100644 --- a/homeassistant/components/apns/notify.py +++ b/homeassistant/components/apns/notify.py @@ -220,9 +220,7 @@ class ApnsNotificationService(BaseNotificationService): ) device_state = kwargs.get(ATTR_TARGET) - message_data = kwargs.get(ATTR_DATA) - - if message_data is None: + if (message_data := kwargs.get(ATTR_DATA)) is None: message_data = {} if isinstance(message, str): diff --git a/homeassistant/components/asuswrt/config_flow.py b/homeassistant/components/asuswrt/config_flow.py index c48ea4d57fe..5a20880b4b0 100644 --- a/homeassistant/components/asuswrt/config_flow.py +++ b/homeassistant/components/asuswrt/config_flow.py @@ -217,8 +217,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow): } ) - conf_mode = self.config_entry.data[CONF_MODE] - if conf_mode == MODE_AP: + if self.config_entry.data[CONF_MODE] == MODE_AP: data_schema = data_schema.extend( { vol.Optional( diff --git a/homeassistant/components/fints/sensor.py b/homeassistant/components/fints/sensor.py index d584bbed4bb..a7bec82cee0 100644 --- a/homeassistant/components/fints/sensor.py +++ b/homeassistant/components/fints/sensor.py @@ -79,8 +79,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): _LOGGER.info("Skipping account %s for bank %s", account.iban, fints_name) continue - account_name = account_config.get(account.iban) - if not account_name: + if not (account_name := account_config.get(account.iban)): account_name = f"{fints_name} - {account.iban}" accounts.append(FinTsAccount(client, account, account_name)) _LOGGER.debug("Creating account %s for bank %s", account.iban, fints_name) diff --git a/homeassistant/components/homekit/aidmanager.py b/homeassistant/components/homekit/aidmanager.py index ddf3c7c564e..0f5e29426a8 100644 --- a/homeassistant/components/homekit/aidmanager.py +++ b/homeassistant/components/homekit/aidmanager.py @@ -92,8 +92,7 @@ class AccessoryAidStorage: def get_or_allocate_aid_for_entity_id(self, entity_id: str): """Generate a stable aid for an entity id.""" - entity = self._entity_registry.async_get(entity_id) - if not entity: + if not (entity := self._entity_registry.async_get(entity_id)): return self.get_or_allocate_aid(None, entity_id) sys_unique_id = get_system_unique_id(entity) diff --git a/homeassistant/components/homematicip_cloud/climate.py b/homeassistant/components/homematicip_cloud/climate.py index 1b6c2491e2e..060d265c62a 100644 --- a/homeassistant/components/homematicip_cloud/climate.py +++ b/homeassistant/components/homematicip_cloud/climate.py @@ -207,8 +207,7 @@ class HomematicipHeatingGroup(HomematicipGenericEntity, ClimateEntity): async def async_set_temperature(self, **kwargs) -> None: """Set new target temperature.""" - temperature = kwargs.get(ATTR_TEMPERATURE) - if temperature is None: + if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: return if self.min_temp <= temperature <= self.max_temp: diff --git a/homeassistant/components/homematicip_cloud/services.py b/homeassistant/components/homematicip_cloud/services.py index 45795f8858e..45b47b40efa 100644 --- a/homeassistant/components/homematicip_cloud/services.py +++ b/homeassistant/components/homematicip_cloud/services.py @@ -208,9 +208,8 @@ async def _async_activate_eco_mode_with_duration( ) -> None: """Service to activate eco mode with duration.""" duration = service.data[ATTR_DURATION] - hapid = service.data.get(ATTR_ACCESSPOINT_ID) - if hapid: + if hapid := service.data.get(ATTR_ACCESSPOINT_ID): home = _get_home(hass, hapid) if home: await home.activate_absence_with_duration(duration) @@ -224,9 +223,8 @@ async def _async_activate_eco_mode_with_period( ) -> None: """Service to activate eco mode with period.""" endtime = service.data[ATTR_ENDTIME] - hapid = service.data.get(ATTR_ACCESSPOINT_ID) - if hapid: + if hapid := service.data.get(ATTR_ACCESSPOINT_ID): home = _get_home(hass, hapid) if home: await home.activate_absence_with_period(endtime) @@ -239,9 +237,8 @@ async def _async_activate_vacation(hass: HomeAssistant, service: ServiceCall) -> """Service to activate vacation.""" endtime = service.data[ATTR_ENDTIME] temperature = service.data[ATTR_TEMPERATURE] - hapid = service.data.get(ATTR_ACCESSPOINT_ID) - if hapid: + if hapid := service.data.get(ATTR_ACCESSPOINT_ID): home = _get_home(hass, hapid) if home: await home.activate_vacation(endtime, temperature) @@ -252,9 +249,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.""" - hapid = service.data.get(ATTR_ACCESSPOINT_ID) - - if hapid: + if hapid := service.data.get(ATTR_ACCESSPOINT_ID): home = _get_home(hass, hapid) if home: await home.deactivate_absence() @@ -265,9 +260,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.""" - hapid = service.data.get(ATTR_ACCESSPOINT_ID) - - if hapid: + if hapid := service.data.get(ATTR_ACCESSPOINT_ID): home = _get_home(hass, hapid) if home: await home.deactivate_vacation() @@ -337,8 +330,7 @@ async def _async_reset_energy_counter(hass: HomeAssistant, service: ServiceCall) def _get_home(hass: HomeAssistant, hapid: str) -> AsyncHome | None: """Return a HmIP home.""" - hap = hass.data[HMIPC_DOMAIN].get(hapid) - if hap: + if hap := hass.data[HMIPC_DOMAIN].get(hapid): return hap.home _LOGGER.info("No matching access point found for access point id %s", hapid) diff --git a/homeassistant/components/nest/__init__.py b/homeassistant/components/nest/__init__.py index 9ca7d530f79..344bdb74970 100644 --- a/homeassistant/components/nest/__init__.py +++ b/homeassistant/components/nest/__init__.py @@ -123,8 +123,7 @@ class SignalUpdateCallback: if not device_entry: return for event in events: - event_type = EVENT_NAME_MAP.get(event) - if not event_type: + if not (event_type := EVENT_NAME_MAP.get(event)): continue message = { "device_id": device_entry.id, diff --git a/homeassistant/components/nest/device_trigger.py b/homeassistant/components/nest/device_trigger.py index bcd5b6b96b3..28eb444b91a 100644 --- a/homeassistant/components/nest/device_trigger.py +++ b/homeassistant/components/nest/device_trigger.py @@ -38,8 +38,7 @@ async def async_get_nest_device_id(hass: HomeAssistant, device_id: str) -> str | device_registry: DeviceRegistry = ( await hass.helpers.device_registry.async_get_registry() ) - device = device_registry.async_get(device_id) - if device: + if device := device_registry.async_get(device_id): for (domain, unique_id) in device.identifiers: if domain == DOMAIN: return unique_id @@ -54,16 +53,15 @@ async def async_get_device_trigger_types( # "shouldn't happen" cases subscriber = hass.data[DOMAIN][DATA_SUBSCRIBER] device_manager = await subscriber.async_get_device_manager() - nest_device = device_manager.devices.get(nest_device_id) - if not nest_device: + if not (nest_device := device_manager.devices.get(nest_device_id)): raise InvalidDeviceAutomationConfig(f"Nest device not found {nest_device_id}") # Determine the set of event types based on the supported device traits - trigger_types = [] - for trait in nest_device.traits: - trigger_type = DEVICE_TRAIT_TRIGGER_MAP.get(trait) - if trigger_type: - trigger_types.append(trigger_type) + trigger_types = [ + trigger_type + for trait in nest_device.traits + if (trigger_type := DEVICE_TRAIT_TRIGGER_MAP.get(trait)) + ] return trigger_types diff --git a/homeassistant/components/netgear/device_tracker.py b/homeassistant/components/netgear/device_tracker.py index 0d3a1098f0c..f7c92a271b9 100644 --- a/homeassistant/components/netgear/device_tracker.py +++ b/homeassistant/components/netgear/device_tracker.py @@ -86,8 +86,7 @@ class NetgearScannerEntity(NetgearDeviceEntity, ScannerEntity): def get_hostname(self): """Return the hostname of the given device or None if we don't know.""" - hostname = self._device["name"] - if hostname == "--": + if (hostname := self._device["name"]) == "--": return None return hostname diff --git a/homeassistant/components/samsungtv/config_flow.py b/homeassistant/components/samsungtv/config_flow.py index bcce5eec5ed..c75086322da 100644 --- a/homeassistant/components/samsungtv/config_flow.py +++ b/homeassistant/components/samsungtv/config_flow.py @@ -159,8 +159,7 @@ class SamsungTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED) return False dev_info = info.get("device", {}) - device_type = dev_info.get("type") - if device_type != "Samsung SmartTV": + if (device_type := dev_info.get("type")) != "Samsung SmartTV": raise data_entry_flow.AbortFlow(RESULT_NOT_SUPPORTED) self._model = dev_info.get("modelName") name = dev_info.get("name") diff --git a/homeassistant/components/sentry/__init__.py b/homeassistant/components/sentry/__init__.py index c34bc2b350a..350ccfa5b8d 100644 --- a/homeassistant/components/sentry/__init__.py +++ b/homeassistant/components/sentry/__init__.py @@ -154,8 +154,7 @@ def process_before_send( ] # Add additional tags based on what caused the event. - platform = entity_platform.current_platform.get() - if platform is not None: + if (platform := entity_platform.current_platform.get()) is not None: # This event happened in a platform additional_tags["custom_component"] = "no" additional_tags["integration"] = platform.platform_name diff --git a/homeassistant/components/spaceapi/__init__.py b/homeassistant/components/spaceapi/__init__.py index 66583050b20..df4f6617ee0 100644 --- a/homeassistant/components/spaceapi/__init__.py +++ b/homeassistant/components/spaceapi/__init__.py @@ -249,8 +249,7 @@ class APISpaceApiView(HomeAssistantView): @staticmethod def get_sensor_data(hass, spaceapi, sensor): """Get data from a sensor.""" - sensor_state = hass.states.get(sensor) - if not sensor_state: + if not (sensor_state := hass.states.get(sensor)): return None sensor_data = {ATTR_NAME: sensor_state.name, ATTR_VALUE: sensor_state.state} if ATTR_SENSOR_LOCATION in sensor_state.attributes: @@ -279,9 +278,8 @@ class APISpaceApiView(HomeAssistantView): pass state_entity = spaceapi["state"][ATTR_ENTITY_ID] - space_state = hass.states.get(state_entity) - if space_state is not None: + if (space_state := hass.states.get(state_entity)) is not None: state = { ATTR_OPEN: space_state.state != "off", ATTR_LASTCHANGE: dt_util.as_timestamp(space_state.last_updated), diff --git a/homeassistant/components/thinkingcleaner/sensor.py b/homeassistant/components/thinkingcleaner/sensor.py index 588fc212138..76ad6578ae2 100644 --- a/homeassistant/components/thinkingcleaner/sensor.py +++ b/homeassistant/components/thinkingcleaner/sensor.py @@ -67,9 +67,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Optional(CONF_HOST): cv.string}) def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the ThinkingCleaner platform.""" - - host = config.get(CONF_HOST) - if host: + if host := config.get(CONF_HOST): devices = [ThinkingCleaner(host, "unknown")] else: discovery = Discovery() diff --git a/homeassistant/components/thinkingcleaner/switch.py b/homeassistant/components/thinkingcleaner/switch.py index cad94b72023..0f24ba6e755 100644 --- a/homeassistant/components/thinkingcleaner/switch.py +++ b/homeassistant/components/thinkingcleaner/switch.py @@ -42,8 +42,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Optional(CONF_HOST): cv.string}) def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the ThinkingCleaner platform.""" - host = config.get(CONF_HOST) - if host: + if host := config.get(CONF_HOST): devices = [ThinkingCleaner(host, "unknown")] else: discovery = Discovery() diff --git a/homeassistant/components/trace/__init__.py b/homeassistant/components/trace/__init__.py index 2f41365cb2f..29ed9f4d062 100644 --- a/homeassistant/components/trace/__init__.py +++ b/homeassistant/components/trace/__init__.py @@ -132,8 +132,7 @@ async def async_list_traces(hass, wanted_domain, wanted_key): def async_store_trace(hass, trace, stored_traces): """Store a trace if its key is valid.""" - key = trace.key - if key: + if key := trace.key: traces = hass.data[DATA_TRACE] if key not in traces: traces[key] = LimitedSizeDict(size_limit=stored_traces) diff --git a/homeassistant/components/tuya/cover.py b/homeassistant/components/tuya/cover.py index bc5a5b85772..0b8a658fd7c 100644 --- a/homeassistant/components/tuya/cover.py +++ b/homeassistant/components/tuya/cover.py @@ -239,8 +239,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): ): return None - position = self.device.status.get(dpcode) - if position is None: + if (position := self.device.status.get(dpcode)) is None: return None return round( @@ -256,8 +255,7 @@ class TuyaCoverEntity(TuyaEntity, CoverEntity): if self._tilt_dpcode is None or self._tilt_type is None: return None - angle = self.device.status.get(self._tilt_dpcode) - if angle is None: + if (angle := self.device.status.get(self._tilt_dpcode)) is None: return None return round(self._tilt_type.remap_value_to(angle, 0, 100))