diff --git a/homeassistant/components/scrape/config_flow.py b/homeassistant/components/scrape/config_flow.py index 3ca13e56b29..1f74caf83f0 100644 --- a/homeassistant/components/scrape/config_flow.py +++ b/homeassistant/components/scrape/config_flow.py @@ -24,6 +24,7 @@ from homeassistant.const import ( CONF_METHOD, CONF_NAME, CONF_PASSWORD, + CONF_PAYLOAD, CONF_RESOURCE, CONF_TIMEOUT, CONF_UNIQUE_ID, @@ -77,6 +78,7 @@ RESOURCE_SETUP = { vol.Optional(CONF_METHOD, default=DEFAULT_METHOD): SelectSelector( SelectSelectorConfig(options=METHODS, mode=SelectSelectorMode.DROPDOWN) ), + vol.Optional(CONF_PAYLOAD): ObjectSelector(), vol.Optional(CONF_AUTHENTICATION): SelectSelector( SelectSelectorConfig( options=[HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION], diff --git a/homeassistant/components/scrape/strings.json b/homeassistant/components/scrape/strings.json index 4301bb7d5a0..fc2d83dada4 100644 --- a/homeassistant/components/scrape/strings.json +++ b/homeassistant/components/scrape/strings.json @@ -16,6 +16,7 @@ "password": "[%key:common::config_flow::data::password%]", "headers": "Headers", "method": "Method", + "payload": "Payload", "timeout": "Timeout", "encoding": "Character encoding" }, @@ -25,7 +26,8 @@ "verify_ssl": "Enables/disables verification of SSL/TLS certificate, for example if it is self-signed", "headers": "Headers to use for the web request", "timeout": "Timeout for connection to website", - "encoding": "Character encoding to use. Defaults to UTF-8" + "encoding": "Character encoding to use. Defaults to UTF-8", + "payload": "Payload to use when method is POST" } }, "sensor": { @@ -107,6 +109,7 @@ "data": { "resource": "[%key:component::scrape::config::step::user::data::resource%]", "method": "[%key:component::scrape::config::step::user::data::method%]", + "payload": "[%key:component::scrape::config::step::user::data::payload%]", "authentication": "[%key:component::scrape::config::step::user::data::authentication%]", "username": "[%key:component::scrape::config::step::user::data::username%]", "password": "[%key:component::scrape::config::step::user::data::password%]", @@ -121,7 +124,8 @@ "headers": "[%key:component::scrape::config::step::user::data_description::headers%]", "verify_ssl": "[%key:component::scrape::config::step::user::data_description::verify_ssl%]", "timeout": "[%key:component::scrape::config::step::user::data_description::timeout%]", - "encoding": "[%key:component::scrape::config::step::user::data_description::encoding%]" + "encoding": "[%key:component::scrape::config::step::user::data_description::encoding%]", + "payload": "[%key:component::scrape::config::step::user::data_description::payload%]" } } } diff --git a/tests/components/scrape/test_config_flow.py b/tests/components/scrape/test_config_flow.py index 9c6c5e0b4de..9e1895f3a58 100644 --- a/tests/components/scrape/test_config_flow.py +++ b/tests/components/scrape/test_config_flow.py @@ -22,6 +22,7 @@ from homeassistant.const import ( CONF_METHOD, CONF_NAME, CONF_PASSWORD, + CONF_PAYLOAD, CONF_RESOURCE, CONF_TIMEOUT, CONF_UNIQUE_ID, @@ -99,6 +100,68 @@ async def test_form( assert len(mock_setup_entry.mock_calls) == 1 +async def test_form_with_post( + hass: HomeAssistant, get_data: MockRestData, mock_setup_entry: AsyncMock +) -> None: + """Test we get the form using POST method.""" + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["step_id"] == "user" + assert result["type"] == FlowResultType.FORM + + with patch( + "homeassistant.components.rest.RestData", + return_value=get_data, + ) as mock_data: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_RESOURCE: "https://www.home-assistant.io", + CONF_METHOD: "GET", + CONF_PAYLOAD: "POST", + CONF_VERIFY_SSL: True, + CONF_TIMEOUT: 10.0, + }, + ) + await hass.async_block_till_done() + result3 = await hass.config_entries.flow.async_configure( + result2["flow_id"], + { + CONF_NAME: "Current version", + CONF_SELECT: ".current-version h1", + CONF_INDEX: 0.0, + CONF_DEVICE_CLASS: NONE_SENTINEL, + CONF_STATE_CLASS: NONE_SENTINEL, + CONF_UNIT_OF_MEASUREMENT: NONE_SENTINEL, + }, + ) + await hass.async_block_till_done() + + assert result3["type"] == FlowResultType.CREATE_ENTRY + assert result3["version"] == 1 + assert result3["options"] == { + CONF_RESOURCE: "https://www.home-assistant.io", + CONF_METHOD: "GET", + CONF_PAYLOAD: "POST", + CONF_VERIFY_SSL: True, + CONF_TIMEOUT: 10.0, + CONF_ENCODING: "UTF-8", + "sensor": [ + { + CONF_NAME: "Current version", + CONF_SELECT: ".current-version h1", + CONF_INDEX: 0.0, + CONF_UNIQUE_ID: "3699ef88-69e6-11ed-a1eb-0242ac120002", + } + ], + } + + assert len(mock_data.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_flow_fails( hass: HomeAssistant, get_data: MockRestData, mock_setup_entry: AsyncMock ) -> None: