diff --git a/homeassistant/components/deconz/device_trigger.py b/homeassistant/components/deconz/device_trigger.py index 70ef46bafc6..a22c8cb9491 100644 --- a/homeassistant/components/deconz/device_trigger.py +++ b/homeassistant/components/deconz/device_trigger.py @@ -738,7 +738,7 @@ async def async_get_triggers( return [] triggers = [] - for trigger, subtype in REMOTES[device.model].keys(): + for trigger, subtype in REMOTES[device.model]: triggers.append( { CONF_DEVICE_ID: device_id, diff --git a/homeassistant/components/fritzbox/button.py b/homeassistant/components/fritzbox/button.py index 00e8ad5e4d6..b9f94e24dc0 100644 --- a/homeassistant/components/fritzbox/button.py +++ b/homeassistant/components/fritzbox/button.py @@ -20,10 +20,7 @@ async def async_setup_entry( ][CONF_COORDINATOR] async_add_entities( - [ - FritzBoxTemplate(coordinator, ain) - for ain in coordinator.data.templates.keys() - ] + [FritzBoxTemplate(coordinator, ain) for ain in coordinator.data.templates] ) diff --git a/homeassistant/components/google_translate/tts.py b/homeassistant/components/google_translate/tts.py index c698a16baae..b720498b4f1 100644 --- a/homeassistant/components/google_translate/tts.py +++ b/homeassistant/components/google_translate/tts.py @@ -65,7 +65,7 @@ class GoogleProvider(Provider): if language in MAP_LANG_TLD: tld = MAP_LANG_TLD[language].tld language = MAP_LANG_TLD[language].lang - if options is not None and "tld" in options.keys(): + if options is not None and "tld" in options: tld = options["tld"] tts = gTTS(text=message, lang=language, tld=tld) mp3_data = BytesIO() diff --git a/homeassistant/components/gtfs/sensor.py b/homeassistant/components/gtfs/sensor.py index 2bce3cb21c1..3a79d8d88a9 100644 --- a/homeassistant/components/gtfs/sensor.py +++ b/homeassistant/components/gtfs/sensor.py @@ -800,9 +800,7 @@ class GTFSDepartureSensor(SensorEntity): @staticmethod def dict_for_table(resource: Any) -> dict: """Return a dictionary for the SQLAlchemy resource given.""" - return { - col: getattr(resource, col) for col in resource.__table__.columns.keys() - } + return {col: getattr(resource, col) for col in resource.__table__.columns} def append_keys(self, resource: dict, prefix: str | None = None) -> None: """Properly format key val pairs to append to attributes.""" diff --git a/homeassistant/components/homematic/climate.py b/homeassistant/components/homematic/climate.py index 998113f1bcf..c1dead1835e 100644 --- a/homeassistant/components/homematic/climate.py +++ b/homeassistant/components/homematic/climate.py @@ -193,5 +193,5 @@ class HMThermostat(HMDevice, ClimateEntity): ): self._data[HM_CONTROL_MODE] = None - for node in self._hmdevice.SENSORNODE.keys(): + for node in self._hmdevice.SENSORNODE: self._data[node] = None diff --git a/homeassistant/components/kraken/config_flow.py b/homeassistant/components/kraken/config_flow.py index b821f7a5920..ff4c882504c 100644 --- a/homeassistant/components/kraken/config_flow.py +++ b/homeassistant/components/kraken/config_flow.py @@ -63,9 +63,7 @@ class KrakenOptionsFlowHandler(config_entries.OptionsFlow): tradable_asset_pairs = await self.hass.async_add_executor_job( get_tradable_asset_pairs, api ) - tradable_asset_pairs_for_multi_select = { - v: v for v in tradable_asset_pairs.keys() - } + tradable_asset_pairs_for_multi_select = {v: v for v in tradable_asset_pairs} options = { vol.Optional( CONF_SCAN_INTERVAL, diff --git a/homeassistant/components/soma/cover.py b/homeassistant/components/soma/cover.py index ecb004083df..144c793ac57 100644 --- a/homeassistant/components/soma/cover.py +++ b/homeassistant/components/soma/cover.py @@ -109,7 +109,7 @@ class SomaTilt(SomaEntity, CoverEntity): api_position = int(response["position"]) - if "closed_upwards" in response.keys(): + if "closed_upwards" in response: self.current_position = 50 + ((api_position * 50) / 100) else: self.current_position = 50 - ((api_position * 50) / 100) diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 38aa6a4706e..0d253d7d94f 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -389,9 +389,7 @@ class SpeechManager: if options is not None: supported_options = provider.supported_options or [] invalid_opts = [ - opt_name - for opt_name in options.keys() - if opt_name not in supported_options + opt_name for opt_name in options if opt_name not in supported_options ] if invalid_opts: raise HomeAssistantError(f"Invalid options found: {invalid_opts}") diff --git a/homeassistant/components/ubus/device_tracker.py b/homeassistant/components/ubus/device_tracker.py index 1c16d997d4b..20b0ad6593d 100644 --- a/homeassistant/components/ubus/device_tracker.py +++ b/homeassistant/components/ubus/device_tracker.py @@ -130,7 +130,7 @@ class UbusDeviceScanner(DeviceScanner): 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(): + for key in result["clients"]: device = result["clients"][key] if device["authorized"]: self.last_results.append(key) diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index 281ab3108b6..c9acdf0d712 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -43,4 +43,4 @@ def extract_domain_configs(config: ConfigType, domain: str) -> Sequence[str]: Async friendly. """ pattern = re.compile(rf"^{domain}(| .+)$") - return [key for key in config.keys() if pattern.match(key)] + return [key for key in config if pattern.match(key)] diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index 4cf689e4767..21a54d64728 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -123,7 +123,7 @@ async def async_check_ha_config_file( # noqa: C901 core_config.pop(CONF_PACKAGES, None) # Filter out repeating config sections - components = {key.partition(" ")[0] for key in config.keys()} + components = {key.partition(" ")[0] for key in config} # Process and validate config for domain in components: diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 5d8f7d8a8fd..c13b703cae6 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -537,7 +537,7 @@ def schema_with_slug_keys( if not isinstance(value, dict): raise vol.Invalid("expected dictionary") - for key in value.keys(): + for key in value: slug_validator(key) return cast(dict, schema(value)) diff --git a/homeassistant/helpers/schema_config_entry_flow.py b/homeassistant/helpers/schema_config_entry_flow.py index c4a274aa6bc..9f76a639e0f 100644 --- a/homeassistant/helpers/schema_config_entry_flow.py +++ b/homeassistant/helpers/schema_config_entry_flow.py @@ -149,7 +149,7 @@ class SchemaCommonFlowHandler: and not self._handler.show_advanced_options ): # Add advanced field default if not set - for key in data_schema.schema.keys(): + for key in data_schema.schema: if isinstance(key, (vol.Optional, vol.Required)): if ( key.description diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index b1e1dc067c6..4eb703c0072 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -166,7 +166,7 @@ def is_complex(value: Any) -> bool: if isinstance(value, list): return any(is_complex(val) for val in value) if isinstance(value, collections.abc.Mapping): - return any(is_complex(val) for val in value.keys()) or any( + return any(is_complex(val) for val in value) or any( is_complex(val) for val in value.values() ) return False diff --git a/pyproject.toml b/pyproject.toml index 334e7956606..d12f0caa3c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,6 +249,7 @@ select = [ "PLC0414", # Useless import alias. Import alias does not rename original package. "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass "SIM117", # Merge with-statements that use the same scope + "SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys() "SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'. "SIM401", # Use get from dict with default instead of an if block "T20", # flake8-print diff --git a/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py b/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py index 7d2be8a6d82..db52d78dd29 100644 --- a/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py +++ b/script/scaffold/templates/config_flow_helper/tests/test_config_flow.py @@ -52,7 +52,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/cast/test_config_flow.py b/tests/components/cast/test_config_flow.py index 8da1baac840..c1ecedcf551 100644 --- a/tests/components/cast/test_config_flow.py +++ b/tests/components/cast/test_config_flow.py @@ -140,7 +140,7 @@ async def test_zeroconf_setup_onboarding(hass: HomeAssistant) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/clicksend_tts/test_notify.py b/tests/components/clicksend_tts/test_notify.py index e9933f25e6f..ae814aa1332 100644 --- a/tests/components/clicksend_tts/test_notify.py +++ b/tests/components/clicksend_tts/test_notify.py @@ -111,7 +111,7 @@ async def test_send_simple_message(hass: HomeAssistant) -> None: expected_content_type = "application/json" assert ( - "Content-Type" in mock.last_request.headers.keys() + "Content-Type" in mock.last_request.headers and mock.last_request.headers["Content-Type"] == expected_content_type ) diff --git a/tests/components/derivative/test_config_flow.py b/tests/components/derivative/test_config_flow.py index b6fa62e290f..5be99026c77 100644 --- a/tests/components/derivative/test_config_flow.py +++ b/tests/components/derivative/test_config_flow.py @@ -67,7 +67,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/group/test_config_flow.py b/tests/components/group/test_config_flow.py index 7fbd22b2dc9..4526bca7768 100644 --- a/tests/components/group/test_config_flow.py +++ b/tests/components/group/test_config_flow.py @@ -170,7 +170,7 @@ async def test_config_flow_hides_members( def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/group/test_light.py b/tests/components/group/test_light.py index c2d94c696a5..f50d4486b39 100644 --- a/tests/components/group/test_light.py +++ b/tests/components/group/test_light.py @@ -805,12 +805,12 @@ async def test_emulated_color_temp_group( state = hass.states.get("light.test1") assert state.state == STATE_ON assert state.attributes[ATTR_COLOR_TEMP] == 200 - assert ATTR_HS_COLOR in state.attributes.keys() + assert ATTR_HS_COLOR in state.attributes state = hass.states.get("light.test2") assert state.state == STATE_ON assert state.attributes[ATTR_COLOR_TEMP] == 200 - assert ATTR_HS_COLOR in state.attributes.keys() + assert ATTR_HS_COLOR in state.attributes state = hass.states.get("light.test3") assert state.state == STATE_ON diff --git a/tests/components/harmony/test_subscriber.py b/tests/components/harmony/test_subscriber.py index a1b78e5565e..bdc57385852 100644 --- a/tests/components/harmony/test_subscriber.py +++ b/tests/components/harmony/test_subscriber.py @@ -52,11 +52,11 @@ async def test_async_callbacks(hass: HomeAssistant) -> None: _call_all_callbacks(subscriber) await hass.async_block_till_done() - for callback_name in _NO_PARAM_CALLBACKS.keys(): + for callback_name in _NO_PARAM_CALLBACKS: callback_mock = callbacks[callback_name] callback_mock.assert_awaited_once() - for callback_name in _ACTIVITY_CALLBACKS.keys(): + for callback_name in _ACTIVITY_CALLBACKS: callback_mock = callbacks[callback_name] callback_mock.assert_awaited_once_with(_ACTIVITY_TUPLE) @@ -96,11 +96,11 @@ async def test_callbacks(hass: HomeAssistant) -> None: _call_all_callbacks(subscriber) await hass.async_block_till_done() - for callback_name in _NO_PARAM_CALLBACKS.keys(): + for callback_name in _NO_PARAM_CALLBACKS: callback_mock = callbacks[callback_name] callback_mock.assert_called_once() - for callback_name in _ACTIVITY_CALLBACKS.keys(): + for callback_name in _ACTIVITY_CALLBACKS: callback_mock = callbacks[callback_name] callback_mock.assert_called_once_with(_ACTIVITY_TUPLE) @@ -122,12 +122,12 @@ async def test_subscribe_unsubscribe(hass: HomeAssistant) -> None: _call_all_callbacks(subscriber) await hass.async_block_till_done() - for callback_name in _NO_PARAM_CALLBACKS.keys(): + for callback_name in _NO_PARAM_CALLBACKS: callback_one[callback_name].assert_not_called() callback_two[callback_name].assert_called_once() callback_three[callback_name].assert_not_called() - for callback_name in _ACTIVITY_CALLBACKS.keys(): + for callback_name in _ACTIVITY_CALLBACKS: callback_one[callback_name].assert_not_called() callback_two[callback_name].assert_called_once_with(_ACTIVITY_TUPLE) callback_three[callback_name].assert_not_called() diff --git a/tests/components/integration/test_config_flow.py b/tests/components/integration/test_config_flow.py index 9ad15096ad3..aedfbc6e8bc 100644 --- a/tests/components/integration/test_config_flow.py +++ b/tests/components/integration/test_config_flow.py @@ -67,7 +67,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/min_max/test_config_flow.py b/tests/components/min_max/test_config_flow.py index 2503eefb1b0..f180f07b657 100644 --- a/tests/components/min_max/test_config_flow.py +++ b/tests/components/min_max/test_config_flow.py @@ -56,7 +56,7 @@ async def test_config_flow(hass: HomeAssistant, platform: str) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/modbus/conftest.py b/tests/components/modbus/conftest.py index 377be04f250..21c5f4ddb25 100644 --- a/tests/components/modbus/conftest.py +++ b/tests/components/modbus/conftest.py @@ -82,7 +82,7 @@ async def mock_modbus_fixture( ): """Load integration modbus using mocked pymodbus.""" conf = copy.deepcopy(do_config) - for key in conf.keys(): + for key in conf: if config_addon: conf[key][0].update(config_addon) for entity in conf[key]: diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index ee4e8afae23..7de60cbc8b1 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -843,7 +843,7 @@ async def test_invalid_discovery_prefix( def get_default(schema: vol.Schema, key: str) -> Any: """Get default value for key in voluptuous schema.""" - for schema_key in schema.keys(): + for schema_key in schema: if schema_key == key: if schema_key.default == vol.UNDEFINED: return None @@ -852,7 +852,7 @@ def get_default(schema: vol.Schema, key: str) -> Any: def get_suggested(schema: vol.Schema, key: str) -> Any: """Get suggested value for key in voluptuous schema.""" - for schema_key in schema.keys(): + for schema_key in schema: if schema_key == key: if ( schema_key.description is None diff --git a/tests/components/smartthings/test_binary_sensor.py b/tests/components/smartthings/test_binary_sensor.py index cf8811d801a..49c63b77b02 100644 --- a/tests/components/smartthings/test_binary_sensor.py +++ b/tests/components/smartthings/test_binary_sensor.py @@ -26,7 +26,7 @@ async def test_mapping_integrity() -> None: for capability, attrib in binary_sensor.CAPABILITY_TO_ATTRIB.items(): assert capability in CAPABILITIES, capability assert attrib in ATTRIBUTES, attrib - assert attrib in binary_sensor.ATTRIB_TO_CLASS.keys(), attrib + assert attrib in binary_sensor.ATTRIB_TO_CLASS, attrib # Ensure every ATTRIB_TO_CLASS value is in DEVICE_CLASSES for attrib, device_class in binary_sensor.ATTRIB_TO_CLASS.items(): assert attrib in ATTRIBUTES, attrib diff --git a/tests/components/threshold/test_config_flow.py b/tests/components/threshold/test_config_flow.py index 3d0ff53a4a9..631b4252c13 100644 --- a/tests/components/threshold/test_config_flow.py +++ b/tests/components/threshold/test_config_flow.py @@ -86,7 +86,7 @@ async def test_fail(hass: HomeAssistant, extra_input_data, error) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/tod/test_config_flow.py b/tests/components/tod/test_config_flow.py index e215a4f1ae9..4d0e2a06190 100644 --- a/tests/components/tod/test_config_flow.py +++ b/tests/components/tod/test_config_flow.py @@ -57,7 +57,7 @@ async def test_config_flow(hass: HomeAssistant, platform) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None diff --git a/tests/components/utility_meter/test_config_flow.py b/tests/components/utility_meter/test_config_flow.py index 29a8cab9677..8deb7601aa6 100644 --- a/tests/components/utility_meter/test_config_flow.py +++ b/tests/components/utility_meter/test_config_flow.py @@ -138,7 +138,7 @@ async def test_tariffs(hass: HomeAssistant) -> None: def get_suggested(schema, key): """Get suggested value for key in voluptuous schema.""" - for k in schema.keys(): + for k in schema: if k == key: if k.description is None or "suggested_value" not in k.description: return None