def test_request_all_info(self):
        """ Test request config with all possible info. """

        values = [
            "config_description", "config image url",
            "config submit caption", []]

        keys = [
            configurator.ATTR_DESCRIPTION, configurator.ATTR_DESCRIPTION_IMAGE,
            configurator.ATTR_SUBMIT_CAPTION, configurator.ATTR_FIELDS]

        exp_attr = dict(zip(keys, values))

        exp_attr[configurator.ATTR_CONFIGURE_ID] = configurator.request_config(
            self.hass, "Test Request", lambda _: None,
            *values)

        states = self.hass.states.all()

        self.assertEqual(1, len(states))

        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        self.assertEqual(exp_attr, state.attributes)
Esempio n. 2
0
    def test_request_all_info(self):
        """Test request config with all possible info."""
        exp_attr = {
            ATTR_FRIENDLY_NAME: "Test Request",
            configurator.ATTR_DESCRIPTION: "config description",
            configurator.ATTR_DESCRIPTION_IMAGE: "config image url",
            configurator.ATTR_SUBMIT_CAPTION: "config submit caption",
            configurator.ATTR_FIELDS: [],
            configurator.ATTR_LINK_NAME: "link name",
            configurator.ATTR_LINK_URL: "link url",
            configurator.ATTR_ENTITY_PICTURE: "config entity picture",
            configurator.ATTR_CONFIGURE_ID: configurator.request_config(
                self.hass,
                name="Test Request",
                callback=lambda _: None,
                description="config description",
                description_image="config image url",
                submit_caption="config submit caption",
                fields=None,
                link_name="link name",
                link_url="link url",
                entity_picture="config entity picture",
            )
        }

        states = self.hass.states.all()
        self.assertEqual(1, len(states))
        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        assert exp_attr == dict(state.attributes)
Esempio n. 3
0
    def test_request_all_info(self):
        """ Test request config with all possible info. """

        values = [
            "config_description", "config image url", "config submit caption",
            []
        ]

        keys = [
            configurator.ATTR_DESCRIPTION, configurator.ATTR_DESCRIPTION_IMAGE,
            configurator.ATTR_SUBMIT_CAPTION, configurator.ATTR_FIELDS
        ]

        exp_attr = dict(zip(keys, values))

        exp_attr[configurator.ATTR_CONFIGURE_ID] = configurator.request_config(
            self.hass, "Test Request", lambda _: None, *values)

        states = self.hass.states.all()

        self.assertEqual(1, len(states))

        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        self.assertEqual(exp_attr, state.attributes)
    def test_request_all_info(self):
        """Test request config with all possible info."""
        exp_attr = {
            ATTR_FRIENDLY_NAME:
            "Test Request",
            configurator.ATTR_DESCRIPTION:
            "config description",
            configurator.ATTR_DESCRIPTION_IMAGE:
            "config image url",
            configurator.ATTR_SUBMIT_CAPTION:
            "config submit caption",
            configurator.ATTR_FIELDS: [],
            configurator.ATTR_CONFIGURE_ID:
            configurator.request_config(self.hass, "Test Request",
                                        lambda _: None, "config description",
                                        "config image url",
                                        "config submit caption")
        }

        states = self.hass.states.all()
        self.assertEqual(1, len(states))
        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        assert exp_attr == dict(state.attributes)
Esempio n. 5
0
    def test_state_change_on_notify_errors(self):
        """Test state change on notify errors."""
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: None)
        error = "Oh no bad bad bad"
        configurator.notify_errors(request_id, error)

        state = self.hass.states.all()[0]
        self.assertEqual(error, state.attributes.get(configurator.ATTR_ERRORS))
Esempio n. 6
0
    def test_request_done_works(self):
        """Test if calling request done works."""
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: None)
        configurator.request_done(request_id)
        self.assertEqual(1, len(self.hass.states.all()))

        self.hass.bus.fire(EVENT_TIME_CHANGED)
        self.hass.block_till_done()
        self.assertEqual(0, len(self.hass.states.all()))
Esempio n. 7
0
    def test_callback_called_on_configure(self):
        """Test if our callback gets called when configure service called."""
        calls = []
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: calls.append(1))

        self.hass.services.call(
            configurator.DOMAIN, configurator.SERVICE_CONFIGURE,
            {configurator.ATTR_CONFIGURE_ID: request_id})

        self.hass.block_till_done()
        self.assertEqual(1, len(calls), "Callback not called")
