Repository: home-assistant/core
File: homeassistant/components/eheimdigital/light.py (Python)

Code before:
async def async_setup_entry(
    hass: HomeAssistant,
    entry: EheimDigitalConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the callbacks for the coordinator so lights can be added as devices are found."""
    coordinator = entry.runtime_data

    def async_setup_device_entities(
        devices: dict[str, EheimDigitalDevice],
    ) -> dict[str, bool]:
        """Set up the light entities for one or multiple devices."""
        return_value: dict[str, bool] = {}
        entities: list[EheimDigitalClassicLEDControlLight] = []
        for device in devices.values():
            try:
                if isinstance(device, EheimDigitalClassicLEDControl):
                    for channel in range(2):
                        if len(device.tankconfig[channel]) > 0:
                            entities.append(  # noqa: PERF401
                                EheimDigitalClassicLEDControlLight(
                                    coordinator, device, channel
                                )
                            )
            except EheimDigitalDataMissingError:
                return_value[device.mac_address] = False
            else:
                if device.mac_address not in return_value:
                    return_value[device.mac_address] = True

        async_add_entities(entities, update_before_add=True)
        return return_value

    coordinator.add_platform_callback(async_setup_device_entities)
    async_setup_device_entities(coordinator.hub.devices)


class EheimDigitalClassicLEDControlLight(
    EheimDigitalEntity[EheimDigitalClassicLEDControl], LightEntity
):
    """Represent a EHEIM Digital classicLEDcontrol light."""

    _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
    _attr_color_mode = ColorMode.BRIGHTNESS
    _attr_effect_list = [EFFECT_DAYCL_MODE]
    _attr_supported_features = LightEntityFeature.EFFECT
    _attr_translation_key = "channel"

    def __init__(
        self,
        coordinator: EheimDigitalUpdateCoordinator,

Review comment (suggestion):
Okay so I am not completely sure when data is missing, but I think we should keep logic on if an entity should be created outside of the entity instead of letting the entity constructor raise an error

Code after:
async def async_setup_entry(
    hass: HomeAssistant,
    entry: EheimDigitalConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up the callbacks for the coordinator so lights can be added as devices are found."""
    coordinator = entry.runtime_data

    def async_setup_device_entities(
        device_address: dict[str, EheimDigitalDevice],
    ) -> None:
        """Set up the light entities for one or multiple devices."""
        entities: list[EheimDigitalClassicLEDControlLight] = []
        for device in device_address.values():
            if isinstance(device, EheimDigitalClassicLEDControl):
                for channel in range(2):
                    if len(device.tankconfig[channel]) > 0:
                        entities.append(
                            EheimDigitalClassicLEDControlLight(
                                coordinator, device, channel
                            )
                        )
                        coordinator.known_devices.add(device.mac_address)

        async_add_entities(entities)

    coordinator.add_platform_callback(async_setup_device_entities)
    async_setup_device_entities(coordinator.hub.devices)


class EheimDigitalClassicLEDControlLight(
    EheimDigitalEntity[EheimDigitalClassicLEDControl], LightEntity
):
    """Represent a EHEIM Digital classicLEDcontrol light."""

    _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
    _attr_color_mode = ColorMode.BRIGHTNESS
    _attr_effect_list = [EFFECT_DAYCL_MODE]
    _attr_supported_features = LightEntityFeature.EFFECT
    _attr_translation_key = "channel"

    def __init__(
        self,
        coordinator: EheimDigitalUpdateCoordinator,
        device: EheimDigitalClassicLEDControl,
        channel: int,
    ) -> None:
        """Initialize an EHEIM Digital classicLEDcontrol light entity."""
        super().__init__(coordinator, device)
        self._channel = channel
        self._attr_translation_placeholders = {"channel_id": str(channel)}