Use assignment expressions 39 ()

pull/58841/head
Marc Mueller 2021-10-31 18:56:25 +01:00 committed by GitHub
parent 72801867d6
commit e0c0d00833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 31 additions and 70 deletions
homeassistant

View File

@ -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()

View File

@ -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()

View File

@ -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 = []

View File

@ -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()

View File

@ -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,

View File

@ -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:

View File

@ -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():

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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))

View File

@ -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))

View File

@ -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"
)

View File

@ -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

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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))

View File

@ -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,

View File

@ -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")