From e6a692f354866dbffa35daa0c36d224fa7c4917e Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 13 Mar 2024 21:51:38 +0100 Subject: [PATCH] Improve lists in integrations [N-O] (#113231) --- homeassistant/components/nam/sensor.py | 11 +++-- homeassistant/components/neato/camera.py | 9 +++-- homeassistant/components/neato/sensor.py | 4 +- homeassistant/components/neato/switch.py | 10 ++--- homeassistant/components/neato/vacuum.py | 7 ++-- .../nederlandse_spoorwegen/sensor.py | 3 +- homeassistant/components/nest/camera.py | 14 +++---- homeassistant/components/nest/climate.py | 25 ++++++------ homeassistant/components/nest/config_flow.py | 9 +++-- homeassistant/components/nest/media_source.py | 7 ++-- .../components/netatmo/device_trigger.py | 24 +++++------ homeassistant/components/netgear/sensor.py | 40 ++++++------------- homeassistant/components/netgear/switch.py | 11 ++--- homeassistant/components/network/__init__.py | 6 +-- .../components/nextdns/binary_sensor.py | 8 ++-- homeassistant/components/nextdns/switch.py | 8 ++-- .../components/nina/binary_sensor.py | 14 +++---- homeassistant/components/obihai/sensor.py | 16 ++++---- homeassistant/components/octoprint/sensor.py | 18 ++++----- .../openalpr_cloud/image_processing.py | 13 +++--- .../components/opencv/image_processing.py | 19 ++++----- .../components/opentherm_gw/binary_sensor.py | 28 +++++-------- .../components/opentherm_gw/sensor.py | 34 ++++++---------- homeassistant/components/opower/sensor.py | 18 ++++----- .../components/osoenergy/water_heater.py | 8 ++-- .../components/overkiz/alarm_control_panel.py | 26 ++++++------ .../components/overkiz/binary_sensor.py | 18 ++++----- homeassistant/components/overkiz/button.py | 18 ++++----- homeassistant/components/overkiz/number.py | 18 ++++----- homeassistant/components/overkiz/select.py | 18 ++++----- homeassistant/components/overkiz/sensor.py | 18 ++++----- homeassistant/components/overkiz/switch.py | 26 ++++++------ .../components/owntracks/__init__.py | 24 +++++------ .../components/netatmo/test_device_trigger.py | 24 +++++------ tests/components/onewire/__init__.py | 10 +++-- 35 files changed, 254 insertions(+), 310 deletions(-) diff --git a/homeassistant/components/nam/sensor.py b/homeassistant/components/nam/sensor.py index 848c3396668..a098f48e434 100644 --- a/homeassistant/components/nam/sensor.py +++ b/homeassistant/components/nam/sensor.py @@ -367,12 +367,11 @@ async def async_setup_entry( ) ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id) - sensors: list[NAMSensor] = [] - for description in SENSORS: - if getattr(coordinator.data, description.key) is not None: - sensors.append(NAMSensor(coordinator, description)) - - async_add_entities(sensors, False) + async_add_entities( + NAMSensor(coordinator, description) + for description in SENSORS + if getattr(coordinator.data, description.key) is not None + ) class NAMSensor(CoordinatorEntity[NAMDataUpdateCoordinator], SensorEntity): diff --git a/homeassistant/components/neato/camera.py b/homeassistant/components/neato/camera.py index 7aeadb0698d..e4d5f81f33a 100644 --- a/homeassistant/components/neato/camera.py +++ b/homeassistant/components/neato/camera.py @@ -29,12 +29,13 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up Neato camera with config entry.""" - dev = [] neato: NeatoHub = hass.data[NEATO_LOGIN] mapdata: dict[str, Any] | None = hass.data.get(NEATO_MAP_DATA) - for robot in hass.data[NEATO_ROBOTS]: - if "maps" in robot.traits: - dev.append(NeatoCleaningMap(neato, robot, mapdata)) + dev = [ + NeatoCleaningMap(neato, robot, mapdata) + for robot in hass.data[NEATO_ROBOTS] + if "maps" in robot.traits + ] if not dev: return diff --git a/homeassistant/components/neato/sensor.py b/homeassistant/components/neato/sensor.py index f781c49457e..c247cc48493 100644 --- a/homeassistant/components/neato/sensor.py +++ b/homeassistant/components/neato/sensor.py @@ -30,10 +30,8 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the Neato sensor using config entry.""" - dev = [] neato: NeatoHub = hass.data[NEATO_LOGIN] - for robot in hass.data[NEATO_ROBOTS]: - dev.append(NeatoSensor(neato, robot)) + dev = [NeatoSensor(neato, robot) for robot in hass.data[NEATO_ROBOTS]] if not dev: return diff --git a/homeassistant/components/neato/switch.py b/homeassistant/components/neato/switch.py index 34a0b13a870..25da1c41df1 100644 --- a/homeassistant/components/neato/switch.py +++ b/homeassistant/components/neato/switch.py @@ -32,12 +32,12 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up Neato switch with config entry.""" - dev = [] neato: NeatoHub = hass.data[NEATO_LOGIN] - - for robot in hass.data[NEATO_ROBOTS]: - for type_name in SWITCH_TYPES: - dev.append(NeatoConnectedSwitch(neato, robot, type_name)) + dev = [ + NeatoConnectedSwitch(neato, robot, type_name) + for robot in hass.data[NEATO_ROBOTS] + for type_name in SWITCH_TYPES + ] if not dev: return diff --git a/homeassistant/components/neato/vacuum.py b/homeassistant/components/neato/vacuum.py index 6f1c7c2f5d7..b750b121f58 100644 --- a/homeassistant/components/neato/vacuum.py +++ b/homeassistant/components/neato/vacuum.py @@ -64,12 +64,13 @@ async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up Neato vacuum with config entry.""" - dev = [] neato: NeatoHub = hass.data[NEATO_LOGIN] mapdata: dict[str, Any] | None = hass.data.get(NEATO_MAP_DATA) persistent_maps: dict[str, Any] | None = hass.data.get(NEATO_PERSISTENT_MAPS) - for robot in hass.data[NEATO_ROBOTS]: - dev.append(NeatoConnectedVacuum(neato, robot, mapdata, persistent_maps)) + dev = [ + NeatoConnectedVacuum(neato, robot, mapdata, persistent_maps) + for robot in hass.data[NEATO_ROBOTS] + ] if not dev: return diff --git a/homeassistant/components/nederlandse_spoorwegen/sensor.py b/homeassistant/components/nederlandse_spoorwegen/sensor.py index 1e8a7c30b9d..d99e4c62646 100644 --- a/homeassistant/components/nederlandse_spoorwegen/sensor.py +++ b/homeassistant/components/nederlandse_spoorwegen/sensor.py @@ -135,8 +135,7 @@ class NSDepartureSensor(SensorEntity): if self._trips[0].trip_parts: route = [self._trips[0].departure] - for k in self._trips[0].trip_parts: - route.append(k.destination) + route.extend(k.destination for k in self._trips[0].trip_parts) # Static attributes attributes = { diff --git a/homeassistant/components/nest/camera.py b/homeassistant/components/nest/camera.py index f0b3ccd477d..e87c9ccbbe7 100644 --- a/homeassistant/components/nest/camera.py +++ b/homeassistant/components/nest/camera.py @@ -48,14 +48,12 @@ async def async_setup_entry( device_manager: DeviceManager = hass.data[DOMAIN][entry.entry_id][ DATA_DEVICE_MANAGER ] - entities = [] - for device in device_manager.devices.values(): - if ( - CameraImageTrait.NAME in device.traits - or CameraLiveStreamTrait.NAME in device.traits - ): - entities.append(NestCamera(device)) - async_add_entities(entities) + async_add_entities( + NestCamera(device) + for device in device_manager.devices.values() + if CameraImageTrait.NAME in device.traits + or CameraLiveStreamTrait.NAME in device.traits + ) class NestCamera(Camera): diff --git a/homeassistant/components/nest/climate.py b/homeassistant/components/nest/climate.py index 5e9dcc01e97..411389f9fb2 100644 --- a/homeassistant/components/nest/climate.py +++ b/homeassistant/components/nest/climate.py @@ -86,11 +86,12 @@ async def async_setup_entry( device_manager: DeviceManager = hass.data[DOMAIN][entry.entry_id][ DATA_DEVICE_MANAGER ] - entities = [] - for device in device_manager.devices.values(): - if ThermostatHvacTrait.NAME in device.traits: - entities.append(ThermostatEntity(device)) - async_add_entities(entities) + + async_add_entities( + ThermostatEntity(device) + for device in device_manager.devices.values() + if ThermostatHvacTrait.NAME in device.traits + ) class ThermostatEntity(ClimateEntity): @@ -217,13 +218,13 @@ class ThermostatEntity(ClimateEntity): @property def preset_modes(self) -> list[str]: """Return the available presets.""" - modes = [] - if ThermostatEcoTrait.NAME in self._device.traits: - trait = self._device.traits[ThermostatEcoTrait.NAME] - for mode in trait.available_modes: - if mode in PRESET_MODE_MAP: - modes.append(PRESET_MODE_MAP[mode]) - return modes + if ThermostatEcoTrait.NAME not in self._device.traits: + return [] + return [ + PRESET_MODE_MAP[mode] + for mode in self._device.traits[ThermostatEcoTrait.NAME].available_modes + if mode in PRESET_MODE_MAP + ] @property def fan_mode(self) -> str: diff --git a/homeassistant/components/nest/config_flow.py b/homeassistant/components/nest/config_flow.py index e3854b06038..092d6fe1f22 100644 --- a/homeassistant/components/nest/config_flow.py +++ b/homeassistant/components/nest/config_flow.py @@ -71,10 +71,11 @@ def _generate_subscription_id(cloud_project_id: str) -> str: def generate_config_title(structures: Iterable[Structure]) -> str | None: """Pick a user friendly config title based on the Google Home name(s).""" - names: list[str] = [] - for structure in structures: - if (trait := structure.traits.get(InfoTrait.NAME)) and trait.custom_name: - names.append(trait.custom_name) + names: list[str] = [ + trait.custom_name + for structure in structures + if (trait := structure.traits.get(InfoTrait.NAME)) and trait.custom_name + ] if not names: return None return ", ".join(names) diff --git a/homeassistant/components/nest/media_source.py b/homeassistant/components/nest/media_source.py index ba2faaeaae5..d48006c449d 100644 --- a/homeassistant/components/nest/media_source.py +++ b/homeassistant/components/nest/media_source.py @@ -490,9 +490,10 @@ def _browse_clip_preview( event_id: MediaId, device: Device, event: ClipPreviewSession ) -> BrowseMediaSource: """Build a BrowseMediaSource for a specific clip preview event.""" - types = [] - for event_type in event.event_types: - types.append(MEDIA_SOURCE_EVENT_TITLE_MAP.get(event_type, "Event")) + types = [ + MEDIA_SOURCE_EVENT_TITLE_MAP.get(event_type, "Event") + for event_type in event.event_types + ] return BrowseMediaSource( domain=DOMAIN, identifier=event_id.identifier, diff --git a/homeassistant/components/netatmo/device_trigger.py b/homeassistant/components/netatmo/device_trigger.py index efc2978a43b..686df2ef2cb 100644 --- a/homeassistant/components/netatmo/device_trigger.py +++ b/homeassistant/components/netatmo/device_trigger.py @@ -96,7 +96,7 @@ async def async_get_triggers( """List device triggers for Netatmo devices.""" registry = er.async_get(hass) device_registry = dr.async_get(hass) - triggers = [] + triggers: list[dict[str, str]] = [] for entry in er.async_entries_for_device(registry, device_id): if ( @@ -106,17 +106,17 @@ async def async_get_triggers( for trigger in DEVICES.get(device.model, []): if trigger in SUBTYPES: - for subtype in SUBTYPES[trigger]: - triggers.append( - { - CONF_PLATFORM: "device", - CONF_DEVICE_ID: device_id, - CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.id, - CONF_TYPE: trigger, - CONF_SUBTYPE: subtype, - } - ) + triggers.extend( + { + CONF_PLATFORM: "device", + CONF_DEVICE_ID: device_id, + CONF_DOMAIN: DOMAIN, + CONF_ENTITY_ID: entry.id, + CONF_TYPE: trigger, + CONF_SUBTYPE: subtype, + } + for subtype in SUBTYPES[trigger] + ) else: triggers.append( { diff --git a/homeassistant/components/netgear/sensor.py b/homeassistant/components/netgear/sensor.py index d569a50d7e1..72087dd28db 100644 --- a/homeassistant/components/netgear/sensor.py +++ b/homeassistant/components/netgear/sensor.py @@ -280,30 +280,16 @@ async def async_setup_entry( coordinator_utilization = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_UTIL] coordinator_link = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_LINK] - # Router entities - router_entities = [] - - for description in SENSOR_TRAFFIC_TYPES: - router_entities.append( - NetgearRouterSensorEntity(coordinator_traffic, router, description) + async_add_entities( + NetgearRouterSensorEntity(coordinator, router, description) + for (coordinator, descriptions) in ( + (coordinator_traffic, SENSOR_TRAFFIC_TYPES), + (coordinator_speed, SENSOR_SPEED_TYPES), + (coordinator_utilization, SENSOR_UTILIZATION), + (coordinator_link, SENSOR_LINK_TYPES), ) - - for description in SENSOR_SPEED_TYPES: - router_entities.append( - NetgearRouterSensorEntity(coordinator_speed, router, description) - ) - - for description in SENSOR_UTILIZATION: - router_entities.append( - NetgearRouterSensorEntity(coordinator_utilization, router, description) - ) - - for description in SENSOR_LINK_TYPES: - router_entities.append( - NetgearRouterSensorEntity(coordinator_link, router, description) - ) - - async_add_entities(router_entities) + for description in descriptions + ) # Entities per network device tracked = set() @@ -317,17 +303,15 @@ async def async_setup_entry( if not coordinator.data: return - new_entities = [] + new_entities: list[NetgearSensorEntity] = [] for mac, device in router.devices.items(): if mac in tracked: continue new_entities.extend( - [ - NetgearSensorEntity(coordinator, router, device, attribute) - for attribute in sensors - ] + NetgearSensorEntity(coordinator, router, device, attribute) + for attribute in sensors ) tracked.add(mac) diff --git a/homeassistant/components/netgear/switch.py b/homeassistant/components/netgear/switch.py index e39d4030f3f..85f214d784a 100644 --- a/homeassistant/components/netgear/switch.py +++ b/homeassistant/components/netgear/switch.py @@ -104,13 +104,10 @@ async def async_setup_entry( """Set up switches for Netgear component.""" router = hass.data[DOMAIN][entry.entry_id][KEY_ROUTER] - # Router entities - router_entities = [] - - for description in ROUTER_SWITCH_TYPES: - router_entities.append(NetgearRouterSwitchEntity(router, description)) - - async_add_entities(router_entities) + async_add_entities( + NetgearRouterSwitchEntity(router, description) + for description in ROUTER_SWITCH_TYPES + ) # Entities per network device coordinator = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR] diff --git a/homeassistant/components/network/__init__.py b/homeassistant/components/network/__init__.py index 0ec6f71814f..517ab7e7246 100644 --- a/homeassistant/components/network/__init__.py +++ b/homeassistant/components/network/__init__.py @@ -131,10 +131,8 @@ async def async_get_announce_addresses(hass: HomeAssistant) -> list[str]: for adapter in adapters: if not adapter["enabled"]: continue - for ips in adapter["ipv4"]: - addresses.append(str(IPv4Address(ips["address"]))) - for ips in adapter["ipv6"]: - addresses.append(str(IPv6Address(ips["address"]))) + addresses.extend(str(IPv4Address(ips["address"])) for ips in adapter["ipv4"]) + addresses.extend(str(IPv6Address(ips["address"])) for ips in adapter["ipv6"]) # Puts the default IPv4 address first in the list to preserve compatibility, # because some mDNS implementations ignores anything but the first announced diff --git a/homeassistant/components/nextdns/binary_sensor.py b/homeassistant/components/nextdns/binary_sensor.py index bf230b937c2..f6860586808 100644 --- a/homeassistant/components/nextdns/binary_sensor.py +++ b/homeassistant/components/nextdns/binary_sensor.py @@ -68,11 +68,9 @@ async def async_setup_entry( ATTR_CONNECTION ] - sensors: list[NextDnsBinarySensor] = [] - for description in SENSORS: - sensors.append(NextDnsBinarySensor(coordinator, description)) - - async_add_entities(sensors) + async_add_entities( + NextDnsBinarySensor(coordinator, description) for description in SENSORS + ) class NextDnsBinarySensor( diff --git a/homeassistant/components/nextdns/switch.py b/homeassistant/components/nextdns/switch.py index f9a5dbde4b7..81bf8b4e8c6 100644 --- a/homeassistant/components/nextdns/switch.py +++ b/homeassistant/components/nextdns/switch.py @@ -538,11 +538,9 @@ async def async_setup_entry( ATTR_SETTINGS ] - switches: list[NextDnsSwitch] = [] - for description in SWITCHES: - switches.append(NextDnsSwitch(coordinator, description)) - - async_add_entities(switches) + async_add_entities( + NextDnsSwitch(coordinator, description) for description in SWITCHES + ) class NextDnsSwitch(CoordinatorEntity[NextDnsSettingsUpdateCoordinator], SwitchEntity): diff --git a/homeassistant/components/nina/binary_sensor.py b/homeassistant/components/nina/binary_sensor.py index bcc1eb2a4b6..8104a52683c 100644 --- a/homeassistant/components/nina/binary_sensor.py +++ b/homeassistant/components/nina/binary_sensor.py @@ -44,15 +44,11 @@ async def async_setup_entry( regions: dict[str, str] = config_entry.data[CONF_REGIONS] message_slots: int = config_entry.data[CONF_MESSAGE_SLOTS] - entities: list[NINAMessage] = [] - - for ent in coordinator.data: - for i in range(0, message_slots): - entities.append( - NINAMessage(coordinator, ent, regions[ent], i + 1, config_entry) - ) - - async_add_entities(entities) + async_add_entities( + NINAMessage(coordinator, ent, regions[ent], i + 1, config_entry) + for ent in coordinator.data + for i in range(0, message_slots) + ) class NINAMessage(CoordinatorEntity[NINADataUpdateCoordinator], BinarySensorEntity): diff --git a/homeassistant/components/obihai/sensor.py b/homeassistant/components/obihai/sensor.py index cc70296028f..91920b4c32d 100644 --- a/homeassistant/components/obihai/sensor.py +++ b/homeassistant/components/obihai/sensor.py @@ -24,16 +24,16 @@ async def async_setup_entry( requester: ObihaiConnection = hass.data[DOMAIN][entry.entry_id] - sensors = [] - for key in requester.services: - sensors.append(ObihaiServiceSensors(requester, key)) + sensors = [ObihaiServiceSensors(requester, key) for key in requester.services] + + sensors.extend( + ObihaiServiceSensors(requester, key) for key in requester.call_direction + ) if requester.line_services is not None: - for key in requester.line_services: - sensors.append(ObihaiServiceSensors(requester, key)) - - for key in requester.call_direction: - sensors.append(ObihaiServiceSensors(requester, key)) + sensors.extend( + ObihaiServiceSensors(requester, key) for key in requester.line_services + ) async_add_entities(sensors, update_before_add=True) diff --git a/homeassistant/components/octoprint/sensor.py b/homeassistant/components/octoprint/sensor.py index eb54cadd023..fb5f292d669 100644 --- a/homeassistant/components/octoprint/sensor.py +++ b/homeassistant/components/octoprint/sensor.py @@ -55,7 +55,7 @@ async def async_setup_entry( if not coordinator.data["printer"]: return - new_tools = [] + new_tools: list[OctoPrintTemperatureSensor] = [] for tool in [ tool for tool in coordinator.data["printer"].temperatures @@ -63,15 +63,15 @@ async def async_setup_entry( ]: assert device_id is not None known_tools.add(tool.name) - for temp_type in ("actual", "target"): - new_tools.append( - OctoPrintTemperatureSensor( - coordinator, - tool.name, - temp_type, - device_id, - ) + new_tools.extend( + OctoPrintTemperatureSensor( + coordinator, + tool.name, + temp_type, + device_id, ) + for temp_type in ("actual", "target") + ) async_add_entities(new_tools) config_entry.async_on_unload(coordinator.async_add_listener(async_add_tool_sensors)) diff --git a/homeassistant/components/openalpr_cloud/image_processing.py b/homeassistant/components/openalpr_cloud/image_processing.py index 525edc7da4b..2a8fe328c7d 100644 --- a/homeassistant/components/openalpr_cloud/image_processing.py +++ b/homeassistant/components/openalpr_cloud/image_processing.py @@ -80,15 +80,12 @@ async def async_setup_platform( "country": config[CONF_REGION], } - entities = [] - for camera in config[CONF_SOURCE]: - entities.append( - OpenAlprCloudEntity( - camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME) - ) + async_add_entities( + OpenAlprCloudEntity( + camera[CONF_ENTITY_ID], params, confidence, camera.get(CONF_NAME) ) - - async_add_entities(entities) + for camera in config[CONF_SOURCE] + ) class ImageProcessingAlprEntity(ImageProcessingEntity): diff --git a/homeassistant/components/opencv/image_processing.py b/homeassistant/components/opencv/image_processing.py index 760c473243d..e3dbe10a1a0 100644 --- a/homeassistant/components/opencv/image_processing.py +++ b/homeassistant/components/opencv/image_processing.py @@ -109,23 +109,20 @@ def setup_platform( ) return - entities = [] if CONF_CLASSIFIER not in config: dest_path = hass.config.path(DEFAULT_CLASSIFIER_PATH) _get_default_classifier(dest_path) config[CONF_CLASSIFIER] = {"Face": dest_path} - for camera in config[CONF_SOURCE]: - entities.append( - OpenCVImageProcessor( - hass, - camera[CONF_ENTITY_ID], - camera.get(CONF_NAME), - config[CONF_CLASSIFIER], - ) + add_entities( + OpenCVImageProcessor( + hass, + camera[CONF_ENTITY_ID], + camera.get(CONF_NAME), + config[CONF_CLASSIFIER], ) - - add_entities(entities) + for camera in config[CONF_SOURCE] + ) class OpenCVImageProcessor(ImageProcessingEntity): diff --git a/homeassistant/components/opentherm_gw/binary_sensor.py b/homeassistant/components/opentherm_gw/binary_sensor.py index de949dafbaf..ad8d09afa89 100644 --- a/homeassistant/components/opentherm_gw/binary_sensor.py +++ b/homeassistant/components/opentherm_gw/binary_sensor.py @@ -28,25 +28,19 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up the OpenTherm Gateway binary sensors.""" - sensors = [] gw_dev = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]] - for var, info in BINARY_SENSOR_INFO.items(): - device_class = info[0] - friendly_name_format = info[1] - status_sources = info[2] - for source in status_sources: - sensors.append( - OpenThermBinarySensor( - gw_dev, - var, - source, - device_class, - friendly_name_format, - ) - ) - - async_add_entities(sensors) + async_add_entities( + OpenThermBinarySensor( + gw_dev, + var, + source, + info[0], + info[1], + ) + for var, info in BINARY_SENSOR_INFO.items() + for source in info[2] + ) class OpenThermBinarySensor(BinarySensorEntity): diff --git a/homeassistant/components/opentherm_gw/sensor.py b/homeassistant/components/opentherm_gw/sensor.py index bf393794450..9171292c21b 100644 --- a/homeassistant/components/opentherm_gw/sensor.py +++ b/homeassistant/components/opentherm_gw/sensor.py @@ -23,29 +23,21 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """Set up the OpenTherm Gateway sensors.""" - sensors = [] gw_dev = hass.data[DATA_OPENTHERM_GW][DATA_GATEWAYS][config_entry.data[CONF_ID]] - for var, info in SENSOR_INFO.items(): - device_class = info[0] - unit = info[1] - friendly_name_format = info[2] - suggested_display_precision = info[3] - status_sources = info[4] - for source in status_sources: - sensors.append( - OpenThermSensor( - gw_dev, - var, - source, - device_class, - unit, - friendly_name_format, - suggested_display_precision, - ) - ) - - async_add_entities(sensors) + async_add_entities( + OpenThermSensor( + gw_dev, + var, + source, + info[0], + info[1], + info[2], + info[3], + ) + for var, info in SENSOR_INFO.items() + for source in info[4] + ) class OpenThermSensor(SensorEntity): diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index 2d8f1b5ded5..9f467dce1c6 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -187,16 +187,16 @@ async def async_setup_entry( and forecast.unit_of_measure in [UnitOfMeasure.THERM, UnitOfMeasure.CCF] ): sensors = GAS_SENSORS - for sensor in sensors: - entities.append( - OpowerSensor( - coordinator, - sensor, - forecast.account.utility_account_id, - device, - device_id, - ) + entities.extend( + OpowerSensor( + coordinator, + sensor, + forecast.account.utility_account_id, + device, + device_id, ) + for sensor in sensors + ) async_add_entities(entities) diff --git a/homeassistant/components/osoenergy/water_heater.py b/homeassistant/components/osoenergy/water_heater.py index 146bdaa3a60..eaf54a9f9a4 100644 --- a/homeassistant/components/osoenergy/water_heater.py +++ b/homeassistant/components/osoenergy/water_heater.py @@ -45,11 +45,9 @@ async def async_setup_entry( """Set up OSO Energy heater based on a config entry.""" osoenergy = hass.data[DOMAIN][entry.entry_id] devices = osoenergy.session.device_list.get("water_heater") - entities = [] - if devices: - for dev in devices: - entities.append(OSOEnergyWaterHeater(osoenergy, dev)) - async_add_entities(entities, True) + if not devices: + return + async_add_entities((OSOEnergyWaterHeater(osoenergy, dev) for dev in devices), True) class OSOEnergyWaterHeater( diff --git a/homeassistant/components/overkiz/alarm_control_panel.py b/homeassistant/components/overkiz/alarm_control_panel.py index 1f088874d43..72c99982a1b 100644 --- a/homeassistant/components/overkiz/alarm_control_panel.py +++ b/homeassistant/components/overkiz/alarm_control_panel.py @@ -221,21 +221,19 @@ async def async_setup_entry( ) -> None: """Set up the Overkiz alarm control panel from a config entry.""" data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id] - entities: list[OverkizAlarmControlPanel] = [] - for device in data.platforms[Platform.ALARM_CONTROL_PANEL]: - if description := SUPPORTED_DEVICES.get(device.widget) or SUPPORTED_DEVICES.get( - device.ui_class - ): - entities.append( - OverkizAlarmControlPanel( - device.device_url, - data.coordinator, - description, - ) - ) - - async_add_entities(entities) + async_add_entities( + OverkizAlarmControlPanel( + device.device_url, + data.coordinator, + description, + ) + for device in data.platforms[Platform.ALARM_CONTROL_PANEL] + if ( + description := SUPPORTED_DEVICES.get(device.widget) + or SUPPORTED_DEVICES.get(device.ui_class) + ) + ) class OverkizAlarmControlPanel(OverkizDescriptiveEntity, AlarmControlPanelEntity): diff --git a/homeassistant/components/overkiz/binary_sensor.py b/homeassistant/components/overkiz/binary_sensor.py index b4365a66e06..871a70b3e0a 100644 --- a/homeassistant/components/overkiz/binary_sensor.py +++ b/homeassistant/components/overkiz/binary_sensor.py @@ -128,15 +128,15 @@ async def async_setup_entry( ): continue - for state in device.definition.states: - if description := SUPPORTED_STATES.get(state.qualified_name): - entities.append( - OverkizBinarySensor( - device.device_url, - data.coordinator, - description, - ) - ) + entities.extend( + OverkizBinarySensor( + device.device_url, + data.coordinator, + description, + ) + for state in device.definition.states + if (description := SUPPORTED_STATES.get(state.qualified_name)) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/button.py b/homeassistant/components/overkiz/button.py index 3e0321b5055..5a1116aeeb5 100644 --- a/homeassistant/components/overkiz/button.py +++ b/homeassistant/components/overkiz/button.py @@ -95,15 +95,15 @@ async def async_setup_entry( ): continue - for command in device.definition.commands: - if description := SUPPORTED_COMMANDS.get(command.command_name): - entities.append( - OverkizButton( - device.device_url, - data.coordinator, - description, - ) - ) + entities.extend( + OverkizButton( + device.device_url, + data.coordinator, + description, + ) + for command in device.definition.commands + if (description := SUPPORTED_COMMANDS.get(command.command_name)) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/number.py b/homeassistant/components/overkiz/number.py index e9664527335..76a3e5d932b 100644 --- a/homeassistant/components/overkiz/number.py +++ b/homeassistant/components/overkiz/number.py @@ -183,15 +183,15 @@ async def async_setup_entry( ): continue - for state in device.definition.states: - if description := SUPPORTED_STATES.get(state.qualified_name): - entities.append( - OverkizNumber( - device.device_url, - data.coordinator, - description, - ) - ) + entities.extend( + OverkizNumber( + device.device_url, + data.coordinator, + description, + ) + for state in device.definition.states + if (description := SUPPORTED_STATES.get(state.qualified_name)) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/select.py b/homeassistant/components/overkiz/select.py index 155c72a7f7a..83cdc9c4f2b 100644 --- a/homeassistant/components/overkiz/select.py +++ b/homeassistant/components/overkiz/select.py @@ -143,15 +143,15 @@ async def async_setup_entry( ): continue - for state in device.definition.states: - if description := SUPPORTED_STATES.get(state.qualified_name): - entities.append( - OverkizSelect( - device.device_url, - data.coordinator, - description, - ) - ) + entities.extend( + OverkizSelect( + device.device_url, + data.coordinator, + description, + ) + for state in device.definition.states + if (description := SUPPORTED_STATES.get(state.qualified_name)) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/sensor.py b/homeassistant/components/overkiz/sensor.py index b8693784395..2b0a222f96f 100644 --- a/homeassistant/components/overkiz/sensor.py +++ b/homeassistant/components/overkiz/sensor.py @@ -459,15 +459,15 @@ async def async_setup_entry( ): continue - for state in device.definition.states: - if description := SUPPORTED_STATES.get(state.qualified_name): - entities.append( - OverkizStateSensor( - device.device_url, - data.coordinator, - description, - ) - ) + entities.extend( + OverkizStateSensor( + device.device_url, + data.coordinator, + description, + ) + for state in device.definition.states + if (description := SUPPORTED_STATES.get(state.qualified_name)) + ) async_add_entities(entities) diff --git a/homeassistant/components/overkiz/switch.py b/homeassistant/components/overkiz/switch.py index 5d1fe0493ac..ac3ea351559 100644 --- a/homeassistant/components/overkiz/switch.py +++ b/homeassistant/components/overkiz/switch.py @@ -116,21 +116,19 @@ async def async_setup_entry( ) -> None: """Set up the Overkiz switch from a config entry.""" data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id] - entities: list[OverkizSwitch] = [] - for device in data.platforms[Platform.SWITCH]: - if description := SUPPORTED_DEVICES.get(device.widget) or SUPPORTED_DEVICES.get( - device.ui_class - ): - entities.append( - OverkizSwitch( - device.device_url, - data.coordinator, - description, - ) - ) - - async_add_entities(entities) + async_add_entities( + OverkizSwitch( + device.device_url, + data.coordinator, + description, + ) + for device in data.platforms[Platform.SWITCH] + if ( + description := SUPPORTED_DEVICES.get(device.widget) + or SUPPORTED_DEVICES.get(device.ui_class) + ) + ) class OverkizSwitch(OverkizDescriptiveEntity, SwitchEntity): diff --git a/homeassistant/components/owntracks/__init__.py b/homeassistant/components/owntracks/__init__.py index dbc4df293c0..ffc50065c5a 100644 --- a/homeassistant/components/owntracks/__init__.py +++ b/homeassistant/components/owntracks/__init__.py @@ -187,19 +187,17 @@ async def handle_webhook(hass, webhook_id, request): async_dispatcher_send(hass, DOMAIN, hass, context, message) - response = [] - - for person in hass.states.async_all("person"): - if "latitude" in person.attributes and "longitude" in person.attributes: - response.append( - { - "_type": "location", - "lat": person.attributes["latitude"], - "lon": person.attributes["longitude"], - "tid": "".join(p[0] for p in person.name.split(" ")[:2]), - "tst": int(person.last_updated.timestamp()), - } - ) + response = [ + { + "_type": "location", + "lat": person.attributes["latitude"], + "lon": person.attributes["longitude"], + "tid": "".join(p[0] for p in person.name.split(" ")[:2]), + "tst": int(person.last_updated.timestamp()), + } + for person in hass.states.async_all("person") + if "latitude" in person.attributes and "longitude" in person.attributes + ] if message["_type"] == "encrypted" and context.secret: return json_response( diff --git a/tests/components/netatmo/test_device_trigger.py b/tests/components/netatmo/test_device_trigger.py index 65d7274cdc8..f7c31d7681c 100644 --- a/tests/components/netatmo/test_device_trigger.py +++ b/tests/components/netatmo/test_device_trigger.py @@ -63,18 +63,18 @@ async def test_get_triggers( expected_triggers = [] for event_type in event_types: if event_type in SUBTYPES: - for subtype in SUBTYPES[event_type]: - expected_triggers.append( - { - "platform": "device", - "domain": NETATMO_DOMAIN, - "type": event_type, - "subtype": subtype, - "device_id": device_entry.id, - "entity_id": entity_entry.id, - "metadata": {"secondary": False}, - } - ) + expected_triggers.extend( + { + "platform": "device", + "domain": NETATMO_DOMAIN, + "type": event_type, + "subtype": subtype, + "device_id": device_entry.id, + "entity_id": entity_entry.id, + "metadata": {"secondary": False}, + } + for subtype in SUBTYPES[event_type] + ) else: expected_triggers.append( { diff --git a/tests/components/onewire/__init__.py b/tests/components/onewire/__init__.py index 2929ed3ed50..7a9dc01117d 100644 --- a/tests/components/onewire/__init__.py +++ b/tests/components/onewire/__init__.py @@ -102,7 +102,9 @@ def _setup_owproxy_mock_device_reads( device_sensors = mock_device.get(platform, []) if platform is Platform.SENSOR and device_id.startswith("12"): # We need to check if there is TAI8570 plugged in - for expected_sensor in device_sensors: - sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS]) - for expected_sensor in device_sensors: - sub_read_side_effect.append(expected_sensor[ATTR_INJECT_READS]) + sub_read_side_effect.extend( + expected_sensor[ATTR_INJECT_READS] for expected_sensor in device_sensors + ) + sub_read_side_effect.extend( + expected_sensor[ATTR_INJECT_READS] for expected_sensor in device_sensors + )