Fix ruff manual-dict-comprehension PERF403 (#120723)

* Fix PERF403

* Fix

* Fix
pull/120749/head
Joost Lekkerkerker 2024-06-28 14:17:47 +02:00 committed by GitHub
parent c7906f90a3
commit 1fdd056c0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 234 additions and 223 deletions

View File

@ -363,15 +363,15 @@ class AuthManager:
local_only: bool | None = None,
) -> None:
"""Update a user."""
kwargs: dict[str, Any] = {}
for attr_name, value in (
("name", name),
("group_ids", group_ids),
("local_only", local_only),
):
if value is not None:
kwargs[attr_name] = value
kwargs: dict[str, Any] = {
attr_name: value
for attr_name, value in (
("name", name),
("group_ids", group_ids),
("local_only", local_only),
)
if value is not None
}
await self._store.async_update_user(user, **kwargs)
if is_active is not None:

View File

@ -105,14 +105,18 @@ class AuthStore:
"perm_lookup": self._perm_lookup,
}
for attr_name, value in (
("is_owner", is_owner),
("is_active", is_active),
("local_only", local_only),
("system_generated", system_generated),
):
if value is not None:
kwargs[attr_name] = value
kwargs.update(
{
attr_name: value
for attr_name, value in (
("is_owner", is_owner),
("is_active", is_active),
("local_only", local_only),
("system_generated", system_generated),
)
if value is not None
}
)
new_user = models.User(**kwargs)

View File

@ -304,21 +304,25 @@ async def async_update_pipeline(
updates.pop("id")
# Refactor this once we bump to Python 3.12
# and have https://peps.python.org/pep-0692/
for key, val in (
("conversation_engine", conversation_engine),
("conversation_language", conversation_language),
("language", language),
("name", name),
("stt_engine", stt_engine),
("stt_language", stt_language),
("tts_engine", tts_engine),
("tts_language", tts_language),
("tts_voice", tts_voice),
("wake_word_entity", wake_word_entity),
("wake_word_id", wake_word_id),
):
if val is not UNDEFINED:
updates[key] = val
updates.update(
{
key: val
for key, val in (
("conversation_engine", conversation_engine),
("conversation_language", conversation_language),
("language", language),
("name", name),
("stt_engine", stt_engine),
("stt_language", stt_language),
("tts_engine", tts_engine),
("tts_language", tts_language),
("tts_voice", tts_voice),
("wake_word_entity", wake_word_entity),
("wake_word_id", wake_word_id),
)
if val is not UNDEFINED
}
)
await pipeline_data.pipeline_store.async_update_item(pipeline.id, updates)

View File

@ -99,8 +99,7 @@ class Blueprint:
inputs = {}
for key, value in self.data[CONF_BLUEPRINT][CONF_INPUT].items():
if value and CONF_INPUT in value:
for key, value in value[CONF_INPUT].items():
inputs[key] = value
inputs.update(dict(value[CONF_INPUT]))
else:
inputs[key] = value
return inputs

View File

@ -180,24 +180,28 @@ class CloudPreferences:
"""Update user preferences."""
prefs = {**self._prefs}
for key, value in (
(PREF_ENABLE_GOOGLE, google_enabled),
(PREF_ENABLE_ALEXA, alexa_enabled),
(PREF_ENABLE_REMOTE, remote_enabled),
(PREF_GOOGLE_SECURE_DEVICES_PIN, google_secure_devices_pin),
(PREF_CLOUDHOOKS, cloudhooks),
(PREF_CLOUD_USER, cloud_user),
(PREF_ALEXA_REPORT_STATE, alexa_report_state),
(PREF_GOOGLE_REPORT_STATE, google_report_state),
(PREF_ALEXA_SETTINGS_VERSION, alexa_settings_version),
(PREF_GOOGLE_SETTINGS_VERSION, google_settings_version),
(PREF_TTS_DEFAULT_VOICE, tts_default_voice),
(PREF_REMOTE_DOMAIN, remote_domain),
(PREF_GOOGLE_CONNECTED, google_connected),
(PREF_REMOTE_ALLOW_REMOTE_ENABLE, remote_allow_remote_enable),
):
if value is not UNDEFINED:
prefs[key] = value
prefs.update(
{
key: value
for key, value in (
(PREF_ENABLE_GOOGLE, google_enabled),
(PREF_ENABLE_ALEXA, alexa_enabled),
(PREF_ENABLE_REMOTE, remote_enabled),
(PREF_GOOGLE_SECURE_DEVICES_PIN, google_secure_devices_pin),
(PREF_CLOUDHOOKS, cloudhooks),
(PREF_CLOUD_USER, cloud_user),
(PREF_ALEXA_REPORT_STATE, alexa_report_state),
(PREF_GOOGLE_REPORT_STATE, google_report_state),
(PREF_ALEXA_SETTINGS_VERSION, alexa_settings_version),
(PREF_GOOGLE_SETTINGS_VERSION, google_settings_version),
(PREF_TTS_DEFAULT_VOICE, tts_default_voice),
(PREF_REMOTE_DOMAIN, remote_domain),
(PREF_GOOGLE_CONNECTED, google_connected),
(PREF_REMOTE_ALLOW_REMOTE_ENABLE, remote_allow_remote_enable),
)
if value is not UNDEFINED
}
)
await self._save_prefs(prefs)

View File

@ -188,20 +188,20 @@ class GdacsEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DESCRIPTION, self._description),
(ATTR_EVENT_TYPE, self._event_type),
(ATTR_ALERT_LEVEL, self._alert_level),
(ATTR_COUNTRY, self._country),
(ATTR_DURATION_IN_WEEK, self._duration_in_week),
(ATTR_FROM_DATE, self._from_date),
(ATTR_TO_DATE, self._to_date),
(ATTR_POPULATION, self._population),
(ATTR_SEVERITY, self._severity),
(ATTR_VULNERABILITY, self._vulnerability),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DESCRIPTION, self._description),
(ATTR_EVENT_TYPE, self._event_type),
(ATTR_ALERT_LEVEL, self._alert_level),
(ATTR_COUNTRY, self._country),
(ATTR_DURATION_IN_WEEK, self._duration_in_week),
(ATTR_FROM_DATE, self._from_date),
(ATTR_TO_DATE, self._to_date),
(ATTR_POPULATION, self._population),
(ATTR_SEVERITY, self._severity),
(ATTR_VULNERABILITY, self._vulnerability),
)
if value or isinstance(value, bool)
}

