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

* Fix PERF403

* Fix

* Fix
This commit is contained in:
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, local_only: bool | None = None,
) -> None: ) -> None:
"""Update a user.""" """Update a user."""
kwargs: dict[str, Any] = {} kwargs: dict[str, Any] = {
attr_name: value
for attr_name, value in ( for attr_name, value in (
("name", name), ("name", name),
("group_ids", group_ids), ("group_ids", group_ids),
("local_only", local_only), ("local_only", local_only),
): )
if value is not None: if value is not None
kwargs[attr_name] = value }
await self._store.async_update_user(user, **kwargs) await self._store.async_update_user(user, **kwargs)
if is_active is not None: if is_active is not None:

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -196,15 +196,19 @@ class ZWaveLock(ZWaveBaseEntity, LockEntity):
) -> None: ) -> None:
"""Set the lock configuration.""" """Set the lock configuration."""
params: dict[str, Any] = {"operation_type": operation_type} params: dict[str, Any] = {"operation_type": operation_type}
for attr, val in ( params.update(
("lock_timeout_configuration", lock_timeout), {
("auto_relock_time", auto_relock_time), attr: val
("hold_and_release_time", hold_and_release_time), for attr, val in (
("twist_assist", twist_assist), ("lock_timeout_configuration", lock_timeout),
("block_to_block", block_to_block), ("auto_relock_time", auto_relock_time),
): ("hold_and_release_time", hold_and_release_time),
if val is not None: ("twist_assist", twist_assist),
params[attr] = val ("block_to_block", block_to_block),
)
if val is not None
}
)
configuration = DoorLockCCConfigurationSetOptions(**params) configuration = DoorLockCCConfigurationSetOptions(**params)
result = await set_configuration( result = await set_configuration(
self.info.node.endpoints[self.info.primary_value.endpoint or 0], 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.""" """Update name of area."""
old = self.areas[area_id] old = self.areas[area_id]
new_values = {} new_values = {
attr_name: value
for attr_name, value in ( for attr_name, value in (
("aliases", aliases), ("aliases", aliases),
("icon", icon), ("icon", icon),
("labels", labels), ("labels", labels),
("picture", picture), ("picture", picture),
("floor_id", floor_id), ("floor_id", floor_id),
): )
if value is not UNDEFINED and value != getattr(old, attr_name): if value is not UNDEFINED and value != getattr(old, attr_name)
new_values[attr_name] = value }
if name is not UNDEFINED and name != old.name: if name is not UNDEFINED and name != old.name:
new_values["name"] = name new_values["name"] = name