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