View File

@ -133,16 +133,16 @@ class GdacsSensor(SensorEntity):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes: dict[str, Any] = {}
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._last_update_successful),
(ATTR_LAST_TIMESTAMP, self._last_timestamp),
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._last_update_successful),
(ATTR_LAST_TIMESTAMP, self._last_timestamp),
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
)
if value or isinstance(value, bool)
}

View File

@ -156,16 +156,16 @@ class GeonetnzQuakesEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DEPTH, self._depth),
(ATTR_LOCALITY, self._locality),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_MMI, self._mmi),
(ATTR_QUALITY, self._quality),
(ATTR_TIME, self._time),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DEPTH, self._depth),
(ATTR_LOCALITY, self._locality),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_MMI, self._mmi),
(ATTR_QUALITY, self._quality),
(ATTR_TIME, self._time),
)
if value or isinstance(value, bool)
}

View File

@ -137,16 +137,16 @@ class GeonetnzQuakesSensor(SensorEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._last_update_successful),
(ATTR_LAST_TIMESTAMP, self._last_timestamp),
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._last_update_successful),
(ATTR_LAST_TIMESTAMP, self._last_timestamp),
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
)
if value or isinstance(value, bool)
}

View File

@ -154,17 +154,17 @@ class GeonetnzVolcanoSensor(SensorEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_ACTIVITY, self._activity),
(ATTR_HAZARDS, self._hazards),
(ATTR_LONGITUDE, self._longitude),
(ATTR_LATITUDE, self._latitude),
(ATTR_DISTANCE, self._distance),
(ATTR_LAST_UPDATE, self._feed_last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._feed_last_update_successful),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_ACTIVITY, self._activity),
(ATTR_HAZARDS, self._hazards),
(ATTR_LONGITUDE, self._longitude),
(ATTR_LATITUDE, self._latitude),
(ATTR_DISTANCE, self._distance),
(ATTR_LAST_UPDATE, self._feed_last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._feed_last_update_successful),
)
if value or isinstance(value, bool)
}

View File

@ -262,9 +262,13 @@ async def handle_devices_execute(
),
EXECUTE_LIMIT,
)
for entity_id, result in zip(executions, execute_results, strict=False):
if result is not None:
results[entity_id] = result
results.update(
{
entity_id: result
for entity_id, result in zip(executions, execute_results, strict=False)
if result is not None
}
)
except TimeoutError:
pass

View File

@ -426,10 +426,7 @@ class HTML5NotificationService(BaseNotificationService):
@property
def targets(self):
"""Return a dictionary of registered targets."""
targets = {}
for registration in self.registrations:
targets[registration] = registration
return targets
return {registration: registration for registration in self.registrations}
def dismiss(self, **kwargs):
"""Dismisses a notification."""

View File

@ -224,15 +224,15 @@ class IgnSismologiaLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_TITLE, self._title),
(ATTR_REGION, self._region),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_IMAGE_URL, self._image_url),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_TITLE, self._title),
(ATTR_REGION, self._region),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_IMAGE_URL, self._image_url),
)
if value or isinstance(value, bool)
}

View File

