Use assignment expressions 35 (#58824)
This commit is contained in:
parent
ab7d8db481
commit
3f1b4906bf
12 changed files with 26 additions and 65 deletions
|
@ -398,8 +398,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
return None
|
||||
|
||||
def _convert_forecast_day(self, date, day):
|
||||
condition = self._get_condition_day(day)
|
||||
if not condition:
|
||||
if not (condition := self._get_condition_day(day)):
|
||||
return None
|
||||
|
||||
return {
|
||||
|
@ -415,8 +414,7 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
}
|
||||
|
||||
def _convert_forecast_hour(self, date, day, hour):
|
||||
condition = self._get_condition(day, hour)
|
||||
if not condition:
|
||||
if not (condition := self._get_condition(day, hour)):
|
||||
return None
|
||||
|
||||
forecast_dt = date.replace(hour=hour, minute=0, second=0)
|
||||
|
@ -435,13 +433,8 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
|
||||
def _calc_precipitation(self, day, hour):
|
||||
"""Calculate the precipitation."""
|
||||
rain_value = self._get_rain(day, hour)
|
||||
if not rain_value:
|
||||
rain_value = 0
|
||||
|
||||
snow_value = self._get_snow(day, hour)
|
||||
if not snow_value:
|
||||
snow_value = 0
|
||||
rain_value = self._get_rain(day, hour) or 0
|
||||
snow_value = self._get_snow(day, hour) or 0
|
||||
|
||||
if round(rain_value + snow_value, 1) == 0:
|
||||
return None
|
||||
|
@ -449,13 +442,8 @@ class WeatherUpdateCoordinator(DataUpdateCoordinator):
|
|||
|
||||
def _calc_precipitation_prob(self, day, hour):
|
||||
"""Calculate the precipitation probability (hour)."""
|
||||
rain_value = self._get_rain_prob(day, hour)
|
||||
if not rain_value:
|
||||
rain_value = 0
|
||||
|
||||
snow_value = self._get_snow_prob(day, hour)
|
||||
if not snow_value:
|
||||
snow_value = 0
|
||||
rain_value = self._get_rain_prob(day, hour) or 0
|
||||
snow_value = self._get_snow_prob(day, hour) or 0
|
||||
|
||||
if rain_value == 0 and snow_value == 0:
|
||||
return None
|
||||
|
|
|
@ -59,8 +59,7 @@ class BTSmartHubScanner(DeviceScanner):
|
|||
self.success_init = False
|
||||
|
||||
# Test the router is accessible
|
||||
data = self.get_bt_smarthub_data()
|
||||
if data:
|
||||
if self.get_bt_smarthub_data():
|
||||
self.success_init = True
|
||||
else:
|
||||
_LOGGER.info("Failed to connect to %s", self.smarthub.router_ip)
|
||||
|
@ -85,8 +84,7 @@ class BTSmartHubScanner(DeviceScanner):
|
|||
return
|
||||
|
||||
_LOGGER.info("Scanning")
|
||||
data = self.get_bt_smarthub_data()
|
||||
if not data:
|
||||
if not (data := self.get_bt_smarthub_data()):
|
||||
_LOGGER.warning("Error scanning devices")
|
||||
return
|
||||
self.last_results = data
|
||||
|
|
|
@ -797,8 +797,7 @@ class ForkedDaapdUpdater:
|
|||
if (
|
||||
"queue" in update_types
|
||||
): # update queue, queue before player for async_play_media
|
||||
queue = await self._api.get_request("queue")
|
||||
if queue:
|
||||
if queue := await self._api.get_request("queue"):
|
||||
update_events["queue"] = asyncio.Event()
|
||||
async_dispatcher_send(
|
||||
self.hass,
|
||||
|
@ -808,8 +807,7 @@ class ForkedDaapdUpdater:
|
|||
)
|
||||
# order of below don't matter
|
||||
if not {"outputs", "volume"}.isdisjoint(update_types): # update outputs
|
||||
outputs = await self._api.get_request("outputs")
|
||||
if outputs:
|
||||
if outputs := await self._api.get_request("outputs"):
|
||||
outputs = outputs["outputs"]
|
||||
update_events[
|
||||
"outputs"
|
||||
|
@ -838,8 +836,7 @@ class ForkedDaapdUpdater:
|
|||
if not {"player", "options", "volume"}.isdisjoint(
|
||||
update_types
|
||||
): # update player
|
||||
player = await self._api.get_request("player")
|
||||
if player:
|
||||
if player := await self._api.get_request("player"):
|
||||
update_events["player"] = asyncio.Event()
|
||||
if update_events.get("queue"):
|
||||
await update_events[
|
||||
|
|
|
@ -89,9 +89,7 @@ class MediaExtractor:
|
|||
"Could not retrieve data for the URL: %s", self.get_media_url()
|
||||
)
|
||||
else:
|
||||
entities = self.get_entities()
|
||||
|
||||
if not entities:
|
||||
if not (entities := self.get_entities()):
|
||||
self.call_media_player_service(stream_selector, None)
|
||||
|
||||
for entity_id in entities:
|
||||
|
|
|
@ -112,9 +112,7 @@ class PwBinarySensor(SmileBinarySensor, BinarySensorEntity):
|
|||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
data = self._api.get_device_data(self._dev_id)
|
||||
|
||||
if not data:
|
||||
if not (data := self._api.get_device_data(self._dev_id)):
|
||||
_LOGGER.error("Received no data for device %s", self._binary_sensor)
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
|
|
@ -361,9 +361,7 @@ class PwThermostatSensor(SmileSensor):
|
|||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
data = self._api.get_device_data(self._dev_id)
|
||||
|
||||
if not data:
|
||||
if not (data := self._api.get_device_data(self._dev_id)):
|
||||
_LOGGER.error("Received no data for device %s", self._entity_name)
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
@ -388,9 +386,7 @@ class PwAuxDeviceSensor(SmileSensor):
|
|||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
data = self._api.get_device_data(self._dev_id)
|
||||
|
||||
if not data:
|
||||
if not (data := self._api.get_device_data(self._dev_id)):
|
||||
_LOGGER.error("Received no data for device %s", self._entity_name)
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
@ -434,9 +430,7 @@ class PwPowerSensor(SmileSensor):
|
|||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the entity."""
|
||||
data = self._api.get_device_data(self._dev_id)
|
||||
|
||||
if not data:
|
||||
if not (data := self._api.get_device_data(self._dev_id)):
|
||||
_LOGGER.error("Received no data for device %s", self._entity_name)
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
|
|
@ -103,9 +103,7 @@ class GwSwitch(SmileGateway, SwitchEntity):
|
|||
@callback
|
||||
def _async_process_data(self):
|
||||
"""Update the data from the Plugs."""
|
||||
data = self._api.get_device_data(self._dev_id)
|
||||
|
||||
if not data:
|
||||
if not (data := self._api.get_device_data(self._dev_id)):
|
||||
_LOGGER.error("Received no data for device %s", self._name)
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
|
|
@ -86,8 +86,7 @@ class SnmpScanner(DeviceScanner):
|
|||
if not self.success_init:
|
||||
return False
|
||||
|
||||
data = self.get_snmp_data()
|
||||
if not data:
|
||||
if not (data := self.get_snmp_data()):
|
||||
return False
|
||||
|
||||
self.last_results = data
|
||||
|
|
|
@ -125,9 +125,7 @@ class UbusDeviceScanner(DeviceScanner):
|
|||
results = 0
|
||||
# for each access point
|
||||
for hostapd in self.hostapd:
|
||||
result = self.ubus.get_hostapd_clients(hostapd)
|
||||
|
||||
if result:
|
||||
if result := self.ubus.get_hostapd_clients(hostapd):
|
||||
results = results + 1
|
||||
# Check for each device is authorized (valid wpa key)
|
||||
for key in result["clients"].keys():
|
||||
|
@ -148,8 +146,7 @@ class DnsmasqUbusDeviceScanner(UbusDeviceScanner):
|
|||
|
||||
def _generate_mac2name(self):
|
||||
if self.leasefile is None:
|
||||
result = self.ubus.get_uci_config("dhcp", "dnsmasq")
|
||||
if result:
|
||||
if result := self.ubus.get_uci_config("dhcp", "dnsmasq"):
|
||||
values = result["values"].values()
|
||||
self.leasefile = next(iter(values))["leasefile"]
|
||||
else:
|
||||
|
@ -170,8 +167,7 @@ class OdhcpdUbusDeviceScanner(UbusDeviceScanner):
|
|||
"""Implement the Ubus device scanning for the odhcp DHCP server."""
|
||||
|
||||
def _generate_mac2name(self):
|
||||
result = self.ubus.get_dhcp_method("ipv4leases")
|
||||
if result:
|
||||
if result := self.ubus.get_dhcp_method("ipv4leases"):
|
||||
self.mac2name = {}
|
||||
for device in result["device"].values():
|
||||
for lease in device["leases"]:
|
||||
|
|
|
@ -73,8 +73,7 @@ class AreaRegistry:
|
|||
@callback
|
||||
def async_get_or_create(self, name: str) -> AreaEntry:
|
||||
"""Get or create an area."""
|
||||
area = self.async_get_area_by_name(name)
|
||||
if area:
|
||||
if area := self.async_get_area_by_name(name):
|
||||
return area
|
||||
return self.async_create(name)
|
||||
|
||||
|
|
|
@ -356,8 +356,7 @@ async def async_get_implementations(
|
|||
registered = dict(registered)
|
||||
|
||||
for provider_domain, get_impl in hass.data[DATA_PROVIDERS].items():
|
||||
implementation = await get_impl(hass, domain)
|
||||
if implementation is not None:
|
||||
if (implementation := await get_impl(hass, domain)) is not None:
|
||||
registered[provider_domain] = implementation
|
||||
|
||||
return registered
|
||||
|
|
|
@ -886,8 +886,7 @@ def expand(hass: HomeAssistant, *args: Any) -> Iterable[State]:
|
|||
entity = search.pop()
|
||||
if isinstance(entity, str):
|
||||
entity_id = entity
|
||||
entity = _get_state(hass, entity)
|
||||
if entity is None:
|
||||
if (entity := _get_state(hass, entity)) is None:
|
||||
continue
|
||||
elif isinstance(entity, State):
|
||||
entity_id = entity.entity_id
|
||||
|
@ -1004,8 +1003,7 @@ def _get_area_name(area_reg: area_registry.AreaRegistry, valid_area_id: str) ->
|
|||
def area_name(hass: HomeAssistant, lookup_value: str) -> str | None:
|
||||
"""Get the area name from an area id, device id, or entity id."""
|
||||
area_reg = area_registry.async_get(hass)
|
||||
area = area_reg.async_get_area(lookup_value)
|
||||
if area:
|
||||
if area := area_reg.async_get_area(lookup_value):
|
||||
return area.name
|
||||
|
||||
dev_reg = device_registry.async_get(hass)
|
||||
|
@ -1226,8 +1224,7 @@ def is_state_attr(hass: HomeAssistant, entity_id: str, name: str, value: Any) ->
|
|||
|
||||
def state_attr(hass: HomeAssistant, entity_id: str, name: str) -> Any:
|
||||
"""Get a specific attribute from a state."""
|
||||
state_obj = _get_state(hass, entity_id)
|
||||
if state_obj is not None:
|
||||
if (state_obj := _get_state(hass, entity_id)) is not None:
|
||||
return state_obj.attributes.get(name)
|
||||
return None
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue