Repository: home-assistant/core
File: homeassistant/components/dexcom/__init__.py (Python)

Code before:
"""The Dexcom integration."""

from pydexcom import AccountError, Dexcom, SessionError

from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from .const import CONF_SERVER, PLATFORMS, SERVER_OUS
from .coordinator import DexcomConfigEntry, DexcomCoordinator


async def async_setup_entry(hass: HomeAssistant, entry: DexcomConfigEntry) -> bool:
    """Set up Dexcom from a config entry."""
    try:
        dexcom = await hass.async_add_executor_job(
            Dexcom,
            entry.data[CONF_USERNAME],
            entry.data[CONF_PASSWORD],
            entry.data[CONF_SERVER] == SERVER_OUS,
        )
    except AccountError:
        return False
    except SessionError as error:
        raise ConfigEntryNotReady from error

    coordinator = DexcomCoordinator(hass, entry=entry, dexcom=dexcom)
    await coordinator.async_config_entry_first_refresh()

    entry.runtime_data = coordinator

    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    return True


async def async_unload_entry(hass: HomeAssistant, entry: DexcomConfigEntry) -> bool:
    """Unload a config entry."""
    return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


Review comment (suggestion):
Answering @epenet's [question in the superseded PR](https://github.com/home-assistant/core/pull/161355#discussion_r2711412683) regarding `lambda` usage...

`Dexcom` [uses `kwargs`](https://github.com/gagebenne/pydexcom/blob/696cc9c6bc2b48628ceebda2f64049c36dbf8c17/pydexcom/dexcom.py#L39):
```python
    def __init__(
        self,
        *,
        password: str,
        account_id: str | None = None,
        username: str | None = None,
        region: Region = Region.US,
    ) -> None:
```

`async_add_executor_job` [only accepts `*args`](https://github.com/home-assistant/core/blob/db9a805ff0720ccab64fd3b1af4c6d1fc9a09085/homeassistant/core.py#L875C5-L887C20):
```python
    @callback
    def async_add_executor_job[*_Ts, _T](
        self, target: Callable[[*_Ts], _T], *args: *_Ts
    ) -> asyncio.Future[_T]:
```

Code after:
"""The Dexcom integration."""

from pydexcom import Dexcom, Region
from pydexcom.errors import AccountError, SessionError

from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

from .const import CONF_SERVER, PLATFORMS, SERVER_OUS
from .coordinator import DexcomConfigEntry, DexcomCoordinator


async def async_setup_entry(hass: HomeAssistant, entry: DexcomConfigEntry) -> bool:
    """Set up Dexcom from a config entry."""
    try:
        dexcom = await hass.async_add_executor_job(
            lambda: Dexcom(
                username=entry.data[CONF_USERNAME],
                password=entry.data[CONF_PASSWORD],
                region=Region.OUS
                if entry.data[CONF_SERVER] == SERVER_OUS
                else Region.US,
            )
        )
    except AccountError:
        return False
    except SessionError as error:
        raise ConfigEntryNotReady from error

    coordinator = DexcomCoordinator(hass, entry=entry, dexcom=dexcom)
    await coordinator.async_config_entry_first_refresh()

    entry.runtime_data = coordinator

    await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

    return True


async def async_unload_entry(hass: HomeAssistant, entry: DexcomConfigEntry) -> bool:
    """Unload a config entry."""
    return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)