Improve lists in integrations [P-Q] (#113236)

This commit is contained in:
Joost Lekkerkerker 2024-03-13 17:30:30 +01:00 committed by GitHub
parent dc7eaee917
commit 9f19e7339d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 63 additions and 76 deletions

View File

@ -66,18 +66,15 @@ class PicnicCart(TodoListEntity, CoordinatorEntity[PicnicUpdateCoordinator]):
_LOGGER.debug(self.coordinator.data["cart_data"]["items"]) _LOGGER.debug(self.coordinator.data["cart_data"]["items"])
items = [] return [
for item in self.coordinator.data["cart_data"]["items"]:
for article in item["items"]:
items.append(
TodoItem( TodoItem(
summary=f"{article['name']} ({article['unit_quantity']})", summary=f"{article['name']} ({article['unit_quantity']})",
uid=f"{item['id']}-{article['id']}", uid=f"{item['id']}-{article['id']}",
status=TodoItemStatus.NEEDS_ACTION, # We set 'NEEDS_ACTION' so they count as state 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"]
return items ]
async def async_create_todo_item(self, item: TodoItem) -> None: async def async_create_todo_item(self, item: TodoItem) -> None:
"""Add item to shopping cart.""" """Add item to shopping cart."""

View File

@ -293,10 +293,7 @@ def generate_plex_uri(server_id, media_id, params=None):
def root_payload(hass, is_internal, platform=None): def root_payload(hass, is_internal, platform=None):
"""Return root payload for Plex.""" """Return root payload for Plex."""
children = [] children = [
for server_id in get_plex_data(hass)[SERVERS]:
children.append(
browse_media( browse_media(
hass, hass,
is_internal, is_internal,
@ -304,7 +301,8 @@ def root_payload(hass, is_internal, platform=None):
generate_plex_uri(server_id, ""), generate_plex_uri(server_id, ""),
platform=platform, platform=platform,
) )
) for server_id in get_plex_data(hass)[SERVERS]
]
if len(children) == 1: if len(children) == 1:
return children[0] return children[0]

View File

@ -57,12 +57,14 @@ async def async_setup_entry(
"""Set up Plex sensor from a config entry.""" """Set up Plex sensor from a config entry."""
server_id = config_entry.data[CONF_SERVER_IDENTIFIER] server_id = config_entry.data[CONF_SERVER_IDENTIFIER]
plexserver = get_plex_server(hass, server_id) plexserver = get_plex_server(hass, server_id)
sensors = [PlexSensor(hass, plexserver)] sensors: list[SensorEntity] = [PlexSensor(hass, plexserver)]
def create_library_sensors(): def create_library_sensors():
"""Create Plex library sensors with sync calls.""" """Create Plex library sensors with sync calls."""
for library in plexserver.library.sections(): sensors.extend(
sensors.append(PlexLibrarySectionSensor(hass, plexserver, library)) PlexLibrarySectionSensor(hass, plexserver, library)
for library in plexserver.library.sections()
)
await hass.async_add_executor_job(create_library_sensors) await hass.async_add_executor_job(create_library_sensors)
async_add_entities(sensors) async_add_entities(sensors)

View File

@ -76,16 +76,13 @@ async def async_setup_entry(
config_entry.entry_id config_entry.entry_id
] ]
entities: list[PlugwiseNumberEntity] = [] async_add_entities(
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) PlugwiseNumberEntity(coordinator, device_id, description)
for device_id, device in coordinator.data.devices.items()
for description in NUMBER_TYPES
if description.key in device
) )
async_add_entities(entities)
class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity): class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity):
"""Representation of a Plugwise number.""" """Representation of a Plugwise number."""

View File

@ -68,16 +68,13 @@ async def async_setup_entry(
config_entry.entry_id config_entry.entry_id
] ]
entities: list[PlugwiseSelectEntity] = [] async_add_entities(
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) 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
) )
async_add_entities(entities)
class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity): class PlugwiseSelectEntity(PlugwiseEntity, SelectEntity):
"""Represent Smile selector.""" """Represent Smile selector."""

View File

