Ejemplo n.º 1
0
def setup(hass, config):
    """Set up the Somfy component."""
    from pymfy.api.somfy_api import SomfyApi

    hass.data[DOMAIN] = {}

    # This is called to create the redirect so the user can Authorize Home .
    redirect_uri = '{}{}'.format(
        hass.config.api.base_url, SOMFY_AUTH_CALLBACK_PATH)
    conf = config[DOMAIN]
    api = SomfyApi(conf.get(CONF_CLIENT_ID),
                   conf.get(CONF_CLIENT_SECRET),
                   redirect_uri, hass.config.path(DEFAULT_CACHE_PATH))
    hass.data[DOMAIN][API] = api

    if not api.token:
        authorization_url, _ = api.get_authorization_url()
        hass.components.persistent_notification.create(
            'In order to authorize Home Assistant to view your Somfy devices'
            ' you must visit this <a href="{}" target="_blank">link</a>.'
            .format(authorization_url),
            title=NOTIFICATION_TITLE,
            notification_id=NOTIFICATION_CB_ID
        )
        hass.http.register_view(SomfyAuthCallbackView(config))
    else:
        update_all_devices(hass)
        for component in SOMFY_COMPONENTS:
            discovery.load_platform(hass, component, DOMAIN, {}, config)

    return True
Ejemplo n.º 2
0
    async def _get_authorization_url(self):
        """Get Somfy authorization url."""
        from pymfy.api.somfy_api import SomfyApi
        flow = self.hass.data[DATA_FLOW_IMPL][self.flow_impl]
        client_id = flow[CLIENT_ID]
        client_secret = flow[CLIENT_SECRET]
        redirect_uri = '{}{}'.format(self.hass.config.api.base_url,
                                     AUTH_CALLBACK_PATH)
        api = SomfyApi(client_id, client_secret, redirect_uri)

        self.hass.http.register_view(SomfyAuthCallbackView())
        return api.get_authorization_url()