Esempio n. 8
0
def request_app_setup(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    config_path: str,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Assist user with configuring the Fitbit dev application."""
    def fitbit_configuration_callback(fields: list[dict[str, str]]) -> None:
        """Handle configuration updates."""
        config_path = hass.config.path(FITBIT_CONFIG_FILE)
        if os.path.isfile(config_path):
            config_file = load_json(config_path)
            if config_file == DEFAULT_CONFIG:
                error_msg = "You didn't correctly modify fitbit.conf, please try again."

                configurator.notify_errors(hass, _CONFIGURING["fitbit"],
                                           error_msg)
            else:
                setup_platform(hass, config, add_entities, discovery_info)
        else:
            setup_platform(hass, config, add_entities, discovery_info)

    try:
        description = f"""Please create a Fitbit developer app at
                       https://dev.fitbit.com/apps/new.
                       For the OAuth 2.0 Application Type choose Personal.
                       Set the Callback URL to {get_url(hass, require_ssl=True)}{FITBIT_AUTH_CALLBACK_PATH}.
                       (Note: Your Home Assistant instance must be accessible via HTTPS.)
                       They will provide you a Client ID and secret.
                       These need to be saved into the file located at: {config_path}.
                       Then come back here and hit the below button.
                       """
    except NoURLAvailableError:
        _LOGGER.error(
            "Could not find an SSL enabled URL for your Home Assistant instance. "
            "Fitbit requires that your Home Assistant instance is accessible via HTTPS"
        )
        return

    submit = "I have saved my Client ID and Client Secret into fitbit.conf."

    _CONFIGURING["fitbit"] = configurator.request_config(
        hass,
        "Fitbit",
        fitbit_configuration_callback,
        description=description,
        submit_caption=submit,
        description_image="/static/images/config_fitbit_app.png",
    )
Esempio n. 9
0
    def test_request_least_info(self):
        """Test request config with least amount of data."""
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: None)

        self.assertEqual(
            1, len(self.hass.services.services.get(configurator.DOMAIN, [])),
            "No new service registered")

        states = self.hass.states.all()

        self.assertEqual(1, len(states), "Expected a new state registered")

        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        self.assertEqual(
            request_id, state.attributes.get(configurator.ATTR_CONFIGURE_ID))
Esempio n. 10
0
    def test_request_least_info(self):
        """Test request config with least amount of data."""
        request_id = configurator.request_config(self.hass, "Test Request",
                                                 lambda _: None)

        assert 1 == \
            len(self.hass.services.services.get(configurator.DOMAIN, [])), \
            "No new service registered"

        states = self.hass.states.all()

        assert 1 == len(states), "Expected a new state registered"

        state = states[0]

        assert configurator.STATE_CONFIGURE == state.state
        assert \
            request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
Esempio n. 11
0
    def test_request_least_info(self):
        """Test request config with least amount of data."""
        request_id = configurator.request_config(
            self.hass, "Test Request", lambda _: None)

        assert 1 == \
            len(self.hass.services.services.get(configurator.DOMAIN, [])), \
            "No new service registered"

        states = self.hass.states.all()

        assert 1 == len(states), "Expected a new state registered"

        state = states[0]

        assert configurator.STATE_CONFIGURE == state.state
        assert \
            request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
Esempio n. 12
0
    def test_request_all_info(self):
        """Test request config with all possible info."""
        exp_attr = {
            ATTR_FRIENDLY_NAME: "Test Request",
            configurator.ATTR_DESCRIPTION: "config description",
            configurator.ATTR_DESCRIPTION_IMAGE: "config image url",
            configurator.ATTR_SUBMIT_CAPTION: "config submit caption",
            configurator.ATTR_FIELDS: [],
            configurator.ATTR_CONFIGURE_ID: configurator.request_config(
                self.hass, "Test Request", lambda _: None,
                "config description", "config image url",
                "config submit caption"
            )
        }

        states = self.hass.states.all()
        self.assertEqual(1, len(states))
        state = states[0]

        self.assertEqual(configurator.STATE_CONFIGURE, state.state)
        assert exp_attr == dict(state.attributes)
Esempio n. 13
0
def request_oauth_completion(hass: HomeAssistant) -> None:
    """Request user complete Fitbit OAuth2 flow."""
    if "fitbit" in _CONFIGURING:
        configurator.notify_errors(hass, _CONFIGURING["fitbit"],
                                   "Failed to register, please try again.")

        return

    def fitbit_configuration_callback(fields: list[dict[str, str]]) -> None:
        """Handle configuration updates."""

    start_url = f"{get_url(hass, require_ssl=True)}{FITBIT_AUTH_START}"

    description = f"Please authorize Fitbit by visiting {start_url}"

    _CONFIGURING["fitbit"] = configurator.request_config(
        hass,
        "Fitbit",
        fitbit_configuration_callback,
        description=description,
        submit_caption="I have authorized Fitbit.",
    )
Esempio n. 14
0
                json.dumps(
                    {
                        "namespace": "connect",
                        "method": "connect",
                        "arguments": ["Home Assistant", code],
                    }
                )
            )
            websocket.close()
            break

    _CONFIGURING["gpmdp"] = configurator.request_config(
        DEFAULT_NAME,
        gpmdp_configuration_callback,
        description=(
            "Enter the pin that is displayed in the "
            "Google Play Music Desktop Player."
        ),
        submit_caption="Submit",
        fields=[{"id": "pin", "name": "Pin Code", "type": "number"}],
    )


def setup_gpmdp(hass, config, code, add_entities):
    """Set up gpmdp."""
    name = config.get(CONF_NAME)
    host = config.get(CONF_HOST)
    port = config.get(CONF_PORT)
    url = f"ws://{host}:{port}"

    if not code:
        request_configuration(hass, config, url, add_entities)