Ejemplo n.º 1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """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]
    _LOGGER.debug(
        "Setting up wolflink integration for device: %s (id: %s, gateway: %s)",
        device_name,
        device_id,
        gateway_id,
    )

    wolf_client = WolfClient(username, password)

    try:
        parameters = await fetch_parameters(wolf_client, gateway_id, device_id)
    except InvalidAuth:
        _LOGGER.debug("Authentication failed")
        return False

    async def async_update_data():
        """Update all stored entities for Wolf SmartSet."""
        try:
            values = await wolf_client.fetch_value(gateway_id, device_id,
                                                   parameters)
            return {v.value_id: v.value for v 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 InvalidAuth as exception:
            raise UpdateFailed(
                "Invalid authentication during update.") from exception

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

    await coordinator.async_refresh()

    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.async_create_task(
        hass.config_entries.async_forward_entry_setup(entry, "sensor"))

    return True
Ejemplo n.º 2
0
 async def async_step_user(self, user_input=None):
     """Handle the initial step to get connection parameters."""
     errors = {}
     if user_input is not None:
         wolf_client = WolfClient(user_input[CONF_USERNAME],
                                  user_input[CONF_PASSWORD])
         try:
             self.fetched_systems = await wolf_client.fetch_system_list()
         except ConnectError:
             errors["base"] = "cannot_connect"
         except InvalidAuth:
             errors["base"] = "invalid_auth"
         except Exception:  # pylint: disable=broad-except
             _LOGGER.exception("Unexpected exception")
             errors["base"] = "unknown"
         else:
             self.username = user_input[CONF_USERNAME]
             self.password = user_input[CONF_PASSWORD]
             return await self.async_step_device()
     return self.async_show_form(step_id="user",
                                 data_schema=USER_SCHEMA,
                                 errors=errors)
Ejemplo n.º 3
0
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