From 99b349fa2c4c5a153e8dcc909f92dff0f7f6384f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 12 Jun 2024 16:44:29 +0200 Subject: [PATCH] Fix consider-using-dict-items warnings in tests (#119497) --- .../components/google_assistant/test_trait.py | 6 +-- tests/components/google_wifi/test_sensor.py | 42 +++++++++---------- tests/components/metoffice/test_weather.py | 4 +- .../components/netatmo/test_device_trigger.py | 6 +-- tests/components/nexia/test_binary_sensor.py | 4 +- tests/components/nexia/test_climate.py | 4 +- tests/components/nexia/test_number.py | 4 +- tests/components/nexia/test_scene.py | 6 +-- tests/components/nexia/test_sensor.py | 18 ++++---- tests/components/number/test_init.py | 7 +--- tests/components/switch_as_x/test_init.py | 19 ++++----- .../components/template/test_binary_sensor.py | 4 +- tests/components/template/test_sensor.py | 4 +- 13 files changed, 60 insertions(+), 68 deletions(-) diff --git a/tests/components/google_assistant/test_trait.py b/tests/components/google_assistant/test_trait.py index d91d12b7074..038b16d0cfc 100644 --- a/tests/components/google_assistant/test_trait.py +++ b/tests/components/google_assistant/test_trait.py @@ -3986,7 +3986,7 @@ async def test_sensorstate( ), } - for sensor_type in sensor_types: + for sensor_type, item in sensor_types.items(): assert helpers.get_google_type(sensor.DOMAIN, None) is not None assert trait.SensorStateTrait.supported(sensor.DOMAIN, None, sensor_type, None) @@ -4002,8 +4002,8 @@ async def test_sensorstate( BASIC_CONFIG, ) - name = sensor_types[sensor_type][0] - unit = sensor_types[sensor_type][1] + name = item[0] + unit = item[1] if sensor_type == sensor.SensorDeviceClass.AQI: assert trt.sync_attributes() == { diff --git a/tests/components/google_wifi/test_sensor.py b/tests/components/google_wifi/test_sensor.py index fcc5603fdc5..c7df2b4e822 100644 --- a/tests/components/google_wifi/test_sensor.py +++ b/tests/components/google_wifi/test_sensor.py @@ -94,8 +94,8 @@ def setup_api(hass, data, requests_mock): "units": desc.native_unit_of_measurement, "icon": desc.icon, } - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for value in sensor_dict.values(): + sensor = value["sensor"] sensor.hass = hass return api, sensor_dict @@ -111,9 +111,9 @@ def fake_delay(hass, ha_delay): def test_name(requests_mock: requests_mock.Mocker) -> None: """Test the name.""" api, sensor_dict = setup_api(None, MOCK_DATA, requests_mock) - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] - test_name = sensor_dict[name]["name"] + for value in sensor_dict.values(): + sensor = value["sensor"] + test_name = value["name"] assert test_name == sensor.name @@ -122,17 +122,17 @@ def test_unit_of_measurement( ) -> None: """Test the unit of measurement.""" api, sensor_dict = setup_api(hass, MOCK_DATA, requests_mock) - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] - assert sensor_dict[name]["units"] == sensor.unit_of_measurement + for value in sensor_dict.values(): + sensor = value["sensor"] + assert value["units"] == sensor.unit_of_measurement def test_icon(requests_mock: requests_mock.Mocker) -> None: """Test the icon.""" api, sensor_dict = setup_api(None, MOCK_DATA, requests_mock) - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] - assert sensor_dict[name]["icon"] == sensor.icon + for value in sensor_dict.values(): + sensor = value["sensor"] + assert value["icon"] == sensor.icon def test_state(hass: HomeAssistant, requests_mock: requests_mock.Mocker) -> None: @@ -140,8 +140,8 @@ def test_state(hass: HomeAssistant, requests_mock: requests_mock.Mocker) -> None api, sensor_dict = setup_api(hass, MOCK_DATA, requests_mock) now = datetime(1970, month=1, day=1) with patch("homeassistant.util.dt.now", return_value=now): - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for name, value in sensor_dict.items(): + sensor = value["sensor"] fake_delay(hass, 2) sensor.update() if name == google_wifi.ATTR_LAST_RESTART: @@ -159,8 +159,8 @@ def test_update_when_value_is_none( ) -> None: """Test state gets updated to unknown when sensor returns no data.""" api, sensor_dict = setup_api(hass, None, requests_mock) - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for value in sensor_dict.values(): + sensor = value["sensor"] fake_delay(hass, 2) sensor.update() assert sensor.state is None @@ -173,8 +173,8 @@ def test_update_when_value_changed( api, sensor_dict = setup_api(hass, MOCK_DATA_NEXT, requests_mock) now = datetime(1970, month=1, day=1) with patch("homeassistant.util.dt.now", return_value=now): - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for name, value in sensor_dict.items(): + sensor = value["sensor"] fake_delay(hass, 2) sensor.update() if name == google_wifi.ATTR_LAST_RESTART: @@ -198,8 +198,8 @@ def test_when_api_data_missing( api, sensor_dict = setup_api(hass, MOCK_DATA_MISSING, requests_mock) now = datetime(1970, month=1, day=1) with patch("homeassistant.util.dt.now", return_value=now): - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for value in sensor_dict.values(): + sensor = value["sensor"] fake_delay(hass, 2) sensor.update() assert sensor.state is None @@ -214,8 +214,8 @@ def test_update_when_unavailable( "google_wifi.GoogleWifiAPI.update", side_effect=update_side_effect(hass, requests_mock), ) - for name in sensor_dict: - sensor = sensor_dict[name]["sensor"] + for value in sensor_dict.values(): + sensor = value["sensor"] sensor.update() assert sensor.state is None diff --git a/tests/components/metoffice/test_weather.py b/tests/components/metoffice/test_weather.py index 64e6ef65ec2..c931222d1d6 100644 --- a/tests/components/metoffice/test_weather.py +++ b/tests/components/metoffice/test_weather.py @@ -94,8 +94,8 @@ async def test_site_cannot_connect( assert hass.states.get("weather.met_office_wavertree_3hourly") is None assert hass.states.get("weather.met_office_wavertree_daily") is None - for sensor_id in WAVERTREE_SENSOR_RESULTS: - sensor_name, _ = WAVERTREE_SENSOR_RESULTS[sensor_id] + for sensor in WAVERTREE_SENSOR_RESULTS.values(): + sensor_name = sensor[0] sensor = hass.states.get(f"sensor.wavertree_{sensor_name}") assert sensor is None diff --git a/tests/components/netatmo/test_device_trigger.py b/tests/components/netatmo/test_device_trigger.py index fac3cedff75..ad1e9bd8cb9 100644 --- a/tests/components/netatmo/test_device_trigger.py +++ b/tests/components/netatmo/test_device_trigger.py @@ -266,13 +266,11 @@ async def test_if_fires_on_event_legacy( ("platform", "camera_type", "event_type", "sub_type"), [ ("climate", "Smart Valve", trigger, subtype) - for trigger in SUBTYPES - for subtype in SUBTYPES[trigger] + for trigger, subtype in SUBTYPES.items() ] + [ ("climate", "Smart Thermostat", trigger, subtype) - for trigger in SUBTYPES - for subtype in SUBTYPES[trigger] + for trigger, subtype in SUBTYPES.items() ], ) async def test_if_fires_on_event_with_subtype( diff --git a/tests/components/nexia/test_binary_sensor.py b/tests/components/nexia/test_binary_sensor.py index e175afe6214..0abb709f6aa 100644 --- a/tests/components/nexia/test_binary_sensor.py +++ b/tests/components/nexia/test_binary_sensor.py @@ -20,7 +20,7 @@ async def test_create_binary_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("binary_sensor.downstairs_east_wing_blower_active") @@ -32,5 +32,5 @@ async def test_create_binary_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) diff --git a/tests/components/nexia/test_climate.py b/tests/components/nexia/test_climate.py index 900838547f2..1d248e5ec5f 100644 --- a/tests/components/nexia/test_climate.py +++ b/tests/components/nexia/test_climate.py @@ -39,7 +39,7 @@ async def test_climate_zones(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("climate.kitchen") @@ -72,5 +72,5 @@ async def test_climate_zones(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) diff --git a/tests/components/nexia/test_number.py b/tests/components/nexia/test_number.py index 7f4c5f92ab6..ee621912807 100644 --- a/tests/components/nexia/test_number.py +++ b/tests/components/nexia/test_number.py @@ -26,7 +26,7 @@ async def test_create_fan_speed_number_entities(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("number.downstairs_east_wing_fan_speed") @@ -40,7 +40,7 @@ async def test_create_fan_speed_number_entities(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) diff --git a/tests/components/nexia/test_scene.py b/tests/components/nexia/test_scene.py index 20f214fff27..5d9ae30c7e1 100644 --- a/tests/components/nexia/test_scene.py +++ b/tests/components/nexia/test_scene.py @@ -35,7 +35,7 @@ async def test_automation_scenes(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("scene.power_outage") @@ -55,7 +55,7 @@ async def test_automation_scenes(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("scene.power_restored") @@ -73,5 +73,5 @@ async def test_automation_scenes(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) diff --git a/tests/components/nexia/test_sensor.py b/tests/components/nexia/test_sensor.py index 1f595da43d1..ec9ed256617 100644 --- a/tests/components/nexia/test_sensor.py +++ b/tests/components/nexia/test_sensor.py @@ -23,7 +23,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.nick_office_zone_setpoint_status") @@ -35,7 +35,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.nick_office_zone_status") @@ -48,7 +48,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_air_cleaner_mode") @@ -61,7 +61,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_current_compressor_speed") @@ -75,7 +75,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_outdoor_temperature") @@ -90,7 +90,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_humidity") @@ -105,7 +105,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_requested_compressor_speed") @@ -119,7 +119,7 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) state = hass.states.get("sensor.master_suite_system_status") @@ -132,5 +132,5 @@ async def test_create_sensors(hass: HomeAssistant) -> None: # Only test for a subset of attributes in case # HA changes the implementation and a new one appears assert all( - state.attributes[key] == expected_attributes[key] for key in expected_attributes + state.attributes[key] == value for key, value in expected_attributes.items() ) diff --git a/tests/components/number/test_init.py b/tests/components/number/test_init.py index dbdbab31d63..6f74a3126c0 100644 --- a/tests/components/number/test_init.py +++ b/tests/components/number/test_init.py @@ -846,13 +846,10 @@ def test_device_classes_aligned() -> None: assert hasattr(NumberDeviceClass, device_class.name) assert getattr(NumberDeviceClass, device_class.name).value == device_class.value - for device_class in SENSOR_DEVICE_CLASS_UNITS: + for device_class, unit in SENSOR_DEVICE_CLASS_UNITS.items(): if device_class in NON_NUMERIC_DEVICE_CLASSES: continue - assert ( - SENSOR_DEVICE_CLASS_UNITS[device_class] - == NUMBER_DEVICE_CLASS_UNITS[device_class] - ) + assert unit == NUMBER_DEVICE_CLASS_UNITS[device_class] class MockFlow(ConfigFlow): diff --git a/tests/components/switch_as_x/test_init.py b/tests/components/switch_as_x/test_init.py index b1ebbbb9322..3889a43f741 100644 --- a/tests/components/switch_as_x/test_init.py +++ b/tests/components/switch_as_x/test_init.py @@ -782,8 +782,8 @@ async def test_import_expose_settings_1( expose_settings = exposed_entities.async_get_entity_settings( hass, entity_entry.entity_id ) - for assistant in EXPOSE_SETTINGS: - assert expose_settings[assistant]["should_expose"] == EXPOSE_SETTINGS[assistant] + for assistant, settings in EXPOSE_SETTINGS.items(): + assert expose_settings[assistant]["should_expose"] == settings # Check the switch is no longer exposed expose_settings = exposed_entities.async_get_entity_settings( @@ -856,18 +856,15 @@ async def test_import_expose_settings_2( expose_settings = exposed_entities.async_get_entity_settings( hass, entity_entry.entity_id ) - for assistant in EXPOSE_SETTINGS: - assert ( - expose_settings[assistant]["should_expose"] - is not EXPOSE_SETTINGS[assistant] - ) + for assistant, settings in EXPOSE_SETTINGS.items(): + assert expose_settings[assistant]["should_expose"] is not settings # Check the switch settings were not modified expose_settings = exposed_entities.async_get_entity_settings( hass, switch_entity_entry.entity_id ) - for assistant in EXPOSE_SETTINGS: - assert expose_settings[assistant]["should_expose"] == EXPOSE_SETTINGS[assistant] + for assistant, settings in EXPOSE_SETTINGS.items(): + assert expose_settings[assistant]["should_expose"] == settings @pytest.mark.parametrize("target_domain", PLATFORMS_TO_TEST) @@ -922,8 +919,8 @@ async def test_restore_expose_settings( expose_settings = exposed_entities.async_get_entity_settings( hass, switch_entity_entry.entity_id ) - for assistant in EXPOSE_SETTINGS: - assert expose_settings[assistant]["should_expose"] == EXPOSE_SETTINGS[assistant] + for assistant, settings in EXPOSE_SETTINGS.items(): + assert expose_settings[assistant]["should_expose"] == settings @pytest.mark.parametrize("target_domain", PLATFORMS_TO_TEST) diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index 452f926dca5..63d9b338eaa 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -1273,9 +1273,9 @@ async def test_trigger_entity_restore_state( state = hass.states.get("binary_sensor.test") assert state.state == initial_state - for attr in restored_attributes: + for attr, value in restored_attributes.items(): if attr in initial_attributes: - assert state.attributes[attr] == restored_attributes[attr] + assert state.attributes[attr] == value else: assert attr not in state.attributes assert "another" not in state.attributes diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index fdcc0587a73..54e53f5257e 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -1828,9 +1828,9 @@ async def test_trigger_entity_restore_state( state = hass.states.get("sensor.test") assert state.state == initial_state - for attr in restored_attributes: + for attr, value in restored_attributes.items(): if attr in initial_attributes: - assert state.attributes[attr] == restored_attributes[attr] + assert state.attributes[attr] == value else: assert attr not in state.attributes assert "another" not in state.attributes