Repository: home-assistant/core
File: homeassistant/components/ecovacs/controller.py (Python)

Code before:
        )
        self._api_client = ApiClient(self._authenticator)

        mqtt_url = config.get(CONF_OVERRIDE_MQTT_URL)
        ssl_context: UndefinedType | ssl.SSLContext = UNDEFINED
        if not config.get(CONF_VERIFY_MQTT_CERTIFICATE, True) and mqtt_url:
            ssl_context = get_default_no_verify_context()

        self._mqtt_config_fn = partial(
            create_mqtt_config,
            device_id=self._device_id,
            country=country,
            override_mqtt_url=mqtt_url,
            ssl_context=ssl_context,
        )
        self._mqtt_client: MqttClient | None = None

        self._added_legacy_entities: set[str] = set()

    async def initialize(self) -> None:
        """Init controller."""
        try:
            devices, credentials = await asyncio.gather(
                self._api_client.get_devices(),
                self._authenticator.authenticate(),
            )
            mqtt = await self._get_mqtt_client()
            mqtt_devices = [Device(info, self._authenticator) for info in devices.mqtt]

            # Initialize all devices concurrently
            results = await asyncio.gather(
                *(device.initialize(mqtt) for device in mqtt_devices),
                return_exceptions=True,
            )

            # Keep only successful ones, we could also use extend and then end up with:
            # self._devices.extend(mqtt_devices), I believe. But up to the ones who know the integration better. :)
            for device, _ in zip(mqtt_devices, results, strict=False):
                self._devices.append(device)

            for device_config in devices.xmpp:
                bot = VacBot(
                    credentials.user_id,
                    EcoVacsAPI.REALM,
                    self._device_id[0:8],
                    credentials.token,
                    device_config,
                    self._continent,
                    monitor=True,
                )
                self._legacy_devices.append(bot)

Review comment (security):
This has no benefit as the authentication function will terminate immediately, if we already have a valid token. In this case we have it for sure as `get_devices` is internally calling also `authenticate`

Code after:
        )
        self._api_client = ApiClient(self._authenticator)

        mqtt_url = config.get(CONF_OVERRIDE_MQTT_URL)
        ssl_context: UndefinedType | ssl.SSLContext = UNDEFINED
        if not config.get(CONF_VERIFY_MQTT_CERTIFICATE, True) and mqtt_url:
            ssl_context = get_default_no_verify_context()

        self._mqtt_config_fn = partial(
            create_mqtt_config,
            device_id=self._device_id,
            country=country,
            override_mqtt_url=mqtt_url,
            ssl_context=ssl_context,
        )
        self._mqtt_client: MqttClient | None = None

        self._added_legacy_entities: set[str] = set()

    async def initialize(self) -> None:
        """Init controller."""
        try:
            devices = await self._api_client.get_devices()
            credentials = await self._authenticator.authenticate()

            if devices.mqtt:
                mqtt = await self._get_mqtt_client()
                mqtt_devices = [
                    Device(info, self._authenticator) for info in devices.mqtt
                ]
                async with asyncio.TaskGroup() as tg:

                    async def _init(device: Device) -> None:
                        """Initialize MQTT device."""
                        await device.initialize(mqtt)
                        self._devices.append(device)

                    for device in mqtt_devices:
                        tg.create_task(_init(device))

            for device_config in devices.xmpp:
                bot = VacBot(
                    credentials.user_id,
                    EcoVacsAPI.REALM,
                    self._device_id[0:8],
                    credentials.token,
                    device_config,
                    self._continent,
                    monitor=True,
                )
                self._legacy_devices.append(bot)