"""The Wolf SmartSet Service integration."""
from datetime import timedelta
import logging

from httpx import ConnectError, ConnectTimeout
from wolf_smartset.token_auth import InvalidAuth
from wolf_smartset.wolf_client import FetchFailed, ParameterReadError, WolfClient

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import (
    COORDINATOR,
    DEVICE_GATEWAY,
    DEVICE_ID,
    DEVICE_NAME,
    DOMAIN,
    PARAMETERS,
)

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["sensor"]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Wolf SmartSet Service from a config entry."""
    username = entry.data[CONF_USERNAME]
    password = entry.data[CONF_PASSWORD]
    device_name = entry.data[DEVICE_NAME]
    device_id = entry.data[DEVICE_ID]
    gateway_id = entry.data[DEVICE_GATEWAY]
    refetch_parameters = False
    _LOGGER.debug(
        "Setting up wolflink integration for device: %s (ID: %s, gateway: %s)",
        device_name,
        device_id,
        gateway_id,
    )

    wolf_client = WolfClient(username, password)

    parameters = await fetch_parameters_init(wolf_client, gateway_id, device_id)

    async def async_update_data():
        """Update all stored entities for Wolf SmartSet."""
        try:
            nonlocal refetch_parameters
            nonlocal parameters
            await wolf_client.update_session()
            if not wolf_client.fetch_system_state_list(device_id, gateway_id):
                refetch_parameters = True
                raise UpdateFailed(
                    "Could not fetch values from server because device is Offline."
                )
            if refetch_parameters:
                parameters = await fetch_parameters(wolf_client, gateway_id, device_id)
                hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
                refetch_parameters = False
            values = {
                v.value_id: v.value
                for v in await wolf_client.fetch_value(
                    gateway_id, device_id, parameters
                )
            }
            return {
                parameter.parameter_id: (
                    parameter.value_id,
                    values[parameter.value_id],
                )
                for parameter in parameters
                if parameter.value_id in values
            }
        except ConnectError as exception:
            raise UpdateFailed(
                f"Error communicating with API: {exception}"
            ) from exception
        except FetchFailed as exception:
            raise UpdateFailed(
                f"Could not fetch values from server due to: {exception}"
            ) from exception
        except ParameterReadError as exception:
            refetch_parameters = True
            raise UpdateFailed(
                "Could not fetch values for parameter. Refreshing value IDs."
            ) from exception
        except InvalidAuth as exception:
            raise UpdateFailed("Invalid authentication during update.") from exception

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        name=DOMAIN,
        update_method=async_update_data,
        update_interval=timedelta(minutes=1),
    )

    await coordinator.async_refresh()

    hass.data.setdefault(DOMAIN, {})
    hass.data[DOMAIN][entry.entry_id] = {}
    hass.data[DOMAIN][entry.entry_id][PARAMETERS] = parameters
    hass.data[DOMAIN][entry.entry_id][COORDINATOR] = coordinator
    hass.data[DOMAIN][entry.entry_id][DEVICE_ID] = device_id

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Unload a config entry."""
    unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
    if unload_ok:
        hass.data[DOMAIN].pop(entry.entry_id)

    return unload_ok


async def fetch_parameters(client: WolfClient, gateway_id: int, device_id: int):
    """
    Fetch all available parameters with usage of WolfClient.

    By default Reglertyp entity is removed because API will not provide value for this parameter.
    """
    fetched_parameters = await client.fetch_parameters(gateway_id, device_id)
    return [param for param in fetched_parameters if param.name != "Reglertyp"]


async def fetch_parameters_init(client: WolfClient, gateway_id: int, device_id: int):
    """Fetch all available parameters with usage of WolfClient but handles all exceptions and results in ConfigEntryNotReady."""
    try:
        return await fetch_parameters(client, gateway_id, device_id)
    except (ConnectError, ConnectTimeout, FetchFailed) as exception:
        raise ConfigEntryNotReady(
            f"Error communicating with API: {exception}"
        ) from exception

[((24, 10, 24, 37), 'logging.getLogger', 'logging.getLogger', ({(24, 28, 24, 36): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((44, 18, 44, 48), 'wolf_smartset.wolf_client.WolfClient', 'WolfClient', ({(44, 29, 44, 37): 'username', (44, 39, 44, 47): 'password'}, {}), '(username, password)', False, 'from wolf_smartset.wolf_client import FetchFailed, ParameterReadError, WolfClient\n'), ((98, 24, 98, 44), 'datetime.timedelta', 'timedelta', (), '', False, 'from datetime import timedelta\n'), ((138, 14, 140, 9), 'homeassistant.exceptions.ConfigEntryNotReady', 'ConfigEntryNotReady', ({(139, 12, 139, 56): 'f"""Error communicating with API: {exception}"""'}, {}), "(f'Error communicating with API: {exception}')", False, 'from homeassistant.exceptions import ConfigEntryNotReady\n'), ((56, 22, 58, 17), 'homeassistant.helpers.update_coordinator.UpdateFailed', 'UpdateFailed', ({(57, 20, 57, 83): '"""Could not fetch values from server because device is Offline."""'}, {}), "('Could not fetch values from server because device is Offline.')", False, 'from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed\n'), ((78, 18, 80, 13), 'homeassistant.helpers.update_coordinator.UpdateFailed', 'UpdateFailed', ({(79, 16, 79, 60): 'f"""Error communicating with API: {exception}"""'}, {}), "(f'Error communicating with API: {exception}')", False, 'from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed\n'), ((82, 18, 84, 13), 'homeassistant.helpers.update_coordinator.UpdateFailed', 'UpdateFailed', ({(83, 16, 83, 73): 'f"""Could not fetch values from server due to: {exception}"""'}, {}), "(f'Could not fetch values from server due to: {exception}')", False, 'from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed\n'), ((87, 18, 89, 13), 'homeassistant.helpers.update_coordinator.UpdateFailed', 'UpdateFailed', ({(88, 16, 88, 77): '"""Could not fetch values for parameter. Refreshing value IDs."""'}, {}), "('Could not fetch values for parameter. Refreshing value IDs.')", False, 'from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed\n'), ((91, 18, 91, 71), 'homeassistant.helpers.update_coordinator.UpdateFailed', 'UpdateFailed', ({(91, 31, 91, 70): '"""Invalid authentication during update."""'}, {}), "('Invalid authentication during update.')", False, 'from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed\n')]