@ -29,7 +29,6 @@ async def async_setup_entry(
"""Set up the binary sensors from a config entry.""" """Set up the binary sensors from a config entry."""
board_api = hass.data[DOMAIN][config_entry.entry_id] board_api = hass.data[DOMAIN][config_entry.entry_id]
input_count = config_entry.data["input_count"] input_count = config_entry.data["input_count"]
binary_sensors = []
async def async_update_data(): async def async_update_data():
"""Fetch data from API endpoint of board.""" """Fetch data from API endpoint of board."""
@ -45,17 +44,15 @@ async def async_setup_entry(
) )
await coordinator.async_refresh() await coordinator.async_refresh()
for i in range(1, int(input_count) + 1): async_add_entities(
binary_sensors.append(
ProgettihwswBinarySensor( ProgettihwswBinarySensor(
coordinator, coordinator,
f"Input #{i}", f"Input #{i}",
setup_input(board_api, i), setup_input(board_api, i),
) )
for i in range(1, int(input_count) + 1)
) )
async_add_entities(binary_sensors)
class ProgettihwswBinarySensor(CoordinatorEntity, BinarySensorEntity): class ProgettihwswBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Represent a binary sensor.""" """Represent a binary sensor."""

View File

@ -30,7 +30,6 @@ async def async_setup_entry(
"""Set up the switches from a config entry.""" """Set up the switches from a config entry."""
board_api = hass.data[DOMAIN][config_entry.entry_id] board_api = hass.data[DOMAIN][config_entry.entry_id]
relay_count = config_entry.data["relay_count"] relay_count = config_entry.data["relay_count"]
switches = []
async def async_update_data(): async def async_update_data():
"""Fetch data from API endpoint of board.""" """Fetch data from API endpoint of board."""
@ -46,17 +45,15 @@ async def async_setup_entry(
) )
await coordinator.async_refresh() await coordinator.async_refresh()
for i in range(1, int(relay_count) + 1): async_add_entities(
switches.append(
ProgettihwswSwitch( ProgettihwswSwitch(
coordinator, coordinator,
f"Relay #{i}", f"Relay #{i}",
setup_switch(board_api, i, config_entry.data[f"relay_{str(i)}"]), setup_switch(board_api, i, config_entry.data[f"relay_{str(i)}"]),
) )
for i in range(1, int(relay_count) + 1)
) )
async_add_entities(switches)
class ProgettihwswSwitch(CoordinatorEntity, SwitchEntity): class ProgettihwswSwitch(CoordinatorEntity, SwitchEntity):
"""Represent a switch entity.""" """Represent a switch entity."""

View File

@ -154,8 +154,10 @@ async def async_setup_entry(
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)] sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)]
if coordinator.api.using_private_api: if coordinator.api.using_private_api:
for sensor_desc in SENSOR_TYPES[1:]: sensors.extend(
sensors.append(ElecPriceSensor(coordinator, sensor_desc, entry.unique_id)) ElecPriceSensor(coordinator, sensor_desc, entry.unique_id)
for sensor_desc in SENSOR_TYPES[1:]
)
async_add_entities(sensors) async_add_entities(sensors)

View File

@ -83,14 +83,14 @@ async def async_setup_entry(
"""Add QNAP QSW binary sensors from a config_entry.""" """Add QNAP QSW binary sensors from a config_entry."""
coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA] coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA]
entities: list[QswBinarySensor] = [] entities: list[QswBinarySensor] = [
QswBinarySensor(coordinator, description, entry)
for description in BINARY_SENSOR_TYPES: for description in BINARY_SENSOR_TYPES
if ( if (
description.key in coordinator.data description.key in coordinator.data
and description.subkey in coordinator.data[description.key] and description.subkey in coordinator.data[description.key]
): )
entities.append(QswBinarySensor(coordinator, description, entry)) ]
for description in LACP_PORT_BINARY_SENSOR_TYPES: for description in LACP_PORT_BINARY_SENSOR_TYPES:
if ( if (

View File

@ -288,14 +288,14 @@ async def async_setup_entry(
"""Add QNAP QSW sensors from a config_entry.""" """Add QNAP QSW sensors from a config_entry."""
coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA] coordinator: QswDataCoordinator = hass.data[DOMAIN][entry.entry_id][QSW_COORD_DATA]
entities: list[QswSensor] = [] entities: list[QswSensor] = [
QswSensor(coordinator, description, entry)
for description in SENSOR_TYPES: for description in SENSOR_TYPES
if ( if (
description.key in coordinator.data description.key in coordinator.data
and description.subkey in coordinator.data[description.key] and description.subkey in coordinator.data[description.key]
): )
entities.append(QswSensor(coordinator, description, entry)) ]
for description in LACP_PORT_SENSOR_TYPES: for description in LACP_PORT_SENSOR_TYPES:
if ( if (