Fix ruff redefined-argument-from-local PLR1704 (#120729)

* Fix PLR1704

* Fix
This commit is contained in:
Joost Lekkerkerker 2024-06-28 12:31:07 +02:00 committed by GitHub
parent 4437c4a204
commit 6ef8e87f88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 59 additions and 46 deletions

View file

@ -1048,29 +1048,32 @@ async def _async_process_config(
automation_configs_with_id: dict[str, tuple[int, AutomationEntityConfig]] = {} automation_configs_with_id: dict[str, tuple[int, AutomationEntityConfig]] = {}
automation_configs_without_id: list[tuple[int, AutomationEntityConfig]] = [] automation_configs_without_id: list[tuple[int, AutomationEntityConfig]] = []
for config_idx, config in enumerate(automation_configs): for config_idx, automation_config in enumerate(automation_configs):
if automation_id := config.config_block.get(CONF_ID): if automation_id := automation_config.config_block.get(CONF_ID):
automation_configs_with_id[automation_id] = (config_idx, config) automation_configs_with_id[automation_id] = (
config_idx,
automation_config,
)
continue continue
automation_configs_without_id.append((config_idx, config)) automation_configs_without_id.append((config_idx, automation_config))
for automation_idx, automation in enumerate(automations): for automation_idx, automation in enumerate(automations):
if automation.unique_id: if automation.unique_id:
if automation.unique_id not in automation_configs_with_id: if automation.unique_id not in automation_configs_with_id:
continue continue
config_idx, config = automation_configs_with_id.pop( config_idx, automation_config = automation_configs_with_id.pop(
automation.unique_id automation.unique_id
) )
if automation_matches_config(automation, config): if automation_matches_config(automation, automation_config):
automation_matches.add(automation_idx) automation_matches.add(automation_idx)
config_matches.add(config_idx) config_matches.add(config_idx)
continue continue
for config_idx, config in automation_configs_without_id: for config_idx, automation_config in automation_configs_without_id:
if config_idx in config_matches: if config_idx in config_matches:
# Only allow an automation config to match at most once # Only allow an automation config to match at most once
continue continue
if automation_matches_config(automation, config): if automation_matches_config(automation, automation_config):
automation_matches.add(automation_idx) automation_matches.add(automation_idx)
config_matches.add(config_idx) config_matches.add(config_idx)
# Only allow an automation to match at most once # Only allow an automation to match at most once

View file

@ -46,8 +46,10 @@ async def async_discover_devices(
targets = [address] targets = [address]
else: else:
targets = [ targets = [
str(address) str(broadcast_address)
for address in await network.async_get_ipv4_broadcast_addresses(hass) for broadcast_address in await network.async_get_ipv4_broadcast_addresses(
hass
)
] ]
scanner = AIOELKDiscovery() scanner = AIOELKDiscovery()
@ -55,8 +57,8 @@ async def async_discover_devices(
for idx, discovered in enumerate( for idx, discovered in enumerate(
await asyncio.gather( await asyncio.gather(
*[ *[
scanner.async_scan(timeout=timeout, address=address) scanner.async_scan(timeout=timeout, address=target_address)
for address in targets for target_address in targets
], ],
return_exceptions=True, return_exceptions=True,
) )

View file

@ -178,16 +178,20 @@ async def async_discover_devices(
targets = [address] targets = [address]
else: else:
targets = [ targets = [
str(address) str(broadcast_address)
for address in await network.async_get_ipv4_broadcast_addresses(hass) for broadcast_address in await network.async_get_ipv4_broadcast_addresses(
hass
)
] ]
scanner = AIOBulbScanner() scanner = AIOBulbScanner()
for idx, discovered in enumerate( for idx, discovered in enumerate(
await asyncio.gather( await asyncio.gather(
*[ *[
create_eager_task(scanner.async_scan(timeout=timeout, address=address)) create_eager_task(
for address in targets scanner.async_scan(timeout=timeout, address=target_address)
)
for target_address in targets
], ],
return_exceptions=True, return_exceptions=True,
) )

View file

@ -110,7 +110,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True return True
async def async_setup_entry(hass: HomeAssistant, entry: HabiticaConfigEntry) -> bool: async def async_setup_entry(
hass: HomeAssistant, config_entry: HabiticaConfigEntry
) -> bool:
"""Set up habitica from a config entry.""" """Set up habitica from a config entry."""
class HAHabitipyAsync(HabitipyAsync): class HAHabitipyAsync(HabitipyAsync):
@ -147,9 +149,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: HabiticaConfigEntry) ->
websession = async_get_clientsession(hass) websession = async_get_clientsession(hass)
url = entry.data[CONF_URL] url = config_entry.data[CONF_URL]
username = entry.data[CONF_API_USER] username = config_entry.data[CONF_API_USER]
password = entry.data[CONF_API_KEY] password = config_entry.data[CONF_API_KEY]
api = await hass.async_add_executor_job( api = await hass.async_add_executor_job(
HAHabitipyAsync, HAHabitipyAsync,
@ -169,18 +171,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: HabiticaConfigEntry) ->
) from e ) from e
raise ConfigEntryNotReady(e) from e raise ConfigEntryNotReady(e) from e
if not entry.data.get(CONF_NAME): if not config_entry.data.get(CONF_NAME):
name = user["profile"]["name"] name = user["profile"]["name"]
hass.config_entries.async_update_entry( hass.config_entries.async_update_entry(
entry, config_entry,
data={**entry.data, CONF_NAME: name}, data={**config_entry.data, CONF_NAME: name},
) )
coordinator = HabiticaDataUpdateCoordinator(hass, api) coordinator = HabiticaDataUpdateCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
entry.runtime_data = coordinator config_entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
if not hass.services.has_service(DOMAIN, SERVICE_API_CALL): if not hass.services.has_service(DOMAIN, SERVICE_API_CALL):
hass.services.async_register( hass.services.async_register(

View file

@ -99,8 +99,8 @@ def ws_subscribe_system_status(
"memory_free_mb": round(virtual_memory.available / 1024**2, 1), "memory_free_mb": round(virtual_memory.available / 1024**2, 1),
"timestamp": dt_util.utcnow().isoformat(), "timestamp": dt_util.utcnow().isoformat(),
} }
for connection, msg_id in system_status.subscribers: for conn, msg_id in system_status.subscribers:
connection.send_message(websocket_api.event_message(msg_id, json_msg)) conn.send_message(websocket_api.event_message(msg_id, json_msg))
if not system_status.subscribers: if not system_status.subscribers:
system_status.remove_periodic_timer = async_track_time_interval( system_status.remove_periodic_timer = async_track_time_interval(

View file

@ -369,11 +369,11 @@ async def _async_process_config(
config_matches: set[int] = set() config_matches: set[int] = set()
for script_idx, script in enumerate(scripts): for script_idx, script in enumerate(scripts):
for config_idx, config in enumerate(script_configs): for config_idx, script_config in enumerate(script_configs):
if config_idx in config_matches: if config_idx in config_matches:
# Only allow a script config to match at most once # Only allow a script config to match at most once
continue continue
if script_matches_config(script, config): if script_matches_config(script, script_config):
script_matches.add(script_idx) script_matches.add(script_idx)
config_matches.add(config_idx) config_matches.add(config_idx)
# Only allow a script to match at most once # Only allow a script to match at most once

View file

@ -62,16 +62,18 @@ async def async_discover_devices(
targets = [address] targets = [address]
else: else:
targets = [ targets = [
str(address) str(broadcast_address)
for address in await network.async_get_ipv4_broadcast_addresses(hass) for broadcast_address in await network.async_get_ipv4_broadcast_addresses(
hass
)
] ]
scanner = AIODiscovery30303() scanner = AIODiscovery30303()
for idx, discovered in enumerate( for idx, discovered in enumerate(
await asyncio.gather( await asyncio.gather(
*[ *[
scanner.async_scan(timeout=timeout, address=address) scanner.async_scan(timeout=timeout, address=target_address)
for address in targets for target_address in targets
], ],
return_exceptions=True, return_exceptions=True,
) )

View file

@ -146,8 +146,8 @@ class UnifiDeviceCommand:
"""Execute previously queued commands.""" """Execute previously queued commands."""
queue = self._command_queue.copy() queue = self._command_queue.copy()
self._command_queue.clear() self._command_queue.clear()
for device_id, device_commands in queue.items(): for dev_id, device_commands in queue.items():
device = self.api.devices[device_id] device = self.api.devices[dev_id]
commands = list(device_commands.items()) commands = list(device_commands.items())
await self.api.request( await self.api.request(
DeviceSetPoePortModeRequest.create(device, targets=commands) DeviceSetPoePortModeRequest.create(device, targets=commands)

View file

@ -89,9 +89,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
def check_entry_id(interface: str) -> str: def check_entry_id(interface: str) -> str:
for entry in hass.config_entries.async_entries(DOMAIN): for config_entry in hass.config_entries.async_entries(DOMAIN):
if "port" in entry.data and entry.data["port"] == interface: if "port" in config_entry.data and config_entry.data["port"] == interface:
return entry.entry_id return config_entry.entry_id
raise vol.Invalid( raise vol.Invalid(
"The interface provided is not defined as a port in a Velbus integration" "The interface provided is not defined as a port in a Velbus integration"
) )

View file

@ -642,8 +642,8 @@ class StorageCollectionWebsocket[_StorageCollectionT: StorageCollection]:
} }
for change in change_set for change in change_set
] ]
for connection, msg_id in self._subscribers: for conn, msg_id in self._subscribers:
connection.send_message(websocket_api.event_message(msg_id, json_msg)) conn.send_message(websocket_api.event_message(msg_id, json_msg))
if not self._subscribers: if not self._subscribers:
self._remove_subscription = ( self._remove_subscription = (

View file

@ -1653,12 +1653,12 @@ async def test_integration_multiple_entity_platforms_with_reload_and_restart(
unregister_binary_sensor_processor() unregister_binary_sensor_processor()
unregister_sensor_processor() unregister_sensor_processor()
async with async_test_home_assistant() as hass: async with async_test_home_assistant() as test_hass:
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(test_hass, DOMAIN, {DOMAIN: {}})
current_entry.set(entry) current_entry.set(entry)
coordinator = PassiveBluetoothProcessorCoordinator( coordinator = PassiveBluetoothProcessorCoordinator(
hass, test_hass,
_LOGGER, _LOGGER,
"aa:bb:cc:dd:ee:ff", "aa:bb:cc:dd:ee:ff",
BluetoothScanningMode.ACTIVE, BluetoothScanningMode.ACTIVE,
@ -1706,7 +1706,7 @@ async def test_integration_multiple_entity_platforms_with_reload_and_restart(
] ]
sensor_entity_one: PassiveBluetoothProcessorEntity = sensor_entities[0] sensor_entity_one: PassiveBluetoothProcessorEntity = sensor_entities[0]
sensor_entity_one.hass = hass sensor_entity_one.hass = test_hass
assert sensor_entity_one.available is False # service data not injected assert sensor_entity_one.available is False # service data not injected
assert sensor_entity_one.unique_id == "aa:bb:cc:dd:ee:ff-pressure" assert sensor_entity_one.unique_id == "aa:bb:cc:dd:ee:ff-pressure"
assert sensor_entity_one.device_info == { assert sensor_entity_one.device_info == {
@ -1723,7 +1723,7 @@ async def test_integration_multiple_entity_platforms_with_reload_and_restart(
binary_sensor_entity_one: PassiveBluetoothProcessorEntity = ( binary_sensor_entity_one: PassiveBluetoothProcessorEntity = (
binary_sensor_entities[0] binary_sensor_entities[0]
) )
binary_sensor_entity_one.hass = hass binary_sensor_entity_one.hass = test_hass
assert binary_sensor_entity_one.available is False # service data not injected assert binary_sensor_entity_one.available is False # service data not injected
assert binary_sensor_entity_one.unique_id == "aa:bb:cc:dd:ee:ff-motion" assert binary_sensor_entity_one.unique_id == "aa:bb:cc:dd:ee:ff-motion"
assert binary_sensor_entity_one.device_info == { assert binary_sensor_entity_one.device_info == {
@ -1739,7 +1739,7 @@ async def test_integration_multiple_entity_platforms_with_reload_and_restart(
cancel_coordinator() cancel_coordinator()
unregister_binary_sensor_processor() unregister_binary_sensor_processor()
unregister_sensor_processor() unregister_sensor_processor()
await hass.async_stop() await test_hass.async_stop()
NAMING_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate( NAMING_PASSIVE_BLUETOOTH_DATA_UPDATE = PassiveBluetoothDataUpdate(