Improve lists in integrations [B] (#113069)

This commit is contained in:
Joost Lekkerkerker 2024-03-11 18:59:57 +01:00 committed by GitHub
parent 1853c2d73a
commit 23ffcaf187
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 144 additions and 151 deletions

View File

@ -100,10 +100,11 @@ async def async_setup_entry(
"""Set up BAF fan sensors.""" """Set up BAF fan sensors."""
data: BAFData = hass.data[DOMAIN][entry.entry_id] data: BAFData = hass.data[DOMAIN][entry.entry_id]
device = data.device device = data.device
sensors_descriptions: list[BAFSensorDescription] = [] sensors_descriptions: list[BAFSensorDescription] = [
for description in DEFINED_ONLY_SENSORS: description
if getattr(device, description.key): for description in DEFINED_ONLY_SENSORS
sensors_descriptions.append(description) if getattr(device, description.key)
]
if device.has_auto_comfort: if device.has_auto_comfort:
sensors_descriptions.extend(AUTO_COMFORT_SENSORS) sensors_descriptions.extend(AUTO_COMFORT_SENSORS)
if device.has_fan: if device.has_fan:

View File

@ -313,9 +313,9 @@ class BayesianBinarySensor(BinarySensorEntity):
self.hass, observations, text=f"{self._attr_name}/{entity}" self.hass, observations, text=f"{self._attr_name}/{entity}"
) )
all_template_observations: list[Observation] = [] all_template_observations: list[Observation] = [
for observations in self.observations_by_template.values(): observations[0] for observations in self.observations_by_template.values()
all_template_observations.append(observations[0]) ]
if len(all_template_observations) == 2: if len(all_template_observations) == 2:
raise_mirrored_entries( raise_mirrored_entries(
self.hass, self.hass,

View File

@ -217,13 +217,13 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up Blue Current sensors.""" """Set up Blue Current sensors."""
connector: Connector = hass.data[DOMAIN][entry.entry_id] connector: Connector = hass.data[DOMAIN][entry.entry_id]
sensor_list: list[SensorEntity] = [] sensor_list: list[SensorEntity] = [
for evse_id in connector.charge_points: ChargePointSensor(connector, sensor, evse_id)
for sensor in SENSORS: for evse_id in connector.charge_points
sensor_list.append(ChargePointSensor(connector, sensor, evse_id)) for sensor in SENSORS
]
for grid_sensor in GRID_SENSORS: sensor_list.extend(GridSensor(connector, sensor) for sensor in GRID_SENSORS)
sensor_list.append(GridSensor(connector, grid_sensor))
async_add_entities(sensor_list) async_add_entities(sensor_list)

View File

@ -686,20 +686,15 @@ class BluesoundPlayer(MediaPlayerEntity):
if self._status is None or (self.is_grouped and not self.is_master): if self._status is None or (self.is_grouped and not self.is_master):
return None return None
sources = [] sources = [source["title"] for source in self._preset_items]
for source in self._preset_items: sources.extend(
sources.append(source["title"]) source["title"]
for source in self._services_items
if source["type"] in ("LocalMusic", "RadioService")
)
for source in [ sources.extend(source["title"] for source in self._capture_items)
x
for x in self._services_items
if x["type"] in ("LocalMusic", "RadioService")
]:
sources.append(source["title"])
for source in self._capture_items:
sources.append(source["title"])
return sources return sources

View File

@ -249,39 +249,47 @@ class BluetoothMatcherIndexBase(Generic[_T]):
def match(self, service_info: BluetoothServiceInfoBleak) -> list[_T]: def match(self, service_info: BluetoothServiceInfoBleak) -> list[_T]:
"""Check for a match.""" """Check for a match."""
matches = [] matches: list[_T] = []
if (name := service_info.name) and ( if (name := service_info.name) and (
local_name_matchers := self.local_name.get( local_name_matchers := self.local_name.get(
name[:LOCAL_NAME_MIN_MATCH_LENGTH] name[:LOCAL_NAME_MIN_MATCH_LENGTH]
) )
): ):
for matcher in local_name_matchers: matches.extend(
if ble_device_matches(matcher, service_info): matcher
matches.append(matcher) for matcher in local_name_matchers
if ble_device_matches(matcher, service_info)
)
if self.service_data_uuid_set and service_info.service_data: if self.service_data_uuid_set and service_info.service_data:
matches.extend(
matcher
for service_data_uuid in self.service_data_uuid_set.intersection( for service_data_uuid in self.service_data_uuid_set.intersection(
service_info.service_data service_info.service_data
): )
for matcher in self.service_data_uuid[service_data_uuid]: for matcher in self.service_data_uuid[service_data_uuid]
if ble_device_matches(matcher, service_info): if ble_device_matches(matcher, service_info)
matches.append(matcher) )
if self.manufacturer_id_set and service_info.manufacturer_data: if self.manufacturer_id_set and service_info.manufacturer_data:
matches.extend(
matcher
for manufacturer_id in self.manufacturer_id_set.intersection( for manufacturer_id in self.manufacturer_id_set.intersection(
service_info.manufacturer_data service_info.manufacturer_data
): )
for matcher in self.manufacturer_id[manufacturer_id]: for matcher in self.manufacturer_id[manufacturer_id]
if ble_device_matches(matcher, service_info): if ble_device_matches(matcher, service_info)
matches.append(matcher) )
if self.service_uuid_set and service_info.service_uuids: if self.service_uuid_set and service_info.service_uuids:
matches.extend(
matcher
for service_uuid in self.service_uuid_set.intersection( for service_uuid in self.service_uuid_set.intersection(
service_info.service_uuids service_info.service_uuids
): )
for matcher in self.service_uuid[service_uuid]: for matcher in self.service_uuid[service_uuid]
if ble_device_matches(matcher, service_info): if ble_device_matches(matcher, service_info)
matches.append(matcher) )
return matches return matches

View File

@ -31,17 +31,10 @@ async def async_setup_entry(
"""Set up the MyBMW lock from config entry.""" """Set up the MyBMW lock from config entry."""
coordinator: BMWDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id] coordinator: BMWDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
entities: list[BMWLock] = []
for vehicle in coordinator.account.vehicles:
if not coordinator.read_only: if not coordinator.read_only:
entities.append( async_add_entities(
BMWLock( BMWLock(coordinator, vehicle) for vehicle in coordinator.account.vehicles
coordinator,
vehicle,
) )
)
async_add_entities(entities)
class BMWLock(BMWBaseEntity, LockEntity): class BMWLock(BMWBaseEntity, LockEntity):

View File

@ -23,20 +23,26 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the SHC binary sensor platform.""" """Set up the SHC binary sensor platform."""
entities: list[BinarySensorEntity] = []
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION] session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
for binary_sensor in ( entities: list[BinarySensorEntity] = [
session.device_helper.shutter_contacts + session.device_helper.shutter_contacts2
):
entities.append(
ShutterContactSensor( ShutterContactSensor(
device=binary_sensor, device=binary_sensor,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
) )
for binary_sensor in (
session.device_helper.shutter_contacts
+ session.device_helper.shutter_contacts2
) )
]
entities.extend(
BatterySensor(
device=binary_sensor,
parent_id=session.information.unique_id,
entry_id=config_entry.entry_id,
)
for binary_sensor in ( for binary_sensor in (
session.device_helper.motion_detectors session.device_helper.motion_detectors
+ session.device_helper.shutter_contacts + session.device_helper.shutter_contacts
@ -47,13 +53,6 @@ async def async_setup_entry(
+ session.device_helper.universal_switches + session.device_helper.universal_switches
+ session.device_helper.wallthermostats + session.device_helper.wallthermostats
+ session.device_helper.water_leakage_detectors + session.device_helper.water_leakage_detectors
):
if binary_sensor.supports_batterylevel:
entities.append(
BatterySensor(
device=binary_sensor,
parent_id=session.information.unique_id,
entry_id=config_entry.entry_id,
) )
) )

View File

@ -25,20 +25,17 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up the SHC cover platform.""" """Set up the SHC cover platform."""
entities = []
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION] session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
for cover in session.device_helper.shutter_controls: async_add_entities(
entities.append(
ShutterControlCover( ShutterControlCover(
device=cover, device=cover,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
) )
for cover in session.device_helper.shutter_controls
) )
async_add_entities(entities)
class ShutterControlCover(SHCEntity, CoverEntity): class ShutterControlCover(SHCEntity, CoverEntity):
"""Representation of a SHC shutter control device.""" """Representation of a SHC shutter control device."""

View File

@ -84,64 +84,65 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up the SHC switch platform.""" """Set up the SHC switch platform."""
entities: list[SwitchEntity] = []
session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION] session: SHCSession = hass.data[DOMAIN][config_entry.entry_id][DATA_SESSION]
for switch in session.device_helper.smart_plugs: entities: list[SwitchEntity] = [
entities.append(
SHCSwitch( SHCSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
description=SWITCH_TYPES["smartplug"], description=SWITCH_TYPES["smartplug"],
) )
) for switch in session.device_helper.smart_plugs
entities.append( ]
entities.extend(
SHCRoutingSwitch( SHCRoutingSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
) )
for switch in session.device_helper.smart_plugs
) )
for switch in session.device_helper.light_switches_bsm: entities.extend(
entities.append(
SHCSwitch( SHCSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
description=SWITCH_TYPES["lightswitch"], description=SWITCH_TYPES["lightswitch"],
) )
for switch in session.device_helper.light_switches_bsm
) )
for switch in session.device_helper.smart_plugs_compact: entities.extend(
entities.append(
SHCSwitch( SHCSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
description=SWITCH_TYPES["smartplugcompact"], description=SWITCH_TYPES["smartplugcompact"],
) )
for switch in session.device_helper.smart_plugs_compact
) )
for switch in session.device_helper.camera_eyes: entities.extend(
entities.append(
SHCSwitch( SHCSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
description=SWITCH_TYPES["cameraeyes"], description=SWITCH_TYPES["cameraeyes"],
) )
for switch in session.device_helper.camera_eyes
) )
for switch in session.device_helper.camera_360: entities.extend(
entities.append(
SHCSwitch( SHCSwitch(
device=switch, device=switch,
parent_id=session.information.unique_id, parent_id=session.information.unique_id,
entry_id=config_entry.entry_id, entry_id=config_entry.entry_id,
description=SWITCH_TYPES["camera360"], description=SWITCH_TYPES["camera360"],
) )
for switch in session.device_helper.camera_360
) )
async_add_entities(entities) async_add_entities(entities)

View File

@ -339,12 +339,11 @@ async def async_setup_entry(
) )
entity_registry.async_update_entity(entity_id, new_unique_id=new_unique_id) entity_registry.async_update_entity(entity_id, new_unique_id=new_unique_id)
sensors = [] async_add_entities(
BrotherPrinterSensor(coordinator, description)
for description in SENSOR_TYPES: for description in SENSOR_TYPES
if description.value(coordinator.data) is not None: if description.value(coordinator.data) is not None
sensors.append(BrotherPrinterSensor(coordinator, description)) )
async_add_entities(sensors, False)
class BrotherPrinterSensor( class BrotherPrinterSensor(