From 9f19e7339d6f0caccfba1f3dc71642323d6777c3 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 13 Mar 2024 17:30:30 +0100 Subject: [PATCH] Improve lists in integrations [P-Q] (#113236) --- homeassistant/components/picnic/todo.py | 21 ++++++++----------- .../components/plex/media_browser.py | 20 ++++++++---------- homeassistant/components/plex/sensor.py | 8 ++++--- homeassistant/components/plugwise/number.py | 15 ++++++------- homeassistant/components/plugwise/select.py | 15 ++++++------- .../components/progettihwsw/binary_sensor.py | 17 +++++++-------- .../components/progettihwsw/switch.py | 17 +++++++-------- .../components/pvpc_hourly_pricing/sensor.py | 6 ++++-- .../components/qnap_qsw/binary_sensor.py | 10 ++++----- homeassistant/components/qnap_qsw/sensor.py | 10 ++++----- 10 files changed, 63 insertions(+), 76 deletions(-) diff --git a/homeassistant/components/picnic/todo.py b/homeassistant/components/picnic/todo.py index c645a5ebf83..7fa2bbccd3e 100644 --- a/homeassistant/components/picnic/todo.py +++ b/homeassistant/components/picnic/todo.py @@ -66,18 +66,15 @@ class PicnicCart(TodoListEntity, CoordinatorEntity[PicnicUpdateCoordinator]): _LOGGER.debug(self.coordinator.data["cart_data"]["items"]) - items = [] - for item in self.coordinator.data["cart_data"]["items"]: - for article in item["items"]: - items.append( - TodoItem( - summary=f"{article['name']} ({article['unit_quantity']})", - uid=f"{item['id']}-{article['id']}", - status=TodoItemStatus.NEEDS_ACTION, # We set 'NEEDS_ACTION' so they count as state - ) - ) - - return items + return [ + TodoItem( + summary=f"{article['name']} ({article['unit_quantity']})", + uid=f"{item['id']}-{article['id']}", + status=TodoItemStatus.NEEDS_ACTION, # We set 'NEEDS_ACTION' so they count as state + ) + for item in self.coordinator.data["cart_data"]["items"] + for article in item["items"] + ] async def async_create_todo_item(self, item: TodoItem) -> None: """Add item to shopping cart.""" diff --git a/homeassistant/components/plex/media_browser.py b/homeassistant/components/plex/media_browser.py index e5bbb9284ff..9184edeb3bd 100644 --- a/homeassistant/components/plex/media_browser.py +++ b/homeassistant/components/plex/media_browser.py @@ -293,18 +293,16 @@ def generate_plex_uri(server_id, media_id, params=None): def root_payload(hass, is_internal, platform=None): """Return root payload for Plex.""" - children = [] - - for server_id in get_plex_data(hass)[SERVERS]: - children.append( - browse_media( - hass, - is_internal, - "server", - generate_plex_uri(server_id, ""), - platform=platform, - ) + children = [ + browse_media( + hass, + is_internal, + "server", + generate_plex_uri(server_id, ""), + platform=platform, ) + for server_id in get_plex_data(hass)[SERVERS] + ] if len(children) == 1: return children[0] diff --git a/homeassistant/components/plex/sensor.py b/homeassistant/components/plex/sensor.py index 74fab3b1714..30ff8e72f60 100644 --- a/homeassistant/components/plex/sensor.py +++ b/homeassistant/components/plex/sensor.py @@ -57,12 +57,14 @@ async def async_setup_entry( """Set up Plex sensor from a config entry.""" server_id = config_entry.data[CONF_SERVER_IDENTIFIER] plexserver = get_plex_server(hass, server_id) - sensors = [PlexSensor(hass, plexserver)] + sensors: list[SensorEntity] = [PlexSensor(hass, plexserver)] def create_library_sensors(): """Create Plex library sensors with sync calls.""" - for library in plexserver.library.sections(): - sensors.append(PlexLibrarySectionSensor(hass, plexserver, library)) + sensors.extend( + PlexLibrarySectionSensor(hass, plexserver, library) + for library in plexserver.library.sections() + ) await hass.async_add_executor_job(create_library_sensors) async_add_entities(sensors) diff --git a/homeassistant/components/plugwise/number.py b/homeassistant/components/plugwise/number.py index 95d09bd845e..2bae113a73e 100644 --- a/homeassistant/components/plugwise/number.py +++ b/homeassistant/components/plugwise/number.py @@ -76,15 +76,12 @@ async def async_setup_entry( config_entry.entry_id ] - entities: list[PlugwiseNumberEntity] = [] - for device_id, device in coordinator.data.devices.items(): - for description in NUMBER_TYPES: - if description.key in device: - entities.append( - PlugwiseNumberEntity(coordinator, device_id, description) - ) - - async_add_entities(entities) + async_add_entities( + PlugwiseNumberEntity(coordinator, device_id, description) + for device_id, device in coordinator.data.devices.items() + for description in NUMBER_TYPES + if description.key in device + ) class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity): diff --git a/homeassistant/components/plugwise/select.py b/homeassistant/components/plugwise/select.py index 21b0794a1e3..10718a818ff 100644 --- a/homeassistant/components/plugwise/select.py +++ b/homeassistant/components/plugwise/select.py @@ -68,15 +68,12 @@ async def async_setup_entry( config_entry.entry_id ] - entities: list[PlugwiseSelectEntity] = [] - for device_id, device in coordinator.data.devices.items(): - for description in SELECT_TYPES: - if description.options_key in device: - entities.append( - PlugwiseSelectEntity(coordinator, device_id, description) - ) - - async_add_entities(entities) + async_add_entities( + PlugwiseSelectEntity(coordinator, device_id, description) + for device_id, device in coordinator.data.devices.items() + for description in SELECT_TYPES + if description.options_key in device + ) class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity): diff --git a/homeassistant/components/progettihwsw/binary_sensor.py b/homeassistant/components/progettihwsw/binary_sensor.py index fd4f478c045..a89b8b3c3f1 100644 --- a/homeassistant/components/progettihwsw/binary_sensor.py +++ b/homeassistant/components/progettihwsw/binary_sensor.py @@ -29,7 +29,6 @@ async def async_setup_entry( """Set up the binary sensors from a config entry.""" board_api = hass.data[DOMAIN][config_entry.entry_id] input_count = config_entry.data["input_count"] - binary_sensors = [] async def async_update_data(): """Fetch data from API endpoint of board.""" @@ -45,16 +44,14 @@ async def async_setup_entry( ) await coordinator.async_refresh() - for i in range(1, int(input_count) + 1): - binary_sensors.append( - ProgettihwswBinarySensor( - coordinator, - f"Input #{i}", - setup_input(board_api, i), - ) + async_add_entities( + ProgettihwswBinarySensor( + coordinator, + f"Input #{i}", + setup_input(board_api, i), ) - - async_add_entities(binary_sensors) + for i in range(1, int(input_count) + 1) + ) class ProgettihwswBinarySensor(CoordinatorEntity, BinarySensorEntity): diff --git a/homeassistant/components/progettihwsw/switch.py b/homeassistant/components/progettihwsw/switch.py index accabcfb32f..88faa35e0a4 100644 --- a/homeassistant/components/progettihwsw/switch.py +++ b/homeassistant/components/progettihwsw/switch.py @@ -30,7 +30,6 @@ async def async_setup_entry( """Set up the switches from a config entry.""" board_api = hass.data[DOMAIN][config_entry.entry_id] relay_count = config_entry.data["relay_count"] - switches = [] async def async_update_data(): """Fetch data from API endpoint of board.""" @@ -46,16 +45,14 @@ async def async_setup_entry( ) await coordinator.async_refresh() - for i in range(1, int(relay_count) + 1): - switches.append( - ProgettihwswSwitch( - coordinator, - f"Relay #{i}", - setup_switch(board_api, i, config_entry.data[f"relay_{str(i)}"]), - ) + async_add_entities( + ProgettihwswSwitch( + coordinator, + f"Relay #{i}", + setup_switch(board_api, i, config_entry.data[f"relay_{str(i)}"]), ) - - async_add_entities(switches) + for i in range(1, int(relay_count) + 1) + ) class ProgettihwswSwitch(CoordinatorEntity, SwitchEntity): diff --git a/homeassistant/components/pvpc_hourly_pricing/sensor.py b/homeassistant/components/pvpc_hourly_pricing/sensor.py index 1b97abcddae..246a8b65892 100644 --- a/homeassistant/components/pvpc_hourly_pricing/sensor.py +++ b/homeassistant/components/pvpc_hourly_pricing/sensor.py @@ -154,8 +154,10 @@ async def async_setup_entry( coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)] if coordinator.api.using_private_api: - for sensor_desc in SENSOR_TYPES[1:]: - sensors.append(ElecPriceSensor(coordinator, sensor_desc, entry.unique_id)) + sensors.extend( + ElecPriceSensor(coordinator, sensor_desc, entry.unique_id) + for sensor_desc in SENSOR_TYPES[1:] + ) async_add_entities(sensors) diff --git a/homeassistant/components/qnap_qsw/binary_sensor.py b/homeassistant/components/qnap_qsw/binary_sensor.py index 907e3d927d3..a9c025b86ce 100644 --- a/homeassistant/components/qnap_qsw/binary_sensor.py +++ b/homeassistant/components/qnap_qsw/binary_sensor.py @@ -83,14 +83,14 @@ async def async_setup_entry( """Add QNAP QSW binary sensors from a config_entry.""" coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA] - entities: list[QswBinarySensor] = [] - - for description in BINARY_SENSOR_TYPES: + entities: list[QswBinarySensor] = [ + QswBinarySensor(coordinator, description, entry) + for description in BINARY_SENSOR_TYPES if ( description.key in coordinator.data and description.subkey in coordinator.data[description.key] - ): - entities.append(QswBinarySensor(coordinator, description, entry)) + ) + ] for description in LACP_PORT_BINARY_SENSOR_TYPES: if ( diff --git a/homeassistant/components/qnap_qsw/sensor.py b/homeassistant/components/qnap_qsw/sensor.py index f9299c778cb..b64c0aaad82 100644 --- a/homeassistant/components/qnap_qsw/sensor.py +++ b/homeassistant/components/qnap_qsw/sensor.py @@ -288,14 +288,14 @@ async def async_setup_entry( """Add QNAP QSW sensors from a config_entry.""" coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA] - entities: list[QswSensor] = [] - - for description in SENSOR_TYPES: + entities: list[QswSensor] = [ + QswSensor(coordinator, description, entry) + for description in SENSOR_TYPES if ( description.key in coordinator.data and description.subkey in coordinator.data[description.key] - ): - entities.append(QswSensor(coordinator, description, entry)) + ) + ] for description in LACP_PORT_SENSOR_TYPES: if (