@ -106,18 +106,15 @@ class KaiterraAirQuality(AirQualityEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
data = {}
attributes = [
(ATTR_VOC, self.volatile_organic_compounds),
(ATTR_AQI_LEVEL, self.air_quality_index_level),
(ATTR_AQI_POLLUTANT, self.air_quality_index_pollutant),
]
for attr, value in attributes:
if value is not None:
data[attr] = value
return data
return {
attr: value
for attr, value in (
(ATTR_VOC, self.volatile_organic_compounds),
(ATTR_AQI_LEVEL, self.air_quality_index_level),
(ATTR_AQI_POLLUTANT, self.air_quality_index_pollutant),
)
if value is not None
}
async def async_added_to_hass(self):
"""Register callback."""

View File

@ -75,10 +75,12 @@ class KrakenOptionsFlowHandler(OptionsFlow):
tracked_asset_pairs = self.config_entry.options.get(
CONF_TRACKED_ASSET_PAIRS, []
)
for tracked_asset_pair in tracked_asset_pairs:
tradable_asset_pairs_for_multi_select[tracked_asset_pair] = (
tracked_asset_pair
)
tradable_asset_pairs_for_multi_select.update(
{
tracked_asset_pair: tracked_asset_pair
for tracked_asset_pair in tracked_asset_pairs
}
)
options = {
vol.Optional(

View File

@ -244,11 +244,7 @@ class MicrosoftFaceGroupEntity(Entity):
@property
def extra_state_attributes(self):
"""Return device specific state attributes."""
attr = {}
for name, p_id in self._api.store[self._id].items():
attr[name] = p_id
return attr
return dict(self._api.store[self._id])
class MicrosoftFace:

View File

@ -269,19 +269,19 @@ class NswRuralFireServiceLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
(ATTR_LOCATION, self._location),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_COUNCIL_AREA, self._council_area),
(ATTR_STATUS, self._status),
(ATTR_TYPE, self._type),
(ATTR_FIRE, self._fire),
(ATTR_SIZE, self._size),
(ATTR_RESPONSIBLE_AGENCY, self._responsible_agency),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
(ATTR_LOCATION, self._location),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_COUNCIL_AREA, self._council_area),
(ATTR_STATUS, self._status),
(ATTR_TYPE, self._type),
(ATTR_FIRE, self._fire),
(ATTR_SIZE, self._size),
(ATTR_RESPONSIBLE_AGENCY, self._responsible_agency),
)
if value or isinstance(value, bool)
}

View File

@ -223,14 +223,14 @@ class QldBushfireLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_UPDATED_DATE, self._updated_date),
(ATTR_STATUS, self._status),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_UPDATED_DATE, self._updated_date),
(ATTR_STATUS, self._status),
)
if value or isinstance(value, bool)
}

View File

@ -276,17 +276,17 @@ class UsgsEarthquakesEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_PLACE, self._place),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_TIME, self._time),
(ATTR_UPDATED, self._updated),
(ATTR_STATUS, self._status),
(ATTR_TYPE, self._type),
(ATTR_ALERT, self._alert),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_PLACE, self._place),
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_TIME, self._time),
(ATTR_UPDATED, self._updated),
(ATTR_STATUS, self._status),
(ATTR_TYPE, self._type),
(ATTR_ALERT, self._alert),
)
if value or isinstance(value, bool)
}

View File

@ -196,15 +196,19 @@ class ZWaveLock(ZWaveBaseEntity, LockEntity):
) -> None:
"""Set the lock configuration."""
params: dict[str, Any] = {"operation_type": operation_type}
for attr, val in (
("lock_timeout_configuration", lock_timeout),
("auto_relock_time", auto_relock_time),
("hold_and_release_time", hold_and_release_time),
("twist_assist", twist_assist),
("block_to_block", block_to_block),
):
if val is not None:
params[attr] = val
params.update(
{
attr: val
for attr, val in (
("lock_timeout_configuration", lock_timeout),
("auto_relock_time", auto_relock_time),
("hold_and_release_time", hold_and_release_time),
("twist_assist", twist_assist),
("block_to_block", block_to_block),
)
if val is not None
}
)
configuration = DoorLockCCConfigurationSetOptions(**params)
result = await set_configuration(
self.info.node.endpoints[self.info.primary_value.endpoint or 0],

View File

@ -315,17 +315,17 @@ class AreaRegistry(BaseRegistry[AreasRegistryStoreData]):
"""Update name of area."""
old = self.areas[area_id]
new_values = {}
for attr_name, value in (
("aliases", aliases),
("icon", icon),
("labels", labels),
("picture", picture),
("floor_id", floor_id),
):
if value is not UNDEFINED and value != getattr(old, attr_name):
new_values[attr_name] = value
new_values = {
attr_name: value
for attr_name, value in (
("aliases", aliases),
("icon", icon),
("labels", labels),
("picture", picture),
("floor_id", floor_id),
)
if value is not UNDEFINED and value != getattr(old, attr_name)
}
if name is not UNDEFINED and name != old.name:
new_values["name"] = name