예제 #1
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up JuiceNet from a config entry."""

    config = entry.data

    session = async_get_clientsession(hass)

    access_token = config[CONF_ACCESS_TOKEN]
    api = Api(access_token, session)

    juicenet = JuiceNetApi(api)

    try:
        await juicenet.setup()
    except TokenError as error:
        _LOGGER.error("JuiceNet Error %s", error)
        return False
    except aiohttp.ClientError as error:
        _LOGGER.error("Could not reach the JuiceNet API %s", error)
        raise ConfigEntryNotReady from error

    if not juicenet.devices:
        _LOGGER.error("No JuiceNet devices found for this account")
        return False
    _LOGGER.info("%d JuiceNet device(s) found", len(juicenet.devices))

    async def async_update_data():
        """Update all device states from the JuiceNet API."""
        for device in juicenet.devices:
            await device.update_state(True)
        return True

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        name="JuiceNet",
        update_method=async_update_data,
        update_interval=timedelta(seconds=30),
    )

    hass.data[DOMAIN][entry.entry_id] = {
        JUICENET_API: juicenet,
        JUICENET_COORDINATOR: coordinator,
    }

    await coordinator.async_refresh()

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component))

    return True
예제 #2
0
async def validate_input(hass: core.HomeAssistant, data):
    """Validate the user input allows us to connect.

    Data has the keys from DATA_SCHEMA with values provided by the user.
    """
    session = async_get_clientsession(hass)
    juicenet = Api(data[CONF_ACCESS_TOKEN], session)

    try:
        await juicenet.get_devices()
    except TokenError as error:
        _LOGGER.error("Token Error %s", error)
        raise InvalidAuth from error
    except aiohttp.ClientError as error:
        _LOGGER.error("Error connecting %s", error)
        raise CannotConnect from error

    # Return info that you want to store in the config entry.
    return {"title": "JuiceNet"}