diff --git a/homeassistant/components/aemet/weather_update_coordinator.py b/homeassistant/components/aemet/weather_update_coordinator.py index 77f4a593fb0..68c979ee27c 100644 --- a/homeassistant/components/aemet/weather_update_coordinator.py +++ b/homeassistant/components/aemet/weather_update_coordinator.py @@ -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 diff --git a/homeassistant/components/bt_smarthub/device_tracker.py b/homeassistant/components/bt_smarthub/device_tracker.py index ef4e89bd4bb..a4c8717e444 100644 --- a/homeassistant/components/bt_smarthub/device_tracker.py +++ b/homeassistant/components/bt_smarthub/device_tracker.py @@ -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 diff --git a/homeassistant/components/forked_daapd/media_player.py b/homeassistant/components/forked_daapd/media_player.py index aeb2350ce22..f19209a0b2d 100644 --- a/homeassistant/components/forked_daapd/media_player.py +++ b/homeassistant/components/forked_daapd/media_player.py @@ -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[ diff --git a/homeassistant/components/media_extractor/__init__.py b/homeassistant/components/media_extractor/__init__.py index a223385d8e8..ded5e3e265e 100644 --- a/homeassistant/components/media_extractor/__init__.py +++ b/homeassistant/components/media_extractor/__init__.py @@ -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: diff --git a/homeassistant/components/plugwise/binary_sensor.py b/homeassistant/components/plugwise/binary_sensor.py index 023ffa3de70..5faf5f00dde 100644 --- a/homeassistant/components/plugwise/binary_sensor.py +++ b/homeassistant/components/plugwise/binary_sensor.py @@ -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 diff --git a/homeassistant/components/plugwise/sensor.py b/homeassistant/components/plugwise/sensor.py index 6b33eccc753..59e7858f947 100644 --- a/homeassistant/components/plugwise/sensor.py +++ b/homeassistant/components/plugwise/sensor.py @@ -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 diff --git a/homeassistant/components/plugwise/switch.py b/homeassistant/components/plugwise/switch.py index ce3be04681a..033fbcd7693 100644 --- a/homeassistant/components/plugwise/switch.py +++ b/homeassistant/components/plugwise/switch.py @@ -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 diff --git a/homeassistant/components/snmp/device_tracker.py b/homeassistant/components/snmp/device_tracker.py index 30ec5cd41a3..aeaf3c72e0f 100644 --- a/homeassistant/components/snmp/device_tracker.py +++ b/homeassistant/components/snmp/device_tracker.py @@ -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 diff --git a/homeassistant/components/ubus/device_tracker.py b/homeassistant/components/ubus/device_tracker.py index dc5cd8857f8..4ccba81d86c 100644 --- a/homeassistant/components/ubus/device_tracker.py +++ b/homeassistant/components/ubus/device_tracker.py @@ -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"]: diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 0073ecfb44b..f08fbe36511 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -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) diff --git a/homeassistant/helpers/config_entry_oauth2_flow.py b/homeassistant/helpers/config_entry_oauth2_flow.py index 54f257cb781..42478e67bb9 100644 --- a/homeassistant/helpers/config_entry_oauth2_flow.py +++ b/homeassistant/helpers/config_entry_oauth2_flow.py @@ -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 diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index a181fbbfb44..f090b28210f 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -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