예제 #1
0
def create_withings_data_manager(hass: HomeAssistantType,
                                 entry: ConfigEntry) -> WithingsDataManager:
    """Set up the sensor config entry."""
    entry_creds = entry.data.get(const.CREDENTIALS) or {}
    profile = entry.data[const.PROFILE]
    credentials = nokia.NokiaCredentials(
        entry_creds.get("access_token"),
        entry_creds.get("token_expiry"),
        entry_creds.get("token_type"),
        entry_creds.get("refresh_token"),
        entry_creds.get("user_id"),
        entry_creds.get("client_id"),
        entry_creds.get("consumer_secret"),
    )

    def credentials_saver(credentials_param):
        _LOGGER.debug("Saving updated credentials of type %s",
                      type(credentials_param))

        # Sanitizing the data as sometimes a NokiaCredentials object
        # is passed through from the API.
        cred_data = credentials_param
        if not isinstance(credentials_param, dict):
            cred_data = credentials_param.__dict__

        entry.data[const.CREDENTIALS] = cred_data
        hass.config_entries.async_update_entry(entry, data={**entry.data})

    _LOGGER.debug("Creating nokia api instance")
    api = nokia.NokiaApi(
        credentials,
        refresh_cb=(lambda token: credentials_saver(api.credentials)))

    _LOGGER.debug("Creating withings data manager for profile: %s", profile)
    return WithingsDataManager(hass, profile, api)
def auth_nokia(config):
    """ Authenticate client with Nokia Health
    """
    creds = nokia.NokiaCredentials(config.get('nokia', 'access_token'),
                                   config.get('nokia', 'access_token_secret'),
                                   config.get('nokia', 'consumer_key'),
                                   config.get('nokia', 'consumer_secret'),
                                   config.get('nokia', 'user_id'))
    client = nokia.NokiaApi(creds)
    return client
예제 #3
0
async def async_setup_entry(
        hass: HomeAssistantType,
        entry: ConfigEntry,
        async_add_entities: types.Callable[[types.List[Entity], bool], None]
):
    """Set up the sensor config entry."""
    profile = entry.data[const.PROFILE]
    credentials = nokia.NokiaCredentials()
    credentials.__dict__.update(entry.data[const.CREDENTIALS])

    def credentials_saver(credentials_param):
        _LOGGER.debug("Saving updated credentials.")
        entry.data[const.CREDENTIALS] = credentials_param
        hass.config_entries.async_update_entry(entry, data={**entry.data})

    _LOGGER.debug(
        "Creating nokia api instance with credentials %s",
        credentials
    )
    api = nokia.NokiaApi(
        credentials,
        refresh_cb=(lambda token: credentials_saver(
            api.credentials
        ))
    )

    _LOGGER.debug(
        "Creating withings data manager for profile: %s",
        profile
    )
    data_manager = WithingsDataManager(
        profile,
        api
    )

    _LOGGER.debug("Confirming we're authenticated")
    try:
        data_manager.check_authenticated()
    except NotAuthenticatedError:
        # Trigger new config flow.
        hass.async_create_task(
            hass.config_entries.flow.async_init(
                const.DOMAIN,
                context={
                    'source': const.SOURCE_USER,
                    const.PROFILE: profile
                },
                data={}
            )
        )
        return

    entities = create_sensor_entities(hass, data_manager)
    async_add_entities(entities, True)