Exemple #1
0
    def _prepare_result_json(
            self,
            result: data_entry_flow.FlowResult) -> data_entry_flow.FlowResult:
        """Convert result to JSON."""
        if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
            data = result.copy()
            data.pop("result")
            data.pop("data")
            return data

        if result["type"] != data_entry_flow.RESULT_TYPE_FORM:
            return result

        import voluptuous_serialize  # pylint: disable=import-outside-toplevel

        data = result.copy()

        schema = data["data_schema"]
        if schema is None:
            data["data_schema"] = []
        else:
            data["data_schema"] = voluptuous_serialize.convert(
                schema, custom_serializer=cv.custom_serializer)

        return data
Exemple #2
0
def _prepare_result_json(
    result: data_entry_flow.FlowResult, ) -> data_entry_flow.FlowResult:
    """Convert result to JSON."""
    if result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY:
        data = result.copy()
        return data

    if result["type"] != data_entry_flow.FlowResultType.FORM:
        return result

    data = result.copy()

    if (schema := data["data_schema"]) is None:
        data["data_schema"] = []
Exemple #3
0
async def test_duplicate(
    hass: HomeAssistant,
    mqtt: None,
    first_input: dict,
    second_input: dict,
    expected_result: FlowResult,
) -> None:
    """Test duplicate detection."""

    with patch("sys.platform", "win32"), patch(
            "homeassistant.components.mysensors.config_flow.try_connect",
            return_value=True), patch(
                "homeassistant.components.mysensors.async_setup_entry",
                return_value=True,
            ):
        MockConfigEntry(domain=DOMAIN, data=first_input).add_to_hass(hass)

        second_gateway_type = second_input.pop(CONF_GATEWAY_TYPE)
        result = await get_form(hass, second_gateway_type,
                                GATEWAY_TYPE_TO_STEP[second_gateway_type])
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            second_input,
        )
        await hass.async_block_till_done()

        for key, val in expected_result.items():
            assert result[key] == val  # type: ignore[literal-required]
Exemple #4
0
    def _prepare_result_json(
            self,
            result: data_entry_flow.FlowResult) -> data_entry_flow.FlowResult:
        """Convert result to JSON."""
        if result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY:
            data = result.copy()
            data.pop("result")
            data.pop("data")
            return data

        if "data_schema" not in result:
            return result

        data = result.copy()

        if (schema := data["data_schema"]) is None:
            data["data_schema"] = []
Exemple #5
0
 async def _async_flow_result_to_response(
     self,
     request: web.Request,
     client_id: str,
     result: data_entry_flow.FlowResult,
 ) -> web.Response:
     """Convert the flow result to a response."""
     if result["type"] != data_entry_flow.FlowResultType.CREATE_ENTRY:
         # @log_invalid_auth does not work here since it returns HTTP 200.
         # We need to manually log failed login attempts.
         if (result["type"] == data_entry_flow.FlowResultType.FORM
                 and (errors := result.get("errors"))
                 and errors.get("base") in (
                     "invalid_auth",
                     "invalid_code",
                 )):
             await process_wrong_login(request)
         return self.json(_prepare_result_json(result))
    async def complete_external_step(
            self,
            result: data_entry_flow.FlowResult) -> data_entry_flow.FlowResult:
        """Fixture method to complete the OAuth flow and return the completed result."""
        client = await self.hass_client()
        state = config_entry_oauth2_flow._encode_jwt(
            self.hass,
            {
                "flow_id": result["flow_id"],
                "redirect_uri": "https://example.com/auth/external/callback",
            },
        )
        assert result["url"] == (
            f"{AUTHORIZE_URL}?response_type=code&client_id={self.client_id}"
            "&redirect_uri=https://example.com/auth/external/callback"
            f"&state={state}")
        resp = await client.get(
            f"/auth/external/callback?code=abcd&state={state}")
        assert resp.status == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"

        self.aioclient_mock.post(
            TOKEN_URL,
            json={
                "refresh_token": REFRESH_TOKEN,
                "access_token": ACCESS_TOKEN,
                "type": "bearer",
                "expires_in": 60,
            },
        )

        result = await self.hass.config_entries.flow.async_configure(
            result["flow_id"])
        assert result.get("type") == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
        assert result.get("title") == self.client_id
        assert "data" in result
        assert "token" in result["data"]
        return result
Exemple #7
0
 [
     (
         {
             CONF_GATEWAY_TYPE: CONF_GATEWAY_TYPE_MQTT,
             CONF_DEVICE: "mqtt",
             CONF_VERSION: "2.3",
             CONF_TOPIC_IN_PREFIX: "same1",
             CONF_TOPIC_OUT_PREFIX: "same2",
         },
         {
             CONF_GATEWAY_TYPE: CONF_GATEWAY_TYPE_MQTT,
             CONF_VERSION: "2.3",
             CONF_TOPIC_IN_PREFIX: "same1",
             CONF_TOPIC_OUT_PREFIX: "same2",
         },
         FlowResult(type="form",
                    errors={CONF_TOPIC_IN_PREFIX: "duplicate_topic"}),
     ),
     (
         {
             CONF_GATEWAY_TYPE: CONF_GATEWAY_TYPE_MQTT,
             CONF_DEVICE: "mqtt",
             CONF_VERSION: "2.3",
             CONF_TOPIC_IN_PREFIX: "different1",
             CONF_TOPIC_OUT_PREFIX: "different2",
         },
         {
             CONF_GATEWAY_TYPE: CONF_GATEWAY_TYPE_MQTT,
             CONF_VERSION: "2.3",
             CONF_TOPIC_IN_PREFIX: "different3",
             CONF_TOPIC_OUT_PREFIX: "different4